Skip to main content

Getting Started with Momento Storage in Go

If you need to get going quickly with Go and Momento Storage, this page contains the basic API calls you'll need. Check the Go SDK examples for complete, working examples including build configuration files.

Install the Momento SDK

The Momento SDK is available on Go Packages: client-sdk-go.

tip

Visit Go Packages to find the latest available version of the SDK.

Install the client library in an existing Go project:

go get github.com/momentohq/client-sdk-go

Set up your API key

You'll need a Momento API key to authenticate with Momento. You can get one from the Momento Web Console. Once you have your API key, store it in an environment variable so that the Momento client can consume it:

export MOMENTO_API_KEY=<your Momento API key here>

Import libraries and connect to return a StorageClient object

This code sets up the main function, the necessary imports, and the StorageClient that will be used to interact with your store.

package main

import (
"context"
"fmt"

"github.com/momentohq/client-sdk-go/auth"
"github.com/momentohq/client-sdk-go/config"
"github.com/momentohq/client-sdk-go/momento"
)

func main() {
ctx := context.Background()
var credentialProvider, err = auth.NewEnvMomentoTokenProvider("MOMENTO_API_KEY")
if err != nil {
panic(err)
}

client, err := momento.NewPreviewStorageClient(config.StorageLaptopLatest(), credentialProvider)

var storeName = "store-name"

defer func() {
fmt.Println("Deleting store")
_, err = client.DeleteStore(ctx, &momento.DeleteStoreRequest{
StoreName: storeName,
})
if err != nil {
panic(err)
}
}()

// ...
}

Create a new store in Momento Storage

Use this function to create a new store in your account.

resp, err := storageClient.CreateStore(ctx, &momento.CreateStoreRequest{
StoreName: storeName,
})
if err != nil {
panic(err)
}

switch resp.(type) {
case *responses.CreateStoreSuccess:
fmt.Printf("Successfully created store %s\n", storeName)
case *responses.CreateStoreAlreadyExists:
fmt.Printf("Store %s already exists\n", storeName)
}

List the names of existing stores in your account

A simple list of the names of stores for the account.

resp, err := storageClient.ListStores(ctx, &momento.ListStoresRequest{})
if err != nil {
panic(err)
}

switch r := resp.(type) {
case *responses.ListStoresSuccess:
log.Printf("Found stores:\n")
for _, store := range r.Stores() {
log.Printf("\tStore name: %s\n", store.Name())
}
}

Write an item to a store

A simple example of doing a put operation.

_, err := storageClient.Put(ctx, &momento.StoragePutRequest{
StoreName: storeName,
Key: "key",
Value: storageTypes.String("my-value"),
})
if err != nil {
panic(err)
}

// Momento storage also supports these other data types:
storageClient.Put(ctx, &momento.StoragePutRequest{
StoreName: storeName,
Key: "key",
Value: storageTypes.Int(42),
})
storageClient.Put(ctx, &momento.StoragePutRequest{
StoreName: storeName,
Key: "key",
Value: storageTypes.Float(3.14),
})
storageClient.Put(ctx, &momento.StoragePutRequest{
StoreName: storeName,
Key: "key",
Value: storageTypes.Bytes{0x01, 0x02, 0x03},
})

Read an item from a store

This is an example of a simple read operation to get an item from a store.

getResp, err := storageClient.Get(ctx, &momento.StorageGetRequest{
StoreName: storeName,
Key: "key",
})
if err != nil {
panic(err)
}

// If the value was not found, the response's Value will be nil.
if getResp.Value() == nil {
fmt.Println("Got nil")
}

// If you know the type you're expecting, you can assert it directly:
intVal, ok := getResp.Value().(storageTypes.Int)
if !ok {
fmt.Printf("Not an integer, received type: %T\n", getResp.Value())
} else {
fmt.Printf("Got the integer %d\n", intVal)
}

// Use switch if you don't know the type beforehand:
switch t := getResp.Value().(type) {
case storageTypes.String:
fmt.Printf("Got the string %s\n", t)
case storageTypes.Bytes:
fmt.Printf("Got the bytes %b\n", t)
case storageTypes.Float:
fmt.Printf("Got the float %f\n", t)
case storageTypes.Int:
fmt.Printf("Got the integer %d\n", t)
case nil:
fmt.Println("Got nil")
}

Running the code

You can find complete, working examples in the Go SDK github repo examples directory.

info

Beyond these basic API calls check out the API reference page for more information on the full assortment of Momento API calls.