blob: dd3a0feb4b1004ffe2a2d04b4b77fa2ac9d7fe69 [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Introduction
2
3Tips for hacking on Go and having multiple ` $GOROOT ` workspaces...
4
Konstantin Kulikov17622402015-08-08 13:02:10 +03005Sometimes 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 Gerrand5bc444d2014-12-10 11:35:11 +11006
7Let'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
9Define 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
11For 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".
17dir=$(pwd)
18while 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
31done
32exec "$GOROOT/bin/go" "$@"
33```