是否可以将输出更改为实际的字符串?

我创建了这个函数来输出跨区域的 aws 账户的所有账户 ID,但我得到的输出非常难以理解


尝试像 c++ 中那样取消引用


package main

import (

    "fmt"


    //"github.com/aws/aws-lambda-go/lambda"

   // "github.com/aws/aws-sdk-go/aws"

    "github.com/aws/aws-sdk-go/aws/awserr"

    //"github.com/aws/aws-sdk-go/aws/credentials/stscreds"

    "github.com/aws/aws-sdk-go/aws/session"

    "github.com/aws/aws-sdk-go/service/organizations"

)


func main()  {

    listAccounts()

}


func listAccounts() {

    sess := session.Must(session.NewSession())

    svc := organizations.New(sess)

    input := &organizations.ListAccountsInput{}



    result, err := svc.ListAccounts(input)

    if err != nil {

        if aerr, ok := err.(awserr.Error); ok {

            switch aerr.Code() {

            case organizations.ErrCodeAccessDeniedException:

                fmt.Println(organizations.ErrCodeAccessDeniedException, aerr.Error())

            case organizations.ErrCodeAWSOrganizationsNotInUseException:

                fmt.Println(organizations.ErrCodeAWSOrganizationsNotInUseException, aerr.Error())

            case organizations.ErrCodeInvalidInputException:

                fmt.Println(organizations.ErrCodeInvalidInputException, aerr.Error())

            case organizations.ErrCodeServiceException:

                fmt.Println(organizations.ErrCodeServiceException, aerr.Error())

            case organizations.ErrCodeTooManyRequestsException:

                fmt.Println(organizations.ErrCodeTooManyRequestsException, aerr.Error())

            default:

                fmt.Println(aerr.Error())

            }

        } else {

        // Print the error, cast err to awserr.Error to get the Code and

        // Message from an error.

        fmt.Println(err.Error())

        }

    return

}

    // fmt.Println(result.Accounts)

    var accountList []*string


    for _, accountId := range result.Accounts {

        accountList = append(accountList, accountId.Id)

    }

    fmt.Println(accountList)


}


芜湖不芜
浏览 132回答 1
1回答

FFIVE

*string当你真正只需要s 时,你却在服用s string。这是一个简单的更改,可以取消引用从 AWS SDK 返回的指针(它对所有内容都使用指针以实现可空性):var accountList []stringfor _, accountId := range result.Accounts {    accountList = append(accountList, *accountId.Id)}fmt.Println(accountList)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go