This topic describes how to use Content Moderation SDK for Go to perform optical character recognition (OCR). This way, you can recognize text in images.


Go dependencies are installed. For more information, see Installation.
Note You must use the required Go version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Submit synchronous OCR tasks

Interface Description Supported region
ImageSyncScanRequest Submits synchronous OCR tasks to recognize text in images. Set the scenes parameter to ocr.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
  • ap-southeast-5: Indonesia (Jakarta)
Sample code
package main

import (

func main() {
   // Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account. 
   client, err := green.NewClientWithAccessKey("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret")
   if err != nil {

   task1 := map[string]interface{}{"dataId": "ID of the image to be moderated", "url": "URL of the image to be moderated"}
   // Example: If you moderate the image of the front side of an ID card, specify id-card-front. 
   cardExtras := map[string]interface{}{"card": "id-card-front"}
   // Example: If you moderate the image of the front side of an ID card, specify id-card-back. 
   // cardExtras := map[string]interface{}{"card": "id-card-back"}
   // scenes: the moderation scenarios. 
   content, _ := json.Marshal(
         "tasks": task1, "scenes": [...]string{"ocr"}, "bizType": "Business scenario", "extras": cardExtras,

   request := green.CreateImageSyncScanRequest()
   response, _err := client.ImageSyncScan(request)
   if _err != nil {
   if response.GetHttpStatus() != 200 {
      fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))