全部產品
Search
文件中心

ApsaraVideo Live:Go SDK使用說明

更新時間:Aug 13, 2025

本文介紹ApsaraVideo for Live服務端Go SDK的使用方法和範例程式碼。以添加直播網域名稱介面為例,協助您快速掌握ApsaraVideo for Live介面的使用方法。

前提條件

Go環境版本 >= 1.10.x。

操作步驟

  1. 登入。在快速入門頁簽中,擷取ApsaraVideo for Live的SDK安裝方式。

    image

  2. 建立專案工程,並使用Credentials工具管理訪問憑證。具體詳情,請參見管理訪問憑證

  3. 發起請求並處理應答或異常。

    // 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:
    // 
    // 使用憑據初始化帳號Client
    // 
    // @return Client
    // 
    // @throws Exception
    func CreateClient () (_result *live20161101.Client, _err error) {
      // 工程代碼建議使用更安全的無AK方式,憑據配置方式請參見: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,
      }
      // Endpoint 請參考 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
          }
        }()
        // 複製代碼運行請自行列印 API 的傳回值
        _, _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())
        }
        // 此處僅做列印展示,請謹慎對待異常處理,在工程專案中切勿直接忽略異常。
        // 錯誤 message
        fmt.Println(tea.StringValue(error.Message))
        // 診斷地址
        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)
      }
    }

更多API樣本,請參見API概覽