|
| 1 | +--- |
| 2 | +title: Go |
| 3 | +--- |
| 4 | + |
| 5 | +To set up BAML with Go do the following: |
| 6 | + |
| 7 | +<Steps> |
| 8 | + ### Install BAML VSCode/Cursor Extension |
| 9 | + https://marketplace.visualstudio.com/items?itemName=boundary.baml-extension |
| 10 | + |
| 11 | + - syntax highlighting |
| 12 | + - testing playground |
| 13 | + - prompt previews |
| 14 | + |
| 15 | + ### Install BAML CLI and Initialize Project |
| 16 | + ```bash go |
| 17 | + go install github.com/boundaryml/baml/go/baml-cli@latest && baml-cli init |
| 18 | + ``` |
| 19 | + |
| 20 | + This command will: |
| 21 | + 1. Install the BAML CLI tool globally |
| 22 | + 2. Create starter BAML code in a `baml_src` directory |
| 23 | + 3. Set up the basic project structure |
| 24 | + |
| 25 | + ### Install BAML Go Runtime |
| 26 | + After initializing your project, install the Go runtime library: |
| 27 | + |
| 28 | + ```bash go |
| 29 | + go get github.com/boundaryml/baml |
| 30 | + ``` |
| 31 | + |
| 32 | + ### Install Required Go Tools |
| 33 | + The BAML generator uses `gofmt` and `goimports` to format the generated Go code. Install these tools: |
| 34 | + |
| 35 | + ```bash go |
| 36 | + # gofmt comes with Go by default, but install goimports |
| 37 | + go install golang.org/x/tools/cmd/goimports@latest |
| 38 | + ``` |
| 39 | + |
| 40 | + These tools are required by the `on_generate` command in your generator configuration and ensure the generated code is properly formatted. |
| 41 | + |
| 42 | + ### Generate the `baml_client` Go package from `.baml` files |
| 43 | + |
| 44 | + One of the files in your `baml_src` directory will have a [generator block](/ref/baml/generator). This tells BAML how to generate the `baml_client` directory, which will have auto-generated Go code to call your BAML functions. |
| 45 | + |
| 46 | + Any types defined in .baml files will be converted into Go structs in the `baml_client` directory. |
| 47 | + |
| 48 | + ```bash |
| 49 | + baml-cli generate |
| 50 | + ``` |
| 51 | + |
| 52 | + You can modify your build process to always call baml-cli generate before building. |
| 53 | + |
| 54 | + ```makefile Makefile |
| 55 | + .PHONY: generate build |
| 56 | + |
| 57 | + generate: |
| 58 | + baml-cli generate |
| 59 | + |
| 60 | + build: generate |
| 61 | + go build ./... |
| 62 | + |
| 63 | + test: generate |
| 64 | + go test ./... |
| 65 | + ``` |
| 66 | + |
| 67 | + See [What is baml_client](/guide/introduction/baml_client) to learn more about how this works. |
| 68 | + |
| 69 | + <Tip> |
| 70 | + If you set up the [VSCode extension](https://marketplace.visualstudio.com/items?itemName=Boundary.baml-extension), it will automatically run `baml-cli generate` on saving a BAML file. |
| 71 | + </Tip> |
| 72 | + |
| 73 | + ### Use a BAML function in Go! |
| 74 | + <Error>If `baml_client` doesn't exist, make sure to run the previous step! </Error> |
| 75 | + |
| 76 | + ```go main.go |
| 77 | + package main |
| 78 | + |
| 79 | + import ( |
| 80 | + "context" |
| 81 | + "fmt" |
| 82 | + "log" |
| 83 | + |
| 84 | + b "example.com/myproject/baml_client" |
| 85 | + "example.com/myproject/baml_client/types" |
| 86 | + ) |
| 87 | + |
| 88 | + func main() { |
| 89 | + ctx := context.Background() |
| 90 | + |
| 91 | + // BAML's internal parser guarantees ExtractResume |
| 92 | + // to always return a Resume type or an error |
| 93 | + resume, err := b.ExtractResume(ctx, rawResume) |
| 94 | + if err != nil { |
| 95 | + log.Fatal(err) |
| 96 | + } |
| 97 | + |
| 98 | + fmt.Printf("Extracted resume: %+v\n", resume) |
| 99 | + } |
| 100 | + |
| 101 | + func exampleStream(rawResume string) (*types.Resume, error) { |
| 102 | + ctx := context.Background() |
| 103 | + |
| 104 | + stream, err := b.Stream.ExtractResume(ctx, rawResume) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + for value := range stream { |
| 110 | + if value.IsError { |
| 111 | + return nil, value.Error |
| 112 | + } |
| 113 | + |
| 114 | + if !value.IsFinal && value.Stream() != nil { |
| 115 | + partial := *value.Stream() |
| 116 | + fmt.Printf("Partial: %+v\n", partial) // This will be a partial Resume type |
| 117 | + } |
| 118 | + |
| 119 | + if value.IsFinal && value.Final() != nil { |
| 120 | + final := *value.Final() |
| 121 | + return &final, nil // This will be a complete Resume type |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return nil, fmt.Errorf("stream ended without final response") |
| 126 | + } |
| 127 | + ``` |
| 128 | +</Steps> |
| 129 | + |
| 130 | +## Working with Go Modules |
| 131 | + |
| 132 | +BAML integrates seamlessly with Go modules. Make sure your `go.mod` file includes the BAML dependency: |
| 133 | + |
| 134 | +```go go.mod |
| 135 | +module example.com/myproject |
| 136 | + |
| 137 | +go 1.21 |
| 138 | + |
| 139 | +require ( |
| 140 | + github.com/boundaryml/baml v0.203.1 |
| 141 | +) |
| 142 | +``` |
| 143 | + |
| 144 | +The generated `baml_client` package will use your module path, so you can import it as: |
| 145 | + |
| 146 | +```go |
| 147 | +import ( |
| 148 | + b "example.com/myproject/baml_client" |
| 149 | + "example.com/myproject/baml_client/types" |
| 150 | +) |
| 151 | +``` |
| 152 | + |
| 153 | +## Context and Cancellation |
| 154 | + |
| 155 | +All BAML Go functions require a `context.Context` as the first parameter, allowing you to: |
| 156 | + |
| 157 | +```go |
| 158 | +// Set timeouts |
| 159 | +ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 160 | +defer cancel() |
| 161 | + |
| 162 | +result, err := b.ExtractResume(ctx, resume) |
| 163 | + |
| 164 | +// Handle cancellation |
| 165 | +ctx, cancel := context.WithCancel(context.Background()) |
| 166 | +go func() { |
| 167 | + time.Sleep(5 * time.Second) |
| 168 | + cancel() // Cancel the request after 5 seconds |
| 169 | +}() |
| 170 | + |
| 171 | +result, err := b.ExtractResume(ctx, resume) |
| 172 | +if errors.Is(err, context.Canceled) { |
| 173 | + fmt.Println("Request was canceled") |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +You're all set! Continue on to the [Deployment Guides](/guide/development/deploying/docker) for your language to learn how to deploy your BAML code or check out the [Interactive Examples](https://baml-examples.vercel.app/) to see more examples. |
0 commit comments