登录
首页 >  Golang >  Go问答

可以将输出内容更改为相应的字符串吗?

来源:stackoverflow

时间:2024-02-26 20:30:21 306浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《可以将输出内容更改为相应的字符串吗?》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我创建了这个函数来输出跨区域的 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)

}

运行 id.go [0xc0002387e0 0xc000238840 0xc0002388a0 0xc000238900 0xc000238960 0xc0002389c0 0xc000238a20 0xc000238a80 0xc000238ae0 0xc0002 38b40 0xc000238ba0 0xc000238c00 0xc000238c60 0xc000238cc0 0xc000238d20 0xc000238d80 0xc000238de0 0xc000238e40 0xc000238ea0 0xc000 238f00]


解决方案


当您实际上只需要 strings 时,您正在使用 *strings。这是一个简单的更改,可以取消引用从 aws sdk 返回的指针(它对所有内容都使用指针以实现可空性):

var accountList []string

for _, accountId := range result.Accounts {
    accountList = append(accountList, *accountId.Id)
}
fmt.Println(accountList)

今天关于《可以将输出内容更改为相应的字符串吗?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>