get started with go
I recommend to use gvm
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
gvm install go1.6.2
gvm use go1.6.2 --default
which go
$HOME/.gvm/gos/go1.6.2/bin/go
go version
go version go1.6.2 linux/amd64
See https://github.com/moovweb/gvm
mkdir $HOME/gow
see https://golang.org/doc/code.html#Workspaces
cat <<EOT >> ~/.bashrc
# Go variables
export GOPATH=$HOME/gow
export PATH=$PATH:$GOPATH/bin
# go 1.6.x and below only
export GOBIN=$GOPATH/bin
EOT
source ~/.bashrc
see https://golang.org/doc/code.html#GOPATH
mkdir -p $GOPATH/pkg
mkdir -p $GOPATH/src
mkdir -p $GOPATH/bin
I recommend glide
at that time of writing
go get github.com/Masterminds/glide
cd $GOPATH/src/github.com/Masterminds/glide
make build
go install -ldflags "-X main.version=0.10.2-86-g5865b8e" glide.go
FYI which glide -> $GOPATH/bin/glide
see https://github.com/Masterminds/glide
Let s create a first package a
, hosted on github.com
, as user mh-cbon
.
At any time you can refer to this folder to see the expected result.
mkdir -p $GOPATH/src/github.com/mh-cbon/a
cd $GOPATH/src/github.com/mh-cbon/a
glide create
mkdir sub
touch sub/lib.go
# File: $GOPATH/src/github.com/mh-cbon/a/sub/lib.go
cat <<EOT > sub/lib.go
package lib
import "fmt"
func Hello () {
fmt.Println("Hello world! It's a/lib!")
}
EOT
touch main.go
# File: $GOPATH/src/github.com/mh-cbon/a/main.go
cat <<EOT > main.go
package main
import "github.com/mh-cbon/a/sub"
func main () {
lib.Hello()
}
EOT
go run main.go
Hello world! It's a/lib!
This step has created a package named a
with a binary available at main.go
.
a
package has a lib sub
loaded with import "github.com/mh-cbon/a/sub"
.
Notice the source are created under $GOPATH/src/
as github.com/mh-cbon/a
Let s create a second package b
to illustrate the setup and import of a library
mkdir -p $GOPATH/src/github.com/mh-cbon/b
cd $GOPATH/src/github.com/mh-cbon/b
glide create
touch index.go
# File: $GOPATH/src/github.com/mh-cbon/b/index.go
cat <<EOT > index.go
package b
import "fmt"
func Hello () {
fmt.Println("Hello world! It's b!")
}
EOT
This step created a b
package. It is a library only. It declares a file index.go
and is loadable via import "github.com/mh-cbon/b"
.
Notice the source are created under $GOPATH/src/
as github.com/mh-cbon/b
It is located at $GOPATH/src
, which makes it loadable from your other go programs/libraries as github.com/mh-cbon/b
.
cd $GOPATH/src/github.com/mh-cbon/a
# File: $GOPATH/src/github.com/mh-cbon/a/main.go
cat <<EOT > main.go
package main
import (
"github.com/mh-cbon/a/sub"
"github.com/mh-cbon/b"
)
func main () {
lib.Hello()
b.Hello()
}
EOT
go run main.go
Hello world! It's a/lib!
Hello world! It's b!
In this step program a
depends on the local library github.com/mh-cbon/b
declared and available at $GOPATH/src
Let s finish this readme by importing ans consuming a remote dependency.
a
will depend and consume github.com/Masterminds/semver
cd $GOPATH/src/github.com/mh-cbon/a
glide get github.com/Masterminds/semver
# File: $GOPATH/src/github.com/mh-cbon/a/main.go
cat <<EOT > main.go
package main
import (
"fmt"
"github.com/mh-cbon/a/sub"
"github.com/mh-cbon/b"
"github.com/Masterminds/semver"
)
func main () {
lib.Hello()
b.Hello()
c, _ := semver.NewConstraint("<= 1.2.3, >= 1.4")
fmt.Println(c)
}
EOT
go run main.go
Hello world! It's a/lib!
Hello world! It's b!
&{[[0xc8200129c0 0xc820012a00]]}
In this step program a
depends on a remote library installed via glide
.
The library semver
loaded via import "github.com/Masterminds/semver"
is installed and available at ./vendor/
.
It is a local dependency.
Notice the new glide.yaml
cat $GOPATH/src/github.com/mh-cbon/a/glide.yaml
package: github.com/mh-cbon/a
import:
- package: github.com/Masterminds/semver
Notice the new vendor
directory
ls -al .
sub/
glide.yaml
main.go
vendor/
Vet examines Go source code and reports suspicious constructs
$ go vet
can't load package: /home/mh-cbon/gow/src/github.com/mh-cbon/a/main.go:7:2: \
local import "./a" in non-local package
see this
Gofmt formats Go programs.
$ go fmt
main.go
see this
Show the documentation for the package in the current directory.
Standard practices to document your code are available here
Go team also provide automatic package documentation generation at godoc.org
Go team provides many tools that you can check here
Notice the environment variables
env | grep GO
GOROOT=$HOME/.gvm/gos/go1.6
GOPATH=$HOME/gow
GOBIN=$GOPATH/bin
Which means,
- when you run
go install....
it installs binary into$GOPATH/bin
- new packages are created under
$HOME/gow/src
- do not use relative import paths (eg
import "./mypackage/"
) because they are not compatible withgo install
. See this and that
If you really want to understand go implementation of import, please check Context.Import method.
That s it !
~~ Happy coding