本文介绍如何使用Go SDK创建一个事件总线。

示例代码如下:

package main

import (
    eventbridge "github.com/alibabacloud-go/eventbridge-sdk/eventbridge"
    console "github.com/alibabacloud-go/tea-console/client"
    util "github.com/alibabacloud-go/tea-utils/service"
    "github.com/alibabacloud-go/tea/tea"
)

/**
* Create client初始化公共请求参数。
 */
func CreateClient() (_result *eventbridge.Client, _err error) {
    config := &eventbridge.Config{}
    // 您的AccessKey ID。
    config.AccessKeyId = tea.String("<accessKeyId>")
    // 您的AccessKey Secret。
    config.AccessKeySecret = tea.String("<accessKeySecret>")
    // 您的接入点。
    config.Endpoint = tea.String("<endpoint>")
    _result = &eventbridge.Client{}
    _result, _err = eventbridge.NewClient(config)
    return _result, _err
}

func CreateEventBusSample(client *eventbridge.Client) (_err error) {
    tryErr := func() (_e error) {
        defer func() {
            if r := tea.Recover(recover()); r != nil {
                _e = r
            }
        }()
        createEventBusRequest := &eventbridge.CreateEventBusRequest{}
        createEventBusRequest.EventBusName = tea.String("demo-bus")
        response, _err := client.CreateEventBus(createEventBusRequest)
        if _err != nil {
            return _err
        }

        console.Log(tea.String("--------------------Create bus success --------------------"))
        console.Log(util.ToJSONString(tea.ToMap(response)))

        return nil
    }()

    _err = tryErr
    if tryErr != nil {
        var error = &tea.SDKError{}
        if _t, ok := tryErr.(*tea.SDKError); ok {
            error = _t
        } else {
            error.SetErrMsg(tryErr.Error())
        }
        console.Log(error.Message)
    }
    return _err
}

func main() {
    client, _err := CreateClient()
    if _err != nil {
        panic(_err)
    }

    _err = CreateEventBusSample(client)
    if _err != nil {
        panic(_err)
    }
}