Nov 18, 2016


Blog Categories

Categories

Blog Categories

Introducing Support for the Go Programming Language

A high priority for the team at Fauna is making developers’ lives easier. Each job requires the right programming language, so we develop and fully support drivers in a variety of languages, including Java, Javascript, Ruby, Scala, Python, and C#. Today we are proud to announce the addition of the Go programming language to our arsenal.

Our new driver makes it easy for developers working in the Go programming language to leverage all of the features of FaunaDB. If you are already familiar with using FaunaDB in another language, the Go driver will be immediately familiar and you will feel right at home coding against FaunaDB in Go.

A quick note: To use these drivers with FaunaDB Cloud, you will need to join the managed beta program.

The rest of this blog post is a tour of essential FaunaDB operations exposed by the Go driver. The fun ones are presented first, with client setup at the end, so look here for an executable version of this code.

Basic Queries

Here is an example of building and executing a FaunaDB query in Go, using a domain specific language designed to be friendly to Go users and also similar to FaunaDB APIs in other languages. Note that we alias our package name to f, so that queries are readable while still being expressed as standard Go.

import (
    f "github.com/faunadb/faunadb-go/faunadb"
)
// skipping setup client code, see the last code block of this article...
instance, _ := client.Query(
    f.Get(
        f.MatchTerm(
            f.Index("pet_by_name"),
            "bob the cat",
        ),
    ),
)

Tagged Structs in Go

The Go driver supports custom data structures via struct tags to control how your data is saved and loaded from FaunaDB. This makes it easy to populate objects for use by your application. You can mark the fields and let the driver decode from Fauna values to your own type. Here is the application data structure for pets in our example.

type Pet struct {
    Name string `fauna:"name"`
    Age  int    `fauna:"age"`
}

You can let the driver decode the result of the query above into an instance of your Pet struct right away.

var myPet Pet
instance.Get(&myPet)

Encoding data structures to FaunaDB is also very simple, below you can see how a custom data struct is used in the create query.

bob := Pet {
    Name: "bob the cat",
    Age:  5,
}
client.Query(
    f.Create(
        f.Class("pets"),
        f.Obj{"data": bob},
    ),
)

Setting Up the Client

Before we can do any of the fun stuff we just read about, we have to set up a connection and create the schema. Here the admin key creates a database and then a server access key for data operations on that database. The server key creates a class for pets, and a database index on pet names.

adminClient := f.NewFaunaClient("your-secret-here")
adminClient.Query(
    f.CreateDatabase(f.Obj{"name": "db-test"}),
)
serverKey, _ := adminClient.Query(
    f.CreateKey(f.Obj{
        "role":      "server", 
        "database":  f.Database("db-test"),
    }),
)
var secret string
serverKey.At(f.ObjKey("secret")).Get(&secret)
client := adminClient.NewSessionClient(secret)
client.Query(
    f.CreateClass(f.Obj{"name": "pets"}),
)
client.Query(
    f.CreateIndex(f.Obj{
       "name":   "pet_by_name",
       "source":  f.Class("pets"),
       "terms":   f.Arr{f.Obj{"field": f.Arr{"data", "name"}}},
       "unique": true,
    }),
)

We hope you enjoy our Go support, and we look forward to your pull requests.

To see our full documentation join our managed beta program, which will also get an account on Fauna’s cloud installation. When you are logged into the Fauna site, view our full documentation, and click the tab on the upper right of the page to set your language to Go.

If you enjoyed this topic and want to work on systems and challenges just like this, Fauna is hiring!