Skip to content

Run your first graph

Build a graph that takes a name and returns a greeting. It runs entirely on your machine and needs only Go 1.26.10 or newer.

In a terminal, create an empty project and add Loom’s graph package.

Terminal window
mkdir hello-loom
cd hello-loom
go mod init example.com/hello-loom
go get github.com/masterkeysrd/loom/graph

Save this program as main.go. The two edges tell Loom to start at greet and finish when that node returns its update.

main.go
package main
import (
"context"
"fmt"
"log"
"github.com/masterkeysrd/loom/graph"
)
type Greeting struct {
Name string
Text string
}
func (s Greeting) Copy() Greeting { return s }
func greet(_ context.Context, _ Greeting) (graph.Command[Greeting], error) {
return graph.Update[Greeting](func(s Greeting) Greeting {
s.Text = "Hello, " + s.Name + "!"
return s
}), nil
}
func main() {
builder := graph.New[Greeting]()
builder.AddNode("greet", graph.NodeFunc(greet))
builder.AddEdge(graph.START, "greet")
builder.AddEdge("greet", graph.END)
g, err := builder.Build()
if err != nil {
log.Fatal(err)
}
input := graph.Update[Greeting](func(s Greeting) Greeting {
s.Name = "Ada"
return s
})
result, err := g.Execute(context.Background(), input, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.State.Text)
}
Terminal window
go run .

You should see this greeting.

Hello, Ada!

Change s.Name = "Ada" to s.Name = "Sam" and run the program again. The input update sets the starting state, the node fills in Text, and result.State contains the final greeting.