我的樂團即將演出的節目儲存在 DynamoDB 中,並且我有以下程式碼:
type PartiQLRunner struct { DynamoDbClient *dynamodb.DynamoDB TableName string } func BuildRunner() *PartiQLRunner { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := dynamodb.New(sess) return &PartiQLRunner{ DynamoDbClient: svc, TableName: "SHOWS", } } type Show struct { PK int `dynamodbav:"PK"` DATE string `dynamodbav:"DATE"` ADDRESS string `dynamodbav:"ADDRESS"` VENUE string `dynamodbav:"VENUE"` } func (runner PartiQLRunner) GetShows() ([]Show, error) { var shows []Show response, err := runner.DynamoDbClient.ExecuteStatement( &dynamodb.ExecuteStatementInput{ Statement: aws.String(fmt.Sprintf("SELECT * FROM \"%v\" WHERE PK = 1", runner.TableName)), }) if err != nil { log.Printf("Couldn't get info. Here's why: %v\n", err) } else { err = attributevalue.UnmarshalListOfMaps(response.Items, &shows) if err != nil { log.Printf("Couldn't unmarshal response. Here's why: %v\n", err) } } return shows, err }
但是,我在 UnmarshalListOfMaps()
中的 response.Items
參數中收到以下錯誤:
無法使用「response.Items」(型別[]map[string]*AttributeValue)作為型別[]map[string]types.AttributeValue
我對 Go 語法仍然有些陌生,我不確定我是否理解傳入內容與預期內容之間的不匹配。任何幫助將不勝感激。
似乎 UnmarshalListOfMaps
函數期望的內容與您提供的內容之間存在類型不符。這個錯誤訊息表示該函數需要[]map[string]types.AttributeValue
類型的參數,但response.Items
的類型為[]map[string]*AttributeValue
。
解決方案是將 response.Items
轉換為正確的類型或為您的切片使用正確的類型簽署。
解決此問題的方法如下:
package main import ( "fmt" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/dynamodb" "github.com/aws/aws-sdk-go-v2/service/dynamodb/attributevalue" "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" ) type Show struct { PK int `dynamodbav:"PK"` DATE string `dynamodbav:"DATE"` ADDRESS string `dynamodbav:"ADDRESS"` VENUE string `dynamodbav:"VENUE"` } type PartiQLRunner struct { DynamoDbClient *dynamodb.Client TableName string } func (runner PartiQLRunner) GetShows() ([]*Show, error) { var shows []*Show response, err := runner.DynamoDbClient.ExecuteStatement( &dynamodb.ExecuteStatementInput{ Statement: aws.String(fmt.Sprintf("SELECT * FROM \"%v\" WHERE PK = 1", runner.TableName)), }) if err != nil { log.Printf("Couldn't get info. Here's why: %v\n", err) return shows, err } err = attributevalue.UnmarshalListOfMaps(response.Items, &shows) if err != nil { log.Printf("Couldn't unmarshal response. Here's why: %v\n", err) return shows, err } return shows, nil } // The main function is just for demonstration and may not be part of your code func main() { // You will need to initialize your DynamoDbClient and TableName runner := PartiQLRunner{ DynamoDbClient: /* initialize your DynamoDB client here */, TableName: "YourTableName", } shows, err := runner.GetShows() if err != nil { log.Fatalf("Error retrieving shows: %v", err) } for _, show := range shows { fmt.Printf("Show Date: %s, Venue: %s, Address: %s\n", show.DATE, show.VENUE, show.ADDRESS) } }
請確保您具有適當的 DynamoDB 用戶端初始化邏輯(在 main
函數中註解掉)來建立與 DynamoDB 的連線。
所提供的程式碼現在可以正確地將回應解組為指向 Show
結構的指標片段。
以上是如何使用 AWS sdk 在 Go 中正確解組 PartiQL 查詢的結果?的詳細內容。更多資訊請關注PHP中文網其他相關文章!