Quickstart
The Go SDK for SurrealDB makes it straightforward to connect to your instance and start querying data. This guide walks you through connecting, authenticating, and performing basic operations.
1. Install the SDK
Follow the installation guide to add the SDK to your project. Once installed, import the SDK and its models package.
The surrealdb package contains the client, query functions, and authentication types. The models package contains value types like RecordID and Table that map to SurrealDB's data model.
2. Connect to SurrealDB
Use FromEndpointURLString to create a new client and connect to a SurrealDB instance. The function accepts a context.Context for cancellation and a URL string that determines the connection type.
Supported connection protocols include:
WebSocket (
ws://,wss://) for long-lived stateful connections, required for live queries, sessions, and transactionsHTTP (
http://,https://) for short-lived stateless connectionsMemory (
mem://) for embedded instances
After connecting, use .Use() to select the namespace and database you want to work with, and .SignIn() to authenticate. Most operations require both.
The defer db.Close(ctx) ensures the connection is cleaned up when your function returns, similar to closing a file or database connection in Go.
3. Inserting data into SurrealDB
Once connected, you can use the Create function to create records. Pass a Table to generate a random ID, or a RecordID to specify the ID explicitly. The data can be a struct or a map.
You can also create a record with a specific ID using RecordID:
Notice that the Go SDK uses generic functions like surrealdb.Create[User](...) rather than methods on the db object. The type parameter tells the SDK what type to unmarshal the response into.
4. Retrieving data from SurrealDB
Selecting records
The Select function retrieves all records from a table, or a single record by its RecordID. Use a slice type parameter when selecting a table, and a single type when selecting a specific record.
Running SurrealQL queries
For more advanced use cases, you can use the Query function to run SurrealQL statements directly. Use parameters to safely pass dynamic values.
5. Closing the connection
Always close the connection when you are done to release resources. The idiomatic Go pattern is to defer the close immediately after creating the connection, as shown in step 2.
What's next?
You have learned how to install the SDK, connect to SurrealDB, create records, and retrieve data. There is a lot more you can do with the SDK, including updating and deleting records, live queries, sessions, and interactive transactions.
Connection management
Learn how to manage your database connections, including protocols and configuration.
Authentication
Read more about authentication levels and how to integrate them into your application.
Data manipulation
Learn how to create, read, update, and delete records using the SDK.
API Reference
Complete reference for all functions, methods, types, and errors.