3. $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
5. 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
6. 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
7. 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
8. 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
9. 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
10. 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
11. Regular Expressions
• Full support for Perl regex
• Provides compilation, matching, finding,
splitting, and replacing
• Go’s backquotes allow for things like
`Hwllo` without doubling backslashes
Wednesday, July 31, 13
12. 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
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
14. goroutine rule
• “Do not communicate by sharing memory;
instead, share memory by communicating.”
Wednesday, July 31, 13
15. 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
16. 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