FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, review the migration guide. Contact support@fauna.com with any questions.

Go

This section is intended to help Go developers get started using the Fauna Go driver for application development.

Current version and repository location

Current stable version

2.0.1

Repository

Supported Go versions

Currently, the driver is tested on the following Go versions:

  • 1.11

  • 1.12

  • 1.13

  • 1.14

Installation

To get the latest version run:

go get github.com/fauna/faunadb-go/v4/faunadb

Please note that the driver undergoes breaking changes from time to time. It is recommended to use one of the following methods instead:

Importing

For better usage, we recommend that you import this driver with an alias import.

Using dep or go get

To import a specific version when using dep or go get, use:

import f "github.com/fauna/faunadb-go/v4/faunadb"

Example application

The following example code runs a bare-bones Go application which creates a new document in a collection called People.

Prerequisites

  • A Fauna account. If you don’t have one, see the dashboard quick start for help getting set up.

  • A supported version of Go and the Fauna Go driver. See Installation for driver installation help.

Procedure

  1. Navigate to the Fauna Dashboard

    Log in to your Fauna account at Fauna Dashboard if you’re not already logged in.

  2. Create a new database

    Click NEW DATABASE and select your Region Group.

  3. Create a new collection

    Click NEW COLLECTION. Name your new collection People and save it.

  4. Create an access key

    Click SECURITY in the left-side navigation menu. Create a new key for your database. Be sure to save the key’s secret in a safe place, as it is only displayed once.

  5. Create a local environment variable with your access key’s secret

    On MacOS and Linux systems, enter the following in a terminal window:

    export FAUNADB_SECRET=<your-secret>

    For Windows systems, enter the following in a terminal window:

    set FAUNADB_SECRET=<your secret>

    For either example, replace <your secret> with the secret for the access key that you created.

  6. Create a local application file

    With your preferred editor, create a file called test.go with the following contents:

    package main
    
    import (
        "fmt"
        f "github.com/fauna/faunadb-go/v4/faunadb"
    	"os"
    )
    
    var (
    	secret = os.Getenv("FAUNADB_SECRET")
    	endpoint = os.Getenv("FAUNADB_ENDPOINT")
    )
    
    func main() {
    	if secret == "" {
    		fmt.Println("The FAUNADB_SECRET environment variable is not set, exiting.")
    		os.Exit(1)
    	}
    
    	if endpoint == "" {
    		endpoint = "https://db.fauna.com/"
    	}
    
    	client := f.NewFaunaClient(secret, f.Endpoint(endpoint))
    
    	result, err := client.Query(
            f.Create(
                f.Collection("People"),
                f.Obj{
                    "data": f.Obj{
                        "first": "Linus",
    				    "last": "Torvalds",
    				    "age": 52 }}))
    	
    	if err != nil {
    		fmt.Fprintln(os.Stderr, err)
    	} else {
    		fmt.Println(result)
    	}
    }

    The above example app uses the Create function to create a new document in the People collection.

  7. Run your application

    In a terminal window, run the application:

    go run test.go

    You should see results similar to this:

    map[data:map[age:52 first:Linus last:Torvalds] ref:{324878383054848512 0xc000200300 0xc000200300 <nil>} ts:1646087019880000]

    If you get an error, check to make sure your secret is correct.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!