You need to enable JavaScript to run this app.
导航
Go SDK
最近更新时间:2022.11.17 10:15:08首次发布时间:2022.09.09 10:30:17

本文介绍镜像仓库 Go SDK 的使用方法以及代码示例。

前提条件

  • 支持 Go 1.5 及以上版本,推荐使用 Go 1.12+ 版本。
  • Service 目录下的文件不得修改,由代码生成器自动生成。
  • 建议使用 go mod 方式进行依赖管理。

安装配置

  • 安装火山引擎 SDK
go get github.com/volcengine/volcengine-go-sdk/volcengine
go get github.com/volcengine/volcengine-go-sdk/volcengine/session
  • 安装镜像仓库 CR SDK
go get github.com/volcengine/volcengine-go-sdk/service/cr

SDK 示例

每个接口都有一个对应的 Request 结构和一个 Response 结构。以下为创建命名空间接口 CreateNamespace 的 SDK 示例。

package main

import (
        "fmt"

        "github.com/volcengine/volcengine-go-sdk/service/cr"
        "github.com/volcengine/volcengine-go-sdk/volcengine"
        "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
        ak := "your_ak"
        sk := "your_sk"
        region := "your_region"

        //if use env Credentials
        //please
        //export volcengine_ACCESS_KEY=your_ak
        //export volcengine_SECRET_KEY=your_sk
        // and WithCredentials(credentials.NewEnvCredentials())

        // Init client
        config := volcengine.NewConfig().
                WithRegion(region).
                WithAkSk(ak, sk)
                //WithCredentials(credentials.NewEnvCredentials()).
                //WithLogLevel(volcengine.LogDebugWithHTTPBody)
        sess, _ := session.NewSession(config)
        svc := cr.New(sess)

        // Call OpenAPI cr CreateNamespace
        in := &cr.CreateNamespaceInput{
                Name: volcengine.String("ns-1"),
                Registry: volcengine.String("registry-1"),
        }

        resp, err := svc.CreateNamespace(in)
        if err != nil {
                panic(err)
        }
        fmt.Printf("Response is %v", resp)
}