Skip to content

Instantly share code, notes, and snippets.

@HoffmannP
Created January 24, 2013 13:56
Show Gist options
  • Save HoffmannP/4621876 to your computer and use it in GitHub Desktop.
Save HoffmannP/4621876 to your computer and use it in GitHub Desktop.
# using the instructions from http://golang.org/doc/code.html
# either use the first command line argument or read the test directory
if [ $# -gt 0 ]; then
dir=$1;
else
read -p "test directory: " dir
fi
###############################
# minimal example starts here #
###############################
# if not existent create test directory
if [ ! -d $dir ]; then
mkdir -p $dir;
fi
cd $dir
# don't forget to export
export GOPATH="$(pwd)"
# create the packages directory inside the "src" directory
mkdir -p src/newmath;
# create an example package file
echo '// Package newmath is a trivial example package.
package newmath
// Sqrt returns an approximation to the square root of x.
func Sqrt(x float64) float64 {
// This is a terrible implementation.
// Real code should import "math" and use math.Sqrt.
z := 0.0
for i := 0; i < 1000; i++ {
z -= (z*z - x) / (2 * x)
}
return z
}
' > src/example/newmath/sqrt.go
# This should give an return value of zero and no output at all
go install example/newmath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment