Get a collection (Go)
Gets a reference to a collection for use with the Data API clients.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Returns a Collection object that corresponds to the specified collection name.
This method returns a Collection object even for collections that don’t exist.
Parameters
Use the Collection method, which belongs to the Db type.
Method signature
func (d *Db) Collection(
name string,
opts ...options.GetCollectionOption
) *Collection
| Name | Type | Summary |
|---|---|---|
|
|
The name of the collection to get. |
|
|
Optional. The options for this method. See Methods of the |
| Method | Summary |
|---|---|
|
Optional if you specified a working keyspace when you created the Default: The working keyspace set when you created the |
|
Optional. General API options for this operation, including the timeout. |
Examples
The following examples demonstrate how to get a collection.
Get a collection
package main
import (
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
// Get a database
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
// Get a collection
database.Collection("COLLECTION_NAME")
}
Client reference
For more information, see the client reference.