Skip to content

Instantly share code, notes, and snippets.

@nicowernli
Created March 14, 2019 15:37
Show Gist options
  • Save nicowernli/7e56c13cba92b89dd03f84e9eb23dec5 to your computer and use it in GitHub Desktop.
Save nicowernli/7e56c13cba92b89dd03f84e9eb23dec5 to your computer and use it in GitHub Desktop.
// 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