Suborbital
Search…
Go Library
The compute-go library helps you easily interact with the Compute API from your Go application. Suborbital Compute is designed to run alongside your application in a Kubernetes or Docker Compose environment.

Documentation

Like most Go packages, complete and up to date technical documentation for compute-go can be found on pkg.go.dev. Those docs are generated from inline comments in the compute-go source code.

Installation

In a directory with a go.mod file, run:
1
go get github.com/suborbital/[email protected]
Copied!

Basic Usage

Configuration

This example sets up a basic client with the token generated in Step 1 or with the web app. The compute.Client object created here assumes that Compute is running on the same host on its default ports. The code for this example can be found here.
client.go
1
package main
2
3
import (
4
"os"
5
6
"github.com/suborbital/compute-go"
7
)
8
9
func client() *compute.Client {
10
token, _ := os.LookupEnv("SCC_ENV_TOKEN")
11
client, _ := compute.NewClient(compute.LocalConfig(), token)
12
13
return client
14
}
Copied!

Build and run a function

We can now integrate Compute into an application. compute-go has access to all of Compute's APIs. It can run builds, list existing functions, run tests, and execute functions.
Behind the scenes, compute-go manages authentication, so you don't have to worry about setting the right HTTP headers when interacting with the Compute API.
app.go
1
package main
2
3
import (
4
"log"
5
6
"github.com/suborbital/compute-go"
7
)
8
9
func main() {
10
client := client()
11
12
// This is a local reference to some Runnable. Nothing has run in Compute at this point.
13
runnable := compute.NewRunnable("com.suborbital", "acmeco", "default", "rs-hello-world", "rust")
14
15
// Request template source code for the above Runnable.
16
template, _ := client.BuilderTemplate(runnable)
17
18
// Log the default 'hello world' Rust template to stdout
19
log.Println(template.Contents)
20
21
// Run a remote build for the provided Runnable and the unmodified 'hello world'
22
// template source code.
23
build, _ := client.BuildFunctionString(runnable, template.Contents)
24
25
if !build.Succeeded {
26
// Log the compiler output to see why the build failed
27
log.Fatal(build.OutputLog)
28
}
29
30
// Deploy the function (the runnable's .Version field is adjusted here)
31
client.PromoteDraft(runnable)
32
33
// Execute the function
34
result, _ := client.ExecString(runnable, "world!")
35
36
// Log the execution output
37
log.Println(string(result))
38
}
Copied!
Now that the Runnable has been built, it can be executed as much as you like without rebuilding using client.Exec or client.ExecString.
Last modified 2mo ago