tencent cloud

Go SDK

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2025-04-10 00:41:11

Overview

Welcome to use the TBaaS Product Developer Toolkit (SDK) 3.0. SDK 3.0 is a companion tool for the TencentCloud API 3.0 platform. To facilitate Go developers in debugging and integrating with the TBaaS product API, this section introduces the TBaaS product development toolkit suitable for Go and provides a simple example for first-time use of the development toolkit. This allows you to quickly obtain the TBaaS product Go SDK and start calling.

Dependent Environment

1. Dependent Environment: Go 1.9 or higher, and set required environment variables such as GOPATH.
2. Enable the TBaaS product through the Tencent Cloud console.
3. Obtain SecretID and SecretKey as well as the API call address (tbaas.tencentcloudapi.com).

Get Installation

Install Go SDK and before first-time use of TencentCloud API, users need to apply for and obtain security credentials in the Tencent Cloud console. Security credentials include SecretID and SecretKey. SecretID is used to identify the identity of the API caller, and SecretKey is used to encrypt signature strings and as the server-side verification key for signature strings. SecretKey must be kept safely to avoid leakage.

Install Via Go Get (Recommended)

Use the built-in tool to install the SDK.
go get -u github.com/tencentcloud/tencentcloud-sdk-go

Install Via Source Code Package

1. Go to the Github code hosting address to download the latest code.
2. Extract the obtained source code package and install it under the directory $GOPATH/src/github.com/tencentcloud.

API List

View the detailed API list in the API document.

Example

Take calling the Fabric user contract to execute a transaction (InvokeFabricChaincode) API as an example:
package main

import (
"fmt"

"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
tbaas "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tbaas/v20180416"
)

func main() {
// Instantiate an authentication object. Input parameters need to be passed in with the SecretId and SecretKey of your Tencent Cloud account. Here, you also need to pay attention to keeping the confidentiality of the key pair.
credential := common.NewCredential(
"",
"",
)
// Instantiate a client option, optional, which can be skipped if there is no special requirement.
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tbaas.intl.tencentcloudapi.com"
// Instantiate the client object to request the product, and clientProfile is optional.
client, _ := tbaas.NewClient(credential, "ap-hongkong", cpf)

// Instantiate a request object. Each interface will correspond to a request object.
request := tbaas.NewInvokeFabricChaincodeRequest()

request.ClusterId = common.StringPtr("fabric-65z42qi150")
request.ChannelId = common.StringPtr("channel-9xej4d")
request.ChaincodeName = common.StringPtr("fact")
request.FuncName = common.StringPtr("Set")
request.FuncParam = common.StringPtrs([]string{ "key", "value" })
request.WithAsyncResult = common.BoolPtr(false)
// The returned resp is an instance of InvokeFabricChaincodeResponse, corresponding to the request object.
response, err := client.InvokeFabricChaincode(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
// Output string in json format.
fmt.Printf("%s", response.ToJsonString())
}


Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan