Skip to content

Instantly share code, notes, and snippets.

@cstockton
Created July 20, 2017 23:43
Show Gist options
  • Select an option

  • Save cstockton/5567dbc67c76437e7cc8150b8f68d919 to your computer and use it in GitHub Desktop.

Select an option

Save cstockton/5567dbc67c76437e7cc8150b8f68d919 to your computer and use it in GitHub Desktop.
Contributing to Go - Workspace configuration example
#!/bin/bash
####
## Workspace layout
#
# I follow a few conventions to keep managing things simple. My default Go
# workspace where most personal projects are live in /one/ws/go. Then I have
# each major release along with tip in /one/ws/go<{semver}|tip>. i.e.:
#
# The "go" folder contains official tagged release of latest Go version I simply
# extracted from the archive built by the Go team after validating the checksum.
# I never edit the files in go for this workspace. All my development happens in
# the "src" dir similar to how any userspace dev is done.
#
# /one/ws/go
# ├── bin
# ├── go # contains official tagged release of latest Go version
# ├── pkg
# └── src
#
# The "go" folder for my tip workspace contains a git checkout of tip and is
# where I do all development on the Go language itself. Here I only use the
# "src" dir to run packages or code that I'm using to validate my changes as
# well as any tooling that may need my changes.
#
# /one/ws/gotip
# ├── bin
# ├── go
# ├── pkg
# └── src
#
# This setup allows me to easily test changes that may span accross multiple
# versions of Go. For example when creating github.com/cstockton/go-trace I had
# to generate testdata for all versions of the Go trace format. Being able to
# easily switch versions with `gows 1.7` `gows 1.5` `gows 1.8` to generate new
# trace output or print out some debug information from src/runtime/trace made
# things a lot easier.
####
## Switching workspaces via file - /one/ws/gotip/exports.sh
#
# A simple method I use for workspaces in some languages is to have a env.sh
# file in the workspace root that may be sourced to configure your shell for
# that workspace.
## File: /one/ws/gotip/exports.sh
# A static env file placed in the $GOPATH root can be used instead of a shell
# function. I.E.:
#
# cd /one/ws/godevtip && . env.sh
#
_prev_bin=$GOPATH/bin
export GOROOT="/one/ws/gotip/go"
export GOPATH="/one/ws/gotip"
export PATH="$GOROOT/bin:$(echo "${PATH}"|sed "s|${_prev_bin}||g")"
####
## Switching workspaces via shell func - ~/.bashrc
#
# The method I use for Go since I change workspaces fairly often is a small
# helper function in my .bashrc along with a couple env vars.
# WSROOT specifies where my "workspace" root is. The GOROOT_BOOTSTRAP is what to
# bootstrap your builds with and should just be set to the latest version of go.
# WSREPO specifies the repo to go to by default if it exists when restoring my
# ws to save myself some time.
export WSROOT="/one/ws"
export WSREPO="github.com/${USER}"
export GOROOT_BOOTSTRAP="/one/ws/go"
# I define a simple func similar to this to change my env vars and change my
# working directory to the new Go workspace.
#
# gows tip # Change to using tip -> GOPATH will be /one/ws/gotip
# gows # Restore gopath to default -> GOPATH will be /one/ws/go
function gows {
_ws_tmp="${WSROOT}/go${1}"
[ -d "${_ws_tmp}/src" ] || \
{ printf >&2 "invalid workspace ${_ws_tmp}"; return; }
_goswap ${1}
_gorepo="${GOPATH}/src/${WSREPO}"
if [[ $# -eq 0 && -d "${_gorepo}" ]]; then
cd "${_gorepo}"
else
cd "${GOPATH}/src"
fi
}
function _goswap {
_prev_bin=$GOPATH/bin
export GOROOT="/one/ws/go${1}/go"
export GOPATH="/one/ws/go${1}"
export PATH="$GOROOT/bin:$(echo "${PATH}"|sed "s|${_prev_bin}||g")"
}
# I set my gows to the default at login by giving it no argument.
_goswap
# Some other useful funcs that I've omitted but may be useful is a simple func
# to recompile GOROOT or compile a specific package based on your CWD.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment