import { Callout } from "nextra/components";
This is the Hatchet Go SDK reference. On this page, we'll get you up and running with a Go worker. This guide assumes that you already have a Hatchet engine instance running. If you don't, you can:
- Sign up on Hatchet Cloud
- Self-host Hatchet
Run the following to install the Hatchet Go SDK:
go get github.com/hatchet-dev/hatchet/pkg
Navigate to your Hatchet dashboard and navigate to your settings tab. You should see a section called "API Keys". Click "Create API Key", input a name for the key and copy the key. Then set the following environment variables:
HATCHET_CLIENT_TOKEN="<your-api-key>"
HATCHET_CLIENT_TLS_STRATEGY=none
Create a main.go
file with the following contents:
package main
import (
"fmt"
"github.com/hatchet-dev/hatchet/pkg/client"
"github.com/hatchet-dev/hatchet/pkg/cmdutils"
"github.com/hatchet-dev/hatchet/pkg/worker"
)
type stepOutput struct{}
func main() {
c, err := client.New()
if err != nil {
panic(fmt.Sprintf("error creating client: %v", err))
}
w, err := worker.NewWorker(
worker.WithClient(
c,
),
worker.WithMaxRuns(1),
)
if err != nil {
panic(fmt.Sprintf("error creating worker: %v", err))
}
err = w.RegisterWorkflow(
&worker.WorkflowJob{
Name: "simple-workflow",
Description: "Simple one-step workflow.",
On: worker.Events("simple"),
Steps: []*worker.WorkflowStep{
worker.Fn(func(ctx worker.HatchetContext) (result *stepOutput, err error) {
fmt.Println("executed step 1")
return &stepOutput{}, nil
},
).SetName("step-one"),
},
},
)
if err != nil {
panic(fmt.Sprintf("error registering workflow: %v", err))
}
interruptCtx, cancel := cmdutils.InterruptContextFromChan(cmdutils.InterruptChan())
defer cancel()
cleanup, err := w.Start()
if err != nil {
panic(fmt.Sprintf("error starting worker: %v", err))
}
<-interruptCtx.Done()
if err := cleanup(); err != nil {
panic(err)
}
}
Next, run the following command to start the worker:
go run main.go
The worker is now running and listening for steps to execute. You should see your first worker registered in the Workers
tab of the Hatchet dashboard:
You can now trigger your first workflow by navigating to the Workflows
tab, selecting your workflow, and clicking the top right "Trigger workflow" button:
That's it! You've successfully deployed Hatchet and run your first workflow.
Congratulations on running your first workflow!
To test out more complex workflows, check out the Hatchet Go Quickstart.