> Full SurrealDB documentation index: https://surrealdb.com/docs/llms.txt

# Set

models.Set in the Go SDK for the SurrealDB set kind: a collection of distinct values that the server keeps sorted, NewSet, decoding a set into any or into a typed slice, writing a set back, and why a nil Set is NULL.

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](https://github.com/surrealdb/surrealdb.go/blob/main/pkg/models/set.go)

---

## Definition {#definition}

```go
type Set []any
```

---

## Constructor {#constructor}

### `NewSet` {#newset}

Returns a `Set` of the given values. The result is never `nil`, so `NewSet()` returns an empty set.

```go title="Syntax"
s := models.NewSet(values...)
```

**Returns:** `Set`

---

## How sets convert {#conversion}

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:

```go
results, err := surrealdb.Query[models.Set](ctx, db, "RETURN $tags", map[string]any{
	"tags": models.NewSet("go", "db", "go"),
})
fmt.Println((*results)[0].Result)
```

```text title="Output"
[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:

```go
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 {#nil}

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](/docs/reference/golang/concepts/value-types.md#sets) for how values convert
- [Sets](/docs/reference/query-language/language-primitives/data-types/sets.md) for the SurrealQL kind
