SDK overview
Preparation
Pub/Sub currently supports Go SDK. To use Go SDK, complete the following prerequisites.
Get IAM access key
To obtain a user authentication token (API authentication token), first create an access key and then retrieve the access key ID and secret access key.
You can only access resources in the project associated with the issued access key.
-
Go to KakaoCloud console > User profile > Credentials > IAM access keys.
-
After creating the user access key, refer to Prepare API usage to retrieve the access key ID and secret access key.
SDK requirements
Use Go SDK
Ensure the following requirements are met to use the Go SDK.
-
Install Go 1.25 or later, then download the Go SDK using the following commands.
Download Go packagewget https://objectstorage.kr-central-2.kakaocloud.com/v1/e9130193fc734337b2b0c1da50e44395/pubsub-sdk/go/v2.0.0/pubsub.tgz
tar -xf pubsub.tgz
cd {your-project}
go mod edit -require pubsub-sdk-go@v0.0.0
go mod edit -replace pubsub-sdk-go={pubsub-sdk-path}The downloaded SDK is release 2.0.0. Because its module path is
pubsub-sdk-gowithout a/v2suffix, usev0.0.0as the local module placeholder and resolve it throughreplaceto the downloaded directory.go.mod file resultsmodule ...
require (
pubsub-sdk-go v0.0.0
)
replace pubsub-sdk-go => {pubsub-sdk-path} -
Refer to Get IAM access key to issue credentials. The SDK authenticates with user access keys. Set the access key ID as
CredentialIDand the secret access key asCredentialSecretin theAccessKeytype, as shown below.Example using credentialsaccessKey := pubsub.AccessKey{CredentialID: "credentialID", CredentialSecret: "credentialSecret"}
opts := []option.ClientOption{
option.WithAccessKey(accessKey),
}
client, err := pubsub.NewClient(ctx, opts...)
You can only access resources in the project associated with the issued access key.
Client
Use client objects to manage topics and subscriptions and to send and receive messages.
You must release resources like threads used by the client object by calling the Close() method.
Configure custom settings
Authentication and endpoint values are managed through ClientOption. Create ClientOption values when creating a client object to configure your own settings.
The default endpoint is set to the kr-central-2 region's endpoint.
| Method | Description |
|---|---|
| Close() | Release resources such as threads |
import (
"context"
"fmt"
"pubsub-sdk-go"
"pubsub-sdk-go/option"
)
func test(credentialID, credentialSecret string) error {
ctx := context.Background()
accessKey := pubsub.AccessKey{CredentialID: credentialID, CredentialSecret: credentialSecret}
opts := []option.ClientOption{
option.WithAccessKey(accessKey),
}
client, err := pubsub.NewClient(ctx, opts...)
if err != nil {
return fmt.Errorf("pubsub.NewClient: %v", err)
}
// do something
err = client.Close()
return err
}