Skip to main content

Go

Go client version

The current Go client version is v4.9.0.

The Weaviate Go client is compatible with Go 1.16+.

Installation

The client doesn't support the old Go modules system. Create a repository for your code before you import the Weaviate client.

Create a repository:

go mod init github.com/weaviate-go-client
go mod tidy

To get the latest stable version of the Go client library, run the following:

go get github.com/weaviate/weaviate-go-client/v4

Example

This example establishes a connection to your Weaviate instance and retrieves the schema.:

package main

import (
"context"
"fmt"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
)

func GetSchema() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client, err := weaviate.NewClient(cfg)
if err != nil {
panic(err)
}

schema, err := client.Schema().Getter().Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("%v", schema)
}

func main() {
GetSchema()
}

Authentication

For more comprehensive information on configuring authentication with Weaviate, refer to the authentication page.

The Go client offers multiple options for authenticating against Weaviate, including multiple OIDC authentication flows.

The suitable authentication options and methods for the client largely depend on the specific configuration of the Weaviate instance.

WCD authentication

WCD + Weaviate client

Each Weaviate instance in Weaviate Cloud (WCD) is pre-configured to act as a token issuer for OIDC authentication.

See our WCD authentication documentation for instructions on how to authenticate against WCD with your preferred Weaviate client.

API key authentication

Added in Weaviate Go client version 4.7.0.

If you use an API key to authenticate, instantiate the client like this:

cfg := weaviate.Config{
Host: "weaviate.example.com",
Scheme: "http",
AuthConfig: auth.ApiKey{Value: "my-secret-key"},
Headers: nil,
}
client, err := weaviate.NewClient(cfg)
if err != nil{
fmt.Println(err)
}

OIDC authentication

To authenticate against Weaviate with OIDC, you must select a flow made available by the identity provider and create the flow-specific authentication configuration.

This configuration will then be used by the Weaviate client to authenticate. The configuration includes secrets that help the client obtain an access token and, if configured, a refresh token.

The access token is added to the HTTP header of each request and is utilized for authentication with Weaviate. Typically, this token has a limited lifespan, and the refresh token can be employed to obtain a new set of tokens when necessary.

Resource Owner Password Flow

This OIDC flow uses the username and password to obtain required tokens for authentication.

Note that not every provider automatically includes a refresh token and an appropriate scope might be required that depends on your identity provider. The client uses offline_access as the default scope. This works with some providers, but as it depends on the configuration of the identity providers, we ask you to refer to the identity provider's documentation.

Without a refresh token, there is no possibility to acquire a new access token and the client becomes unauthenticated after expiration.

note

The Weaviate client does not save the username or password used.

They are only used to obtain the first tokens, after which existing tokens will be used to obtain subsequent tokens if possible.

cfg := weaviate.Config{
Host: "weaviate.example.com",
Scheme: "http",
AuthConfig: auth.ResourceOwnerPasswordFlow{
Username: "Your user",
Password: "Your password",
Scopes: []string{"offline_access"}, // optional, depends on the configuration of your identity provider (not required with WCD)
},
Headers: nil,
}
client, err := weaviate.NewClient(cfg)
if err != nil{
fmt.Println(err)
}

Client Credentials flow

This OIDC flow uses a client secret to obtain required tokens for authentication.

This flow is recommended for server-to-server communication without end-users and authenticates an application to Weaviate. This authentication flow is typically regarded as more secure than the resource owner password flow: a compromised client secret can be simply revoked, whereas a compromised password may have larger implications beyond the scope of breached authentication.

To authenticate a client secret most identity providers require a scope to be specified. This scope depends on the configuration of the identity providers, so we ask you to refer to the identity provider's documentation.

Most providers do not include a refresh token in their response so client secret is saved in the client to obtain a new access token on expiration of the existing one.

cfg := weaviate.Config{
Host: "weaviate.example.com",
Scheme: "http",
AuthConfig: auth.ClientCredentials{
ClientSecret: "your_client_secret",
Scopes: []string{"scope1 scope2"}, // optional, depends on the configuration of your identity provider (not required with WCD)
},
Headers: nil,
}
client, err := weaviate.NewClient(cfg)
if err != nil{
fmt.Println(err)
}

Refresh Token flow

Any other OIDC authentication method can be used to obtain tokens directly from your identity provider, for example by using this step-by-step guide of the hybrid flow.

If no refresh token is provided, there is no possibility to obtain a new access token and the client becomes unauthenticated after expiration.

cfg := weaviate.Config{
Host: "weaviate.example.com",
Scheme: "http",
AuthConfig: auth.BearerToken{
AccessToken: "some token",
RefreshToken: "other token",
ExpiresIn: uint(500), // in seconds
},
Headers: nil,
}
client, err := weaviate.NewClient(cfg)
if err != nil{
fmt.Println(err)
}

Custom headers

You can pass custom headers to the client, which are added at initialization:

cfg := weaviate.Config{
Host:"weaviate.example.com",
Scheme: "http",
AuthConfig: nil,
Headers: map[string]string{
"header_key1": "value",
"header_key2": "otherValue",
},
}
client, err := weaviate.NewClient(cfg)
if err != nil{
fmt.Println(err)
}

References

All RESTful endpoints and GraphQL functions references covered by the Go client, and explained on those reference pages in the code blocks.

Design

Builder pattern

The Go client functions are designed with a 'Builder pattern'. A pattern is used to build complex query objects. This means that a function (for example to retrieve data from Weaviate with a request similar to a RESTful GET request, or a more complex GraphQL query) is built with single objects to reduce complexity. Some builder objects are optional, others are required to perform specific functions. All is documented on the RESTful API reference pages and the GraphQL reference pages.

The code snippet above shows a simple query similar to RESTful GET /v1/schema. The client is initiated by requiring the package and connecting to the running instance. Then, a query is constructed by getting the .Schema with .Getter(). The query will be sent with the .Go() function, this object is thus required for every function you want to build and execute.

Migration Guides

From v2 to v4

Unnecessary .Objects() removed from GraphQL.Get()

Before:

client.GraphQL().Get().Objects().WithClassName...

After:

client.GraphQL().Get().WithClassName

GraphQL Get().WithNearVector() uses a builder pattern

In v2 specifying a nearVector argument to client.GraphQL().Get() required passing a string. As a result the user had to know the structure of the GraphQL API. v4 fixes this by using a builder pattern like so:

Before:

client.GraphQL().Get().
WithNearVector("{vector: [0.1, -0.2, 0.3]}")...

After

nearVector := client.GraphQL().NearVectorArgBuilder().
WithVector([]float32{0.1, -0.2, 0.3})

client.GraphQL().Get().
WithNearVector(nearVector)...

All where filters use the same builder

In v2 filters were sometimes specified as strings, sometimes in a structured way. v4 unifies this and makes sure that you can always use the same builder pattern.

GraphQL Get

Before:

// using filter encoded as string
where := `where :{
operator: Equal
path: ["id"]
valueText: "5b6a08ba-1d46-43aa-89cc-8b070790c6f2"
}`

client.GraphQL().Get().
Objects().
WithWhere(where)...
// using deprecated graphql arg builder
where := client.GraphQL().WhereArgBuilder().
WithOperator(graphql.Equal).
WithPath([]string{"id"}).
WithValueString("5b6a08ba-1d46-43aa-89cc-8b070790c6f2")

client.GraphQL().Get().
Objects().
WithWhere(where)...

After:

where := filters.Where().
WithPath([]string{"id"}).
WithOperator(filters.Equal).
WithValueString("5b6a08ba-1d46-43aa-89cc-8b070790c6f2")

client.GraphQL().Get().
WithWhere(where)...
GraphQL Aggregate

Before:

where := client.GraphQL().WhereArgBuilder().
WithPath([]string{"id"}).
WithOperator(graphql.Equal).
WithValueString("5b6a08ba-1d46-43aa-89cc-8b070790c6f2")

client.GraphQL().Aggregate().
Objects().
WithWhere(where)...

After:

where := filters.Where().
WithPath([]string{"id"}).
WithOperator(filters.Equal).
WithValueString("5b6a08ba-1d46-43aa-89cc-8b070790c6f2")

client.GraphQL().Aggregate().
WithWhere(where)...
Classification

Before:

valueInt := 100
valueText := "Government"

sourceWhere := &models.WhereFilter{
ValueInt: &valueInt,
Operator: string(graphql.GreaterThan),
Path: []string{"wordCount"},
}

targetWhere := &models.WhereFilter{
ValueString: &valueText,
Operator: string(graphql.NotEqual),
Path: []string{"name"},
}

client.Classifications().Scheduler().
WithSourceWhereFilter(sourceWhere).
WithTargetWhereFilter(targetWhere)...

After:

sourceWhere := filters.Where().
WithOperator(filters.GreaterThan).
WithPath([]string{"wordCount"}).
WithValueInt(100)

targetWhere := filters.Where().
WithOperator(filters.NotEqual).
WithPath([]string{"name"}).
WithValueString("Government")

client.Classifications().Scheduler().
WithSourceWhereFilter(sourceWhere).
WithTargetWhereFilter(targetWhere)...

GraphQL Get().WithFields()

In v2 .WithFields() took a GraphQL string that required knowledge of how GraphQL fields are structured. Now this can be done with a variadic function. E.g:

Before:

client.GraphQL.Get().WithClassName("MyClass").WithFields("name price age")...

After:

client.GraphQL.Get().WithClassName("MyClass").
WithFields(graphql.Field{Name: "name"},graphql.Field{Name: "price"}, graphql.Field{Name: "age"})...

Graphql Get().WithGroup()

In v2 .WithFields() took a GraphQL string that required knowledge of how GraphQL fields are structured. Now this can be done with a builder. E.g:

Before:

client.GraphQL.Get().WithClassName("MyClass")
.WithGroup("{type:merge force:1.0}")

After:

group := client.GraphQL().GroupArgBuilder()
.WithType(graphql.Merge).WithForce(1.0)

client.GraphQL.Get().WithClassName("MyClass").WithGroup(group)

Graphql Data().Validator() property renamed

In v2 the naming of the method to specify the Schema was inconsistent with other places in the client. This has been fixed in v4. Rename according to the following:

Before:

client.Data().Validator().WithSchema(properties)

After:

client.Data().Validator().WithProperties(properties)

Releases

For links to the Go Client releases, expand this section.

Go Client
Client VersionRelease Date
4.15.12024-08-07
4.15.02024-07-25
4.14.32024-07-25
4.14.22024-07-17
4.14.12024-07-11
4.14.02024-05-20
4.13.12024-03-07
4.13.02024-03-05
4.12.02023-12-08
4.11.02023-11-13
4.10.02023-08-22
4.9.02023-07-06
4.8.12023-05-26
4.8.02023-05-05
4.7.12023-04-14
4.7.02023-04-03
4.6.42023-03-25
4.6.32023-03-14
4.6.22023-03-07
4.6.12023-02-01
4.6.02023-02-01
4.5.22023-01-19
4.5.12023-01-17
4.5.02022-12-20
4.4.02022-10-31
4.3.12022-09-27
4.3.02022-09-07
4.2.12022-07-12
4.2.02022-07-07
4.1.02022-05-25
4.0.02022-05-03
3.0.02022-04-05
2.6.22022-10-31
2.6.12022-05-04
2.6.02022-03-11
2.5.02022-02-01
2.4.12021-12-15
2.4.02021-11-30
2.3.02021-08-31
2.2.02021-06-07
2.1.02021-04-23
2.0.12021-03-25
2.0.02021-03-23
1.1.22021-10-31
1.1.12021-05-04
1.1.02020-11-09
1.0.02020-11-06

For links to the client releases associated with the latest Weaviate core releases, expand this section.

Current core and client releases
Weaviate VersionRelease DatePythonTypeScriptGoJava
1.26.52024-09-274.8.13.1.44.15.14.8.3
1.26.42024-09-064.7.1''''4.8.2
1.26.32024-08-29''''''''
1.26.22024-08-29''''''''
1.26.12024-07-184.6.63.0.84.14.24.8.0
1.26.02024-07-18''''''4.7.0

For links to previous releases, expand this section.

Previous core and client releases
Weaviate VersionRelease DatePythonTypeScriptGoJava
1.25.02024-05-104.6.02.1.14.13.14.6.0
1.24.02024-02-274.5.02.1.04.12.04.5.1
1.23.02023-12-183.26.02.0.0''4.4.2
1.22.02023-10-273.25.0''4.10.04.3.0
1.21.02023-08-173.22.11.4.04.9.04.2.1
1.20.02023-07-063.22.0''''4.2.0
1.19.02023-05-043.17.01.1.0]4.7.14.0.1
1.18.02023-03-073.13.02.14.54.6.23.6.4
1.17.02022-12-203.9.02.14.04.5.03.5.0
1.16.02022-10-313.8.02.13.04.4.03.4.0
1.15.02022-09-07''2.12.04.3.03.3.0
1.14.02022-07-073.6.02.11.04.2.03.2.0
1.13.02022-05-033.4.22.9.04.0.02.4.0
1.12.02022-04-053.4.02.8.03.0.0''
1.11.02022-03-143.2.52.7.02.6.02.3.0
1.10.02022-01-27''2.5.02.4.12.1.1
1.9.02021-12-10''''2.4.02.1.0
1.8.02021-11-30''''''''
1.7.02021-09-013.1.12.4.02.3.01.1.0
1.6.02021-08-112.4.02.3.02.2.0''
1.5.02021-07-13''''''''
1.4.02021-06-09''''''''
1.3.02021-04-23''2.1.02.1.01.0.0
1.2.02021-03-152.2.02.0.01.1.0-
1.1.02021-02-102.1.0''''-
1.0.02021-01-142.0.0''''-

Note: The TypeScript client replaced the JavaScript client on 2023-03-17.

Change logs

Check the change logs on GitHub for updates on the latest Go client changes.

Questions and feedback

If you have any questions or feedback, let us know in the user forum.