O'Reilly logo

Go Programming Blueprints - Second Edition by Mat Ryer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Casting a vote

Before our API is a complete feature, we need to add the ability for users to cast votes. We'll break this piece into two functions in order to increase the readability of our code.

Inside votes.go, add the following function:

func CastVote(ctx context.Context, answerKey *datastore.Key, score int) (*Vote, error) { question, err := GetQuestion(ctx, answerKey.Parent()) if err != nil { return nil, err } user, err := UserFromAEUser(ctx) if err != nil { return nil, err } var vote Vote err = datastore.RunInTransaction(ctx, func(ctx context.Context) error { var err error vote, err = castVoteInTransaction(ctx, answerKey, question, user, score) if err != nil { return err } return nil }, &datastore.TransactionOptions{XG: true}) if err != nil ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required