Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 1 | # Introduction |
| 2 | |
| 3 | Tips for hacking on Go and having multiple ` $GOROOT ` workspaces... |
| 4 | |
Konstantin Kulikov | 1762240 | 2015-08-08 13:02:10 +0300 | [diff] [blame] | 5 | Sometimes you need to check out multiple copies of the Go tree, perhaps you're working on several core library changes at once and you want to test them independently. |
Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 6 | |
| 7 | Let's say you've checked the trees out as ` $HOME/go1 `, ` $HOME/go2 `, etc. (The specific names are not important.) While you're working in each tree, it's important that you always set ` GOROOT ` to the correct tree or unexpected things will happen, like binaries will be built from sources other than the ones you've just edited. Such mistakes can be time-consuming to notice, and it's easy to forget to update ` GOPATH ` when you change directories. The following trick may be helpful. |
| 8 | |
| 9 | Define a script called ` go `, and ensure its directory is on your ` PATH ` or define a shell alias ` go ` that points to it. In the script, set the ` GOROOT ` and (if you like) ` GOPATH ` environment variables to appropriate values determined from your current working directory. Then exec the real ` go ` command. |
| 10 | |
| 11 | For example: |
| 12 | |
| 13 | ``` |
| 14 | #!/bin/sh |
| 15 | # Set GOROOT to the innermost enclosing directory containing |
| 16 | # an AUTHORS file. Set GOPATH to its child called "got". |
| 17 | dir=$(pwd) |
| 18 | while true; do |
| 19 | if [ -f "$dir/AUTHORS" ]; then |
| 20 | export "GOROOT=$dir" |
| 21 | export "GOPATH=$GOROOT/got" |
| 22 | echo "GOROOT=$GOROOT" >&2 |
| 23 | echo "GOPATH=$GOPATH" >&2 |
| 24 | break |
| 25 | fi |
| 26 | dir=$(dirname "$dir") |
| 27 | if [ "$dir" = / ]; then |
| 28 | echo "Can't locate GOROOT". >&2 |
| 29 | exit 1 |
| 30 | fi |
| 31 | done |
| 32 | exec "$GOROOT/bin/go" "$@" |
| 33 | ``` |