Go SDK usage

Updated at:
Copy as MD

Use the ApsaraVideo Live Go SDK to call API operations. The following example demonstrates how to add a live streaming domain.

Prerequisites

Go 1.10.x or later is installed.

Procedure

  1. Go to the . On the Quick Start tab, find the installation method for the ApsaraVideo Live SDK.

    Set SDK Generation to V2.0 (Recommended) and Language to Go. Use Go 1.10.x or later. In the Installation Method area, run the command go get github.com/alibabacloud-go/live-20161101/v2 to install the SDK.

  2. Create a project and use the Credentials tool to manage your access credentials. For more information, see Managing access credentials.

  3. Send a request and handle the response or exceptions.

    // This file is auto-generated, don't edit it. Thanks.
    package main
    import (
      "encoding/json"
      "strings"
      "fmt"
      "os"
      live20161101  "github.com/alibabacloud-go/live-20161101/v2/client"
      openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
      util  "github.com/alibabacloud-go/tea-utils/v2/service"
      credential  "github.com/aliyun/credentials-go/credentials"
      "github.com/alibabacloud-go/tea/tea"
    )
    // Description:
    // 
    // Initializes a client by using access credentials.
    // 
    // @return Client
    // 
    // @throws Exception
    func CreateClient () (_result *live20161101.Client, _err error) {
      // For improved security, we recommend that you do not hard-code your access credentials in your code. For information about how to configure credentials, see https://www.alibabacloud.com/help/document_detail/378661.html.
      credential, _err := credential.NewCredential(nil)
      if _err != nil {
        return _result, _err
      }
      config := &openapi.Config{
        Credential: credential,
      }
      // For more information about endpoints, see https://api.aliyun.com/product/live.
      config.Endpoint = tea.String("live.aliyuncs.com")
      _result = &live20161101.Client{}
      _result, _err = live20161101.NewClient(config)
      return _result, _err
    }
    func _main (args []*string) (_err error) {
      client, _err := CreateClient()
      if _err != nil {
        return _err
      }
      addLiveDomainRequest := &live20161101.AddLiveDomainRequest{}
      runtime := &util.RuntimeOptions{}
      tryErr := func()(_e error) {
        defer func() {
          if r := tea.Recover(recover()); r != nil {
            _e = r
          }
        }()
        // To view the API return value, add code to print it.
        _, _err = client.AddLiveDomainWithOptions(addLiveDomainRequest, runtime)
        if _err != nil {
          return _err
        }
        return nil
      }()
      if tryErr != nil {
        var error = &tea.SDKError{}
        if _t, ok := tryErr.(*tea.SDKError); ok {
          error = _t
        } else {
          error.Message = tea.String(tryErr.Error())
        }
        // This code is for demonstration only. In your project, you must handle exceptions properly and not ignore them.
        // Error message
        fmt.Println(tea.StringValue(error.Message))
        // Diagnostic information
        var data interface{}
        d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
        d.Decode(&data)
        if m, ok := data.(map[string]interface{}); ok {
          recommend, _ := m["Recommend"]
          fmt.Println(recommend)
        }
        _, _err = util.AssertAsString(error.Message)
        if _err != nil {
          return _err
        }
      }
      return _err
    }
    func main() {
      err := _main(tea.StringSlice(os.Args[1:]))
      if err != nil {
        panic(err)
      }
    }

For more code samples, see the API Reference.