我创建了这个函数来输出跨区域的 aws 账户的所有账户 ID,但我得到的输出非常难以理解尝试像 c++ 中那样取消引用package mainimport ( "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)}
1 回答
FFIVE
TA贡献1797条经验 获得超6个赞
*string当你真正只需要s 时,你却在服用s string。这是一个简单的更改,可以取消引用从 AWS SDK 返回的指针(它对所有内容都使用指针以实现可空性):
var accountList []string
for _, accountId := range result.Accounts {
accountList = append(accountList, *accountId.Id)
}
fmt.Println(accountList)
- 1 回答
- 0 关注
- 131 浏览
添加回答
举报
0/150
提交
取消