Skip to content

Commit 8e737d9

Browse files
mohsenariLagoja
andauthored
[docs] Added gopath example in docs (#1957)
## Summary TSIA This was requested by community user. ## How was it tested? N/A --------- Co-authored-by: John Lago <[email protected]>
1 parent 33815df commit 8e737d9

File tree

2 files changed

+32
-1
lines changed
  • docs/app/docs/devbox_examples/languages
  • examples/development/go/hello-world

2 files changed

+32
-1
lines changed

docs/app/docs/devbox_examples/languages/go.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,30 @@ If you need additional C libraries, you can add them along with `gcc` to your pa
2929
"libcap"
3030
]
3131
```
32+
33+
## Installing go packages that have CLIs
34+
35+
Installing go packages in your devbox shell is as simple as `go get <package_name>` but some packages come with a CLI of their own (e.g., `godotenv`). That means after installing the package you should be able to use the CLI binary and also control where that binary is installed. This is done by setting `$GOPATH` or `$GOBIN` env variable.
36+
37+
With Devbox you can set these variables in the `"env"` section of your `devbox.json` file.
38+
In the example below we are setting `$GOPATH` to be the same directory of our project and therefore `godotenv` binary will be located in the `bin/` subdirectory of `$GOPATH`:
39+
40+
```json
41+
{
42+
"packages": [
43+
"go@latest"
44+
],
45+
"env": {
46+
"GOPATH": "$PWD",
47+
"PATH": "$PATH:$PWD/bin"
48+
},
49+
"shell": {
50+
"init_hook": [
51+
"echo 'Welcome to devbox!' > /dev/null"
52+
],
53+
"scripts": {}
54+
}
55+
}
56+
```
57+
58+
Running `go install github.com/joho/godotenv/cmd/godotenv@latest` will create a `bin/` subdirectory in my project and puts `godotenv` there. Since I also added that subdirectory to my `$PATH`, my devbox shell can now recognize the `godotenv` binary and I can run commands like `godotenv -h` to use `godotenv` in CLI mode.

examples/development/go/hello-world/devbox.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"packages": [
33
44
],
5+
"env": {
6+
"GOPATH": "$HOME/go/",
7+
"PATH": "$PATH:$HOME/go/bin"
8+
},
59
"shell": {
610
"init_hook": [
711
"export \"GOROOT=$(go env GOROOT)\""
@@ -10,4 +14,4 @@
1014
"run_test": "go run main.go"
1115
}
1216
}
13-
}
17+
}

0 commit comments

Comments
 (0)