Creating an Amazon SNS Topic - AWS SDK for Go (version 1)

We announced the upcoming end-of-support for AWS SDK for Go V1. We recommend that you migrate to AWS SDK for Go V2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

Creating an Amazon SNS Topic

The following example creates a topic with the name from the command line, in your default region, and displays the resulting topic ARN.

package main import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sns" "fmt" "os" ) func main() { if len(os.Args) < 2 { fmt.Println("You must supply a topic name") fmt.Println("Usage: go run SnsCreateTopic.go TOPIC") os.Exit(1) } // Initialize a session that the SDK will use to load // credentials from the shared credentials file. (~/.aws/credentials). sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := sns.New(sess) result, err := svc.CreateTopic(&sns.CreateTopicInput{ Name: aws.String(os.Args[1]), }) if err != nil { fmt.Println(err.Error()) os.Exit(1) } fmt.Println(*result.TopicArn) }

See the complete example on GitHub.