Go
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 uint64userID := UserID(123)kp := "/userID-"+stately.ToKeyID(uint64(userID))
type EmailAddress stringkp := "/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 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 the zero value (in this case an empty string) even if called on a nil pointer.
UUIDs
The Go code generator will automatically choose which package to use for UUID-typed fields based on what is availale in the go.mod
file in the output directory. This allows you to use you preferred UUID package with the generated code. The generator can currently detect and use github.com/google/uuid
, [github.com/gofrs/uuid
], or github.com/satori/go.uuid
. If you don’t already have a UUID package specified, it defaults to github.com/google/uuid
.