---
title: Create a user context using the Go SDK
slug: feature-experimentation/docs/create-user-context-go
docTags: 
createdAt: 2026-08-05T20:40:01.000Z
---

Create a user for which you can make flag decisions and track events. The user context is returned as a runtime object that is not otherwise persisted. The purpose of this method is to set a user context once, so you do not have to specify the user each time you make a flag decision or track an event. You can define multiple user contexts.

## Version

SDK v1.6.0 and higher

## Description

This call creates a user context for flag decisions and events. You can call this method on the Optimizely client instance, even when the instance is not fully configured yet.

## Parameters

The following table lists the required and optional parameters:

| Parameter                    | Type   | Description                                                                                                                                          |
| ---------------------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| **userId** \*required\*      | string | The ID of the user.                                                                                                                                  |
| **attributes**\ \*optional\* | map    | A map of custom key-value pairs specifying attributes for the user that are used for audience targeting. See the following section for more details. |

## Audience attributes

You can set custom audience attributes for the user, which you can then use to [Target audiences](docId\:UDBvr5a8WHNkyOqzPhOr9).  You can pass strings, numbers, Booleans, and null as custom user attribute values. If you want to target audiences based on the version of your application that they are using, you can also pass in a string formatted as a [semantic version](https://semver.org/), then define a `version`  audience condition in the Optimizely app.

:::hint{type="warning"}
**Important**
During audience evaluation, if you do not pass a valid attribute value for a given audience condition, for example, if you pass a string when the audience condition requires a Boolean, or if you forget to pass a value, then that condition is skipped. The [SDK logs](docId:8Ita8V20aE9N-xe9oJvkI) will include warnings when this occurs.
:::

## Returns

Returns an OptimizelyUserContext object. For details, see [OptimizelyUserContext](docId\:YD23vbbeambJxxmSEXUo8).

## Example

```go
import "github.com/optimizely/go-sdk/pkg/client" // for v2: "github.com/optimizely/go-sdk/v2/pkg/client"

optimizelyFactory := client.OptimizelyFactory{SDKKey: "sdk-key"}
client, err := optimizelyFactory.StaticClient()
if err != nil {
 // handle error
}
// Option 1 – Create a user, then set attributes.
user := client.CreateUserContext("user123", nil)
user.SetAttribute("is_logged_in", false)
user.SetAttribute("app_version", "1.3.2")

// Option 2 – Pass attributes when creating the user.
attributes := map[string]interface{}{
	"device":       "iPhone",
  "lifetime":     24738388,
  "is_logged_in": true,
  "app_version":  "4.3.0-beta",
}
user = client.CreateUserContext("user123", attributes)
```

## See Also

[OptimizelyUserContext](docId\:YD23vbbeambJxxmSEXUo8)

## Source files

The language and platform source files containing the implementation for Go are available on [GitHub](https://github.com/optimizely/go-sdk/blob/master/pkg/client/client.go).
