All Products
Search
Document Center

EventBridge:SDK for Go

Last Updated:Nov 07, 2023

This topic describes how to install the SDK for Go provided by EventBridge and how to use the SDK for Go to publish events. Sample code is also provided in this topic.

Overview

EventBridge SDKs are classified into management API SDKs and data API SDKs. Sample code and dependencies that are used by different types of SDKs vary.

  • Management API SDKs: the SDKs that are used to perform operations on the EventBridge console.

  • Data API SDKs: the channel for event data. Only the PutEvents API operation is called by using this type of SDK.

Before you begin

Perform the following operations:

Environment preparations

  • Environment requirements

    Go 1.12.0 or later is installed. For more information, see Installing Go from source.

  • Check the Go version

    Run the go version command to view the version of Go.

Management API SDKs

Install the SDK for Go

  1. Run the following command to enable Go modules:

    go env -w GO111MODULE=on
  2. Run the following command to configure a Go module proxy:

    go env -w GOPROXY=https://goproxy.cn,direct 
  3. Run the following command to initialize Go modules and generate the go.mod file:

    go mod init
  4. Run the following command to install the SDK for Go:

    go get github.com/alibabacloud-go/eventbridge-20200401/v2  
  5. Run the following command to install the output library for Go:

    go get github.com/alibabacloud/tea-console

Sample code

You can call the corresponding API operation in OpenAPI Explorer to automatically generate sample code. For more information, see Automatic generation of SDK examples.

Data API SDKs

Install the SDK for Go

  1. Run the following command to enable Go modules:

    go env -w GO111MODULE=on
  2. Run the following command to configure a Go module proxy:

    go env -w GOPROXY=https://goproxy.cn,direct 
  3. Run the following command to initialize Go modules and generate the go.mod file:

    go mod init
  4. Run the following command to install the SDK for Go:

    go get github.com/alibabacloud-go/eventbridge-sdk    
  5. Run the following command to install the output library for Go:

    go get github.com/alibabacloud/tea-console

Sample code

Data API SDKs support only the PutEvents API operation. If you want to use the SDK for Go to publish one or more events, refer to the following sample code:

package main

import (
    "fmt"

    eventbridge "github.com/alibabacloud-go/eventbridge-sdk/eventbridge"
)

func main() {
    config := new(eventbridge.Config).
        SetAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
        SetAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
        SetEndpoint("<endpoint>")

    client, err := eventbridge.NewClient(config)
    if err != nil {
        panic(err)
    }

    event := new(eventbridge.CloudEvent).
        SetDatacontenttype("application/json").
        SetData([]byte("test")).
        SetId("id").
        SetSource("source").
        SetTime("2020-08-24T13:54:05.965Asia/Shanghai").
        SetSubject("1.0").
        SetType("type").
        SetExtensions(map[string]interface{}{
            "aliyuneventbusname": "BusName",
        })

    resp, err := client.PutEvents([]*eventbridge.CloudEvent{event})
    if err != nil {
        panic(err)
    }
    fmt.Println(resp)
}