Skip to content

Go

Go Reference

Our API Reference has generic examples in every supported language, and we strive to make the experience of each SDK very similar. However, there are some things specific to the Go SDK that we want to call out here.

Key Path Helper

The github.com/StatelyCloud/go-sdk/stately.ToKeyID(value) function can be used to format an ID value (especially a UUID) correctly to include in a key path:

kp := "/movie-"+stately.ToKeyID(movieID)+"/actor-"+stately.ToKeyID(actorID)

The ToKeyID helper has generic support for types string, []byte, [16]byte, uint64, uint32, int64, and uint64. When using a typed version of these it may be necessary to convert to one of these types first. See examples below:

A typed primitive:

type UserID uint64
userID := UserID(123)
kp := "/userID-"+stately.ToKeyID(uint64(userID))
type EmailAddress string
email := EmailAddress("[email protected]")
kp := "/email-"+stately.ToKeyID(string(email))

github.com/gofrs/uuid/v5 UUID:

moveID := uuid.Must(uuid.NewV4())
kp := "/movie-"+stately.ToKeyID(id[:])
kp := "/movie-"+stately.ToKeyID(id.Bytes())
kp := "/movie-"+stately.ToKeyID([16]byte(id))

github.com/google/uuid UUID:

moveID := uuid.New()
kp := "/movie-"+stately.ToKeyID(id[:])
kp := "/movie-"+stately.ToKeyID([16]byte(id))

Checking an Item’s Type

Many client APIs return a *stately.Item, but you want to know exactly what type it is. You can use standard Go type checks for this:

if movie, ok := item.(*schema.Movie); ok {
// it's a movie
}
switch v := item.(type) {
case *schema.Movie:
// it's a movie
case *schema.Character:
// it's a character
}

Protobuf

The Go types generated from your schema are actually google.golang.org/protobuf objects. They expose each of their fields as properties, but also have a method version that is nil-safe. For example if you have a name field, the Go object will have a Name property and a GetName() method. The method version will return an empty string even if called on a nil pointer.

UUIDs

UUIDs are represented as []byte of length 16. You can use the github.com/gofrs/uuid/v5 or github.com/google/uuid package to convert them to and from strings.