Skip to content

deta/deta-go

Repository files navigation

DETA SDK for Go

Go Doc

deta-go is the official Deta SDK for Go.

Installing

Use go get to retreive the SDK to add it to your GOPATH workspace, or project's Go module dependencies.

go get github.com/deta/deta-go

To update the SDK use go get -u to retrieve the latest version of the SDK.

go get -u github.com/deta/deta-go

If you are using Go modules, your go get will default to the latest tagged release version of the SDK. To get a specific release version of the SDK use @<tag> in your go get command.

go get github.com/deta/[email protected]

To get the latest SDK repository change use @latest.

go get github.com/aws/deta-go@latest

SDK Packages

The SDK constitutes of two main components, the core package and service packages.

  • deta: The core SDK package, provides shared functionalities to the service packages. All the errors are also exported from this package.

  • service: The service packages, the services supported by the SDK.

    • base: Deta Base service package
    • drive: Deta Drive service package

Configuring credentials

When using the SDK you will require you project key. The project key can be provided explicitly or is taken from the environement variable DETA_PROJECT_KEY.

Default

By default, the SDK looks for the environment variable DETA_PROJECT_KEY for the project key

// Create a new Deta instance taking the project key from the environment by default
d, err := deta.New()
if err != nil {
	fmt.Fprintf(os.Stderr, "failed to create new deta instance: %v\n", err)
}

Provide the project key explicitly

You can use the WithProjectKey option when creating a Deta instance to provide the project key explicitly.

// Create a new Deta instance with explicit project key
d, err := deta.New(deta.WithProjectKey("project_key"))
if err != nil {
	fmt.Fprintf(os.Stderr, "failed to create new deta instance: %v\n", err)
}

Examples

Base

The following is a simple Put operation example.

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/deta/deta-go/deta"
	"github.com/deta/deta-go/service/base"
)

// User an example user struct
type User struct {
	// json struct tag 'key' used to denote the key
	Key      string   `json:"key"` 
	Username string   `json:"username"`
	Active   bool     `json:"active"`
	Age      int      `json:"age"`
	Likes    []string `json:"likes"`
	// json struct tag '__expires' for expiration timestamp
	// 'omitempty' to prevent default 0 value
	Expires  int64    `json:"__expires,omitempty"`
}

func main() {
	// Create a new Deta instance with a project key
	d, err := deta.New(deta.WithProjectKey("project_key"))
	if err != nil {
		fmt.Fprintf(