Go Basics
Resources: http://golang.org/
Install and Setup
sudo apt-get install golang
http://golang.org/doc/code.html discusses setting up the GOLANG environment variable and your code repository
mkdir -p $HOME/projects/go/src export GOPATH=$HOME/projects/go (add this to .profile too)
Hello World
// hello.go
package main
import (
"fmt"
"os"
"strings"
)
func main() {
who := "World!"
if len(os.Args) > 1 { /* os.Args[0] is "hello" or "hello.exe" */
who = strings.Join(os.Args[1:], " ")
}
fmt.Println("Hello", who)
}
Commands
godoc -http=:8000 # host docs at http://localhost:8000 go build # create binary file in local directory go install # create binary file in $GOPATH/bin go run # just run it, disposing of binary go get github.com/nsf/gocode # grab a remote package and install it to $GOPATH/bin go get -u github.com/nsf/gocode # update to latest version of remote package
Packages
- Every file should begin with a
package
statement. - A package may be defined across several files.
- Code execution begins with a
main()
function insidepackage main
Comments
// for end-of-line
/* for multiline */
Constants and Variables
count, err = fmt.Println(x) // get number of bytes printed and error
count, _ = fmt.Println(x) // get number of bytes printed; discard error
_, err = fmt.Println(x) // discard number of bytes printed; get error
fmt.Println(x) // ignore return values
const limit = 512 // constant; type-compatible with any number
const top uint16 = 1421 // constant; type: uint16
start := -19 // variable; inferred type: int
end := int64(9876543210) // variable; type: int64
var i int // variable; value 0; type: int
var debug = false // variable; inferred type: bool
checkResults := true // variable; inferred type: bool
stepSize := 1.5 // variable; inferred type: float64
acronym := "FOSS" // variable; inferred type: string
i, j := 56, 67 // multiple assignment
Enumerations
const Cyan = 0 // one-per-line
const Magenta = 1
const Yellow = 2
const ( // grouped
Cyan = 0
Magenta = 1
Yellow = 2
)
const ( // using iota
Cyan = iota // 0
Magenta // 1
Yellow // 2
)
type BitFlag int // different example
const (
Active BitFlag = 1 << iota // (1 << 0 == 1)
Send // Implicitly BitFlag = 1 << iota (1 << 1 == 2)
Receive // Implicitly BitFlag = 1 << iota (1 << 2 == 4)
)
flag := Active | Send
Numeric type conversions
const factor = 3 // factor is compatible with any numeric type
i := 20000 // i is of type int by inference
i *= factor
j := int16(20) // j is of type int16; same as: var j int16 = 20
i += int(j) // Types must match so conversion is required
k := uint8(0) // Same as: var k uint8
k = uint8(i) // Succeeds, but k's value is truncated to 8 bits ✗
fmt.Println(i, j, k) // Prints: 60020 20 116
func Uint8FromInt(x int) (uint8, error) {
if 0 <= x && x <= math.MaxUint8 {
return uint8(x), nil
}
return 0, fmt.Errorf("%d is out of the uint8 range", x)
}
Types
Boolean
bool (true or false)
Integer
byte (synonym for uint8)
int (int32 or int64)
int8, int16, int32, int64
rune (synonym for int32, used for UTF-8 code points)
uint (uint32 or uint64)
uint8, uint16, uint32, uint64
uintptr (uint that stores a pointer)
Floating-Point
float32, float64
complex64, complex128
Bitwise Operations
x & y // The bitwise AND of x and y
x | y // The bitwise OR of x and y
x ^ y // The bitwise XOR of x and y
x &^ y // The bitwise clear (AND NOT) of x and y
x << u // The result of left-shifting x by unsigned int u shifts
x >> u // The result of right-shifting x by unsigned int u shifts