📙 The architect's guide to building scalable global applications |
Learn more
Fauna logo
Product
Solutions
Pricing
Resources
Company
Log InContact usStart for free
Fauna logo
Pricing
Customers
Log InContact usStart for free
© 2022 Fauna, Inc. All Rights Reserved.

Related posts

Building composable serverless architectures with Fauna, Netlify, and Big Nerd RanchImplementing Fauna as infrastructure as code with Serverless FrameworkTransfer data in Fauna to your analytics tool using Airbyte

Get started

Sign up and claim your forever-free Fauna account
Sign up and get started

Request demo

Connect with a Fauna expert
request demo

Introducing Support for the Go Programming Language

Chris Anderson|Nov 18th, 2016|

Categories:

FeaturesTutorial
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 Fauna. If you are already familiar with using Fauna in another language, the Go driver will be immediately familiar and you will feel right at home coding against Fauna in Go.
A quick note: To use these drivers with Fauna Cloud, you will need to join the managed beta program.
The rest of this blog post is a tour of essential Fauna 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 Fauna query in Go, using a domain specific language designed to be friendly to Go users and also similar to Fauna 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 Fauna. 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 Fauna 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 our blog, and want to work on systems and challenges related to globally distributed systems, serverless databases, GraphQL, and Jamstack, Fauna is hiring!

Share this post

TWITTERLINKEDIN
‹︁ PreviousNext ›︁

Subscribe to Fauna's newsletter

Get latest blog posts, development tips & tricks, and latest learning material delivered right to your inbox.