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

4.3.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.

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

Procedure

  1. Navigate to the Fauna v4 Dashboard

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

  2. Create a new database

    Create a new database and select your Region Group.

  3. Access the v4 Dashboard

    Access the Fauna v4 Dashboard by clicking the v4 Dashboard tab for the database.

  4. Create a new collection

    Under Collections, click NEW COLLECTION. Name your new collection People and save it.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

\