Go language
References
About Go
- created by Google
- It can be compiled (with libs and runtime included) - Whooho!
- What's the Go language really good for?
- Every Go program is made up of packages. Programs start running in package main.
- In Go, a name is exported if it begins with a capital letter. Foo or FOO is exported, foo is not exported.
- Finally, the convention in Go is to use MixedCaps or mixedCaps rather than underscores to write multiword names.
$GOPATH & Workspaces.
Go code must be kept inside a workspace. A workspace is a directory hierarchy with three directories at its root:
- src contains Go source files organized into packages (one package per directory),
- pkg contains package objects, and
- bin contains executable commands.
Formatting
- go fmt
- Indentation: We use tabs for indentation and gofmt emits them by default. Use spaces only if you must.
- Line length: Go has no line length limit. Don't worry about overflowing a punched card. If a line feels too long, wrap it and indent with an extra tab.
- Parentheses: Go needs fewer parentheses than C and Java: control structures (if, for, switch) do not have parentheses in their syntax. Also, the operator precedence hierarchy is shorter and clearer.
Compilation example:
mkdir -p work/bin work/src work/pkg
$ export GOPATH=$HOME/work
$ cat << EOF >> $GOPATH/src/hello.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
EOF
# Then compile it with the go tool:
$ export GOBIN=$GOPATH/bin
$ go install $GOPATH/src/hello.go
$ $GOPATH/bin/hello
hello, world
Ps: Use 'go build' instead of 'go install' or even 'go run "path/hello.go"'.
end