我創建了這個函數來輸出跨區域的 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 關注
- 166 瀏覽
添加回答
舉報
0/150
提交
取消