A models.Set holds the values of a SurrealDB set, a collection of distinct values that the server keeps sorted. This page documents the type and how a set converts.
Package: github.com/surrealdb/surrealdb.go/v2/pkg/models
Source: pkg/models/set.go
Definition
type Set []anyConstructor
NewSet
Returns a Set of the given values. The result is never nil, so NewSet() returns an empty set.
s := models.NewSet(values...)Returns: Set
How sets convert
The SDK sends a Set as a SurrealDB set. The server sorts the values and removes duplicates, so you do not need to sort a Set or remove its duplicates before you send it:
results, err := surrealdb.Query[models.Set](ctx, db, "RETURN $tags", map[string]any{
"tags": models.NewSet("go", "db", "go"),
})
fmt.Println((*results)[0].Result)[db go]A set decodes into any as a Set, rather than a []any, so a set that you read and write back stays a set. A set also decodes into a slice of a concrete type, such as a []string field:
type Post struct {
Tags []string `json:"tags"`
}The SDK sends a []string or []any as an array, not as a set. To write a set from a slice, wrap the slice in a Set. On SurrealDB 4.0, a field defined as set<string> also turns an array into a set. SurrealDB 3.x refuses an array for such a field.
A nil set is NULL
The SDK sends a nil Set as NULL, as it does any nil slice, and not as an empty set. Use models.NewSet() or models.Set{} for an empty set, and a *Set or omitempty for a field that may be absent.
See also
Value types for how values convert
Sets for the SurrealQL kind