Go driver
This section describes Fauna’s open source Go driver, which provides the resources required to interact with Fauna.
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:
Importing
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:
-
by adding a call to
QueryTimeoutMS
to theoptions
block when instantiating the client, or -
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
-
Driver repository: https://github.com/fauna/faunadb-go
-
Driver-specific reference documentation is hosted at GoDoc.
-
For more information about the Fauna Query Language, consult our query language reference documentation.
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!