You need to enable JavaScript to run this app.
导航

使用火山引擎 Go SDK

最近更新时间2023.08.03 19:53:55

首次发布时间2023.08.03 19:53:55

本文档介绍如何使用 火山引擎 Go SDK 调用火山引擎云解析(DNS)的 API。

前提条件

  • 您已经开通了云解析 DNS。参见 快速入门
  • 您使用的 Go 版本需要与 SDK 兼容。您可以参见 go.mod 了解 SDK 支持的 Go 版本。

获取 SDK

使用以下命令获取火山引擎 Go SDK。您需要根据您的 Go 版本选择不同的命令。

# Go 1.17 或更低版本
go get -u github.com/volcengine/volc-sdk-golang
# Go 1.18 或更高版本
go install github.com/volcengine/volc-sdk-golang@latest

实现步骤

  1. 创建环境变量。

  2. 调用 NewClient 方法初始化 SDK。

  3. 调用 API。本文档以 ListZones API 为例。

示例代码

实现步骤的示例代码如下:

package main

import (
	"context"
	"fmt"
	"os"
	"encoding/json"

	dns "github.com/volcengine/volc-sdk-golang/service/dns"
)

func main() {
	// 创建环境变量
	// your_ak 是您的 Access Key ID
	// your_sk 是您的 Secret Access Key
	os.Setenv("VOLC_ACCESSKEY", "Your Access Key ID")
	os.Setenv("VOLC_SECRETKEY", "Your Access Key Secret")

	// 初始化 SDK
	ctx := context.Background()
	client := dns.NewClient(dns.NewVolcCaller())


	// 调用 ListZones API
	resp, err := client.ListZones(ctx, &dns.ListZonesRequest{})
	if err != nil {
		panic(err)
	}

	// Marshal the struct into JSON format
	jsonBytes, err := json.Marshal(resp)
	if err != nil {
		panic(err)
	}
	
	// Convert the JSON bytes to a string
	respString := string(jsonBytes)
	fmt.Println(respString)
}