Created
March 14, 2019 15:37
-
-
Save nicowernli/7e56c13cba92b89dd03f84e9eb23dec5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GetItem finds a returns a single item from a dynamodb table. | |
// It returns an element and an error if any. | |
// If no element is found, will return nil without an error | |
func GetItem(id string, dest interface{}) error { | |
tableName := "table-name" | |
// create get operation information | |
item := &dynamodb.GetItemInput{ | |
TableName: aws.String(tableName), | |
Key: map[string]*dynamodb.AttributeValue{ | |
"ID": {S: aws.String(id)}, | |
}, | |
} | |
// execute GetItem operation | |
result, err := DynamoDB().GetItem(item) | |
if err != nil { | |
return err | |
} | |
return dynamodbattribute.UnmarshalMap(result.Item, &dest) | |
} | |
type Product struct { | |
ID string `json:"id"` | |
Name string `json:"name"` | |
} | |
func main() err { | |
p := Product{} | |
if err := GetItem("p-id", &p); err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment