GoGo driver

This section describes Fauna’s open source Go driver, which provides the resources required to interact with Fauna.

Supported Go versions

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

  • 1.11

  • 1.12

  • 1.13

Install

To get the latest version run:

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

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

Using gopkg.in

To get a specific version when using gopkg.in, use:

go get gopkg.in/fauna/faunadb-go.v2/faunadb

Using dep

To get a specific version when using dep, use:

dep ensure -add github.com/fauna/faunadb-go/faunadb@v2.12.0

Importing

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

Using gopkg.in

To import a specific version when using gopkg.in, use:

import f "gopkg.in/fauna/faunadb-go.v2/faunadb"

Using dep or go get

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

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

Usage

Here is an example demonstrating how to use the Go driver to execute a simple query on Fauna:

package main

import (
    "fmt"

    f "github.com/fauna/faunadb-go/faunadb"
)

type User struct {
    Name string `fauna:"name"`
}

func main() {
    client := f.NewFaunaClient("your-secret-here")

    res, err := client.Query(f.Get(f.RefClass(f.Collection("user"), "42")))
    if err != nil {
        panic(err)
    }

    var user User

    if err := res.At(f.ObjKey("data")).Get(&user); err != nil {
        panic(err)
    }

    fmt.Println(user)
}

Query timeout

Your code can configure the client to handle timeouts in two different ways:

  1. by adding a call to QueryTimeoutMS to the options block when instantiating the client, or

  2. by adding a call to TimeoutMS as the second parameter to the .Query method.

When you provide a query timeout, the server waits for the specified period before timing out, if it has yet to complete the current query. Once the period has elapsed, the query fails and the server responds with an error.

Both timeouts are expressed in milliseconds.

For example:

// Specify a query timeout during client instantiation
client := f.NewFaunaClient(
	"secret",
	f.QueryTimeoutMS(1))
// Specify a query timeout per query
result, err := client.Query(
	 f.Paginate(f.Collections()),
	 f.TimeoutMS(1))

The value used in the TimeoutMS call passed to .Query() takes precedence over any value used in the QueryTimeoutMS call during client instantiation.

Next steps

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!