Skip to content

mh-cbon/go-get-started

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

go-get-started

get started with go

Install go >= 1.6.2

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

Configure the go workspace

Create a folder to host the workspace

mkdir $HOME/gow

see https://golang.org/doc/code.html#Workspaces

Setup the required environment variables

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

Setup directories

mkdir -p $GOPATH/pkg
mkdir -p $GOPATH/src
mkdir -p $GOPATH/bin

Get a package manager

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

Create your first package

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

Add a second package

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.

Import b package from a package

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

Depend on a remote package

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/

Other tools to use

go vet

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

go fmt

Gofmt formats Go programs.

$ go fmt
main.go

see this

go doc

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

There is more of it

Go team provides many tools that you can check here

Other notes

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 with go install. See this and that

If you really want to understand go implementation of import, please check Context.Import method.

Continue the readings

That s it !

~~ Happy coding

About

get started with go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages