Go
To set up BAML with Go do the following:
Install BAML VSCode/Cursor Extension
https://marketplace.visualstudio.com/items?itemName=boundary.baml-extension
- syntax highlighting
- testing playground
- prompt previews
Install BAML CLI and Initialize Project
This command will:
- Install the BAML CLI tool globally
- Create starter BAML code in a
baml_src
directory - Set up the basic project structure
Install Required Go Tools
The BAML generator uses gofmt
and goimports
to format the generated Go code. Install these tools:
These tools are required by the on_generate
command in your generator configuration and ensure the generated code is properly formatted.
Generate the baml_client
Go package from .baml
files
One of the files in your baml_src
directory will have a generator block. This tells BAML how to generate the baml_client
directory, which will have auto-generated Go code to call your BAML functions.
Any types defined in .baml files will be converted into Go structs in the baml_client
directory.
You can modify your build process to always call baml-cli generate before building.
See What is baml_client to learn more about how this works.
If you set up the VSCode extension, it will automatically run baml-cli generate
on saving a BAML file.
Working with Go Modules
BAML integrates seamlessly with Go modules. Make sure your go.mod
file includes the BAML dependency:
The generated baml_client
package will use your module path, so you can import it as:
Context and Cancellation
All BAML Go functions require a context.Context
as the first parameter, allowing you to:
You’re all set! Continue on to the Deployment Guides for your language to learn how to deploy your BAML code or check out the Interactive Examples to see more examples.