Instantiate a client object (Go)
All Data API interactions through a client start with a DataAPIClient object.
Once you have a DataAPIClient object, you can connect to a database and the perform CRUD operations on your data.
Creating a client object is an essential component of Data API client scripts. For more information, see Get started with the Data API (Go).
For more information about the Data API client hierarchy, see Python client internals, TypeScript client internals, Go client internals, Java client internals, and C# client internals.
Result
Returns a DataAPIClient struct that you can use to interact with the Data API.
Parameters
Use the NewClient method.
Method signature
func NewClient(
opts ...options.APIOption
) *DataAPIClient
| Name | Type | Summary |
|---|---|---|
|
Optional. A specification of options to override the inherited defaults. Use this to customize the interaction of the client with the admin object. For example, you can change the default timeouts or token. You can override these options later with See Methods of the |
| Method | Summary |
|---|---|
|
Optional.
An application token for a database, in the form of an The token should have sufficient permissions to perform the desired operations. |
|
Optional. Specify the environment this client will work in. Default: |
|
Optional. The maximum time that the client should wait for an entire operation, which may involve multiple HTTP requests, to complete. Default: 30 seconds |
|
Optional. The maximum time that the client should wait for individual HTTP requests. Default: 15 seconds |
|
Optional.
The maximum time that the client should wait for database administrative operations like Default: 10 minutes |
|
Optional.
The maximum time that the client should wait for keyspace administrative operations like Default: 30 seconds |
|
Optional.
The maximum time that the client should wait for collection administrative operations like Default: 60 seconds |
|
Optional.
The maximum time that the client should wait for table administrative operations like Default: 30 seconds |
|
Optional.
An alternative to |
|
Optional. Sets the database backend. Default: |
|
Optional The keyspace to use for downstream operations. Default: default_keyspace |
|
Optional. Specifies headers for all HTTP requests to the Data API. |
|
Optional. Attaches special identifying entries to the User-Agent header for all HTTP requests to the Data API. |
|
Sets the Data API version. Default: v1 |
|
Optional. Sets the HTTP client to use for requests. |
|
Optional. Specifies a callback function that is invoked for warnings emitted by the Data API. Data API warnings indicate non-fatal conditions that don’t prevent the operation from completing, such as missing indexes or deprecated features. |
|
Optional. Sets the serialization/deserialization options. |
Examples
The following examples demonstrate how to instantiate a client.
Instantiate a client
package main
import (
"github.com/datastax/astra-db-go/v2/astra"
)
func main() {
_ = astra.NewClient()
}
Instantiate a client and pass a token
package main
import (
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
_ = astra.NewClient(options.API().SetToken("APPLICATION_TOKEN"))
}
Instantiate a client and specify options
package main
import (
"time"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
_ = astra.NewClient(
options.API().
SetRequestTimeout(10 * time.Second).
SetGeneralMethodTimeout(20 * time.Second).
SetKeyspace("example_keyspace"),
)
}
Client reference
For more information, see the client reference.