go: beyond the basics
DESCRIPTION
An advanced introduction to the Go language that I presented to the Atlanta Go User group on Wednesday, July 31, 2013.TRANSCRIPT
Go: Beyond the BasicsJoey Gibson
Senior ArchitectLancope, Inc.
@joeygibson
Wednesday, July 31, 13
Agenda• $GOPATH
• Project Organization
• Packages
• Structs
• Methods
• Interfaces
• Regular Expressions
• Functions are First-Class Citizens
• Goroutines and Channels
Wednesday, July 31, 13
$GOPATH
• A series of directories where Go looks for sources and libraries
• export GOPATH=~/Projects/gobtb:~/Projects/gotest
• Must contain a src/ directory, with package directories under that
• Will contain pkg/ directory after doing an install
• go env -- shows all variables
Wednesday, July 31, 13
Bash
export GOROOT=/Users/<yourname>/Projects/go
export GOPATH=/opt/go-3rdparty:/Users/<yourname>/Projects/gotest
export PATH=$GOROOT/bin:$PATH:${GOPATH//://bin:}/bin
Wednesday, July 31, 13
Project Organization
• Project directory should have a src/ subdirectory
• Files in subdirectories of src/ that are in a package (!main) install to pkg/$GOOS_$GOARCH
• (e.g. src/foo for a “foo” package)
• go install foo will install foo.a in pkg/$GOOS_$GOARCH
• Files in subdirectories of src/ that are in package main, install to the bin/ directory (e.g. src/bar for a bar command)
• go install bar will install bar executable in bin/
Wednesday, July 31, 13
Packages• Should live in src/foo, src/bar, etc.
• Everything in the directory will get built into a single “thing” (i.e. foo.a, bar[.exe])
• Can reference each other; don’t use relative references (e.g. import “../bar”)
• Can have subpackages
• package quux -- in src/foo/quux/*.go
• imported as “foo/quux”
• referenced as quux.Q()
• Don’t forget aliases -- import xxx foo/quux
Wednesday, July 31, 13
Structs
• Similar to C structs; data only
• Can have methods associated with them
• Can be created on stack or heap; compiler decides which based on context
• Can be nested
• ... also anonymously...
Wednesday, July 31, 13
Methods
• Look like functions, but have an extra type clause that specifies which “thing” the method is for
• The type clause can take a thing, or a pointer to a thing
• If specified as *thing, no need to provide address; Go will handle it
Wednesday, July 31, 13
Interfaces• Specify required methods a type must provide
• Any named type can implement methods to implement an interface
• No need to declare that a thing implements an interface
• A thing can implement multiple interfaces
• Like duck typing in Ruby, Python, etc., but...
• A compile-time error will occur if trying to pass a thing that doesn’t implement a required interface
• Convention is to name with -er suffix: Printer, Looper, Planner, etc.
Wednesday, July 31, 13
Interfaces: Semi-Generic
• Declaring that a function takes interface{} will allow the function to accept anything as a paramerter
• But that means anything at all
Wednesday, July 31, 13
Regular Expressions
• Full support for Perl regex
• Provides compilation, matching, finding, splitting, and replacing
• Go’s backquotes allow for things like `H\wllo` without doubling backslashes
Wednesday, July 31, 13
First-class functions
• Can be
• assigned to variables
• passed as parameters to other functions
• stored in maps, arrays, etc.
• created/executed anonymously
Wednesday, July 31, 13
goroutines
• goroutines execute concurrently, on one or more threads according to availability
• Very lightweight; start with a 4k stack
• Stack grows and shrinks as necessary
• Run in same address space as calling process...
• Are not parallel by default!
• To parallelize: set GOMAXPROCS > 1
Wednesday, July 31, 13
goroutine rule
• “Do not communicate by sharing memory; instead, share memory by communicating.”
Wednesday, July 31, 13
Channels• Datatype providing communication and
synchronization between goroutines
• Channels have a type; only one type of thing can be pushed over a channel
• Created using make()
• Can be buffered or not (defaults to unbuffered)
• Can be declared read-only or write-only...
• Unbuffered means sends and receives block until both sides are ready
Wednesday, July 31, 13
Channels
• The <-‐ operator pushes to, or pulls from, a channel
• The range operator reads from a channel, until it’s closed
• Close with close()
Wednesday, July 31, 13
Resources
• Testify: https://github.com/stretchr/testify
• Goclipse: https://code.google.com/p/goclipse/
Wednesday, July 31, 13