Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/development/go/hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,38 @@ If you need additional C libraries, you can add them along with `gcc` to your pa
"libcap"
]
```

## Setting up the Go environment

This example configures a few environment variables in `devbox.json`:

```json
"env": {
"GOPATH": "$HOME/go/",
"PATH": "$PATH:$HOME/go/bin"
}
```

- `GOPATH` is set to `$HOME/go`, which is Go's default location. Keeping the
`GOPATH` outside of your project directory lets the module cache
(`$GOPATH/pkg/mod`) be shared across projects and keeps build artifacts out of
your source tree.
- Adding `$GOPATH/bin` to `PATH` makes tools installed with `go install`
available inside the Devbox shell.

The `init_hook` exports `GOROOT` so that the Go toolchain provided by Nix is
used:

```json
"shell": {
"init_hook": [
"export \"GOROOT=$(go env GOROOT)\""
]
}
```

> **Note on `GOPATH`:** avoid setting `GOPATH` to your project directory (e.g.
> `"GOPATH": "$PWD"`). When a module (a directory containing `go.mod`) lives
> inside `GOPATH`, the Go toolchain prints `go: warning: ignoring go.mod in
> $GOPATH` and falls back to legacy `GOPATH` mode. Using `$HOME/go` (as above)
> keeps module-aware builds working as expected.
Loading