Go: Beyond the Basics

2,234 views
2,169 views

Published on

An advanced introduction to the Go language that I presented to the Atlanta Go User group on Wednesday, July 31, 2013.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,234
On SlideShare
0
From Embeds
0
Number of Embeds
1,276
Actions
Shares
0
Downloads
35
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Go: Beyond the Basics

  1. 1. Go: Beyond the Basics Joey Gibson Senior Architect Lancope, Inc. @joeygibson Wednesday, July 31, 13
  2. 2. Agenda • $GOPATH • Project Organization • Packages • Structs • Methods • Interfaces • Regular Expressions • Functions are First-Class Citizens • Goroutines and Channels Wednesday, July 31, 13
  3. 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
  4. 4. 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
  5. 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. 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. 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. 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. 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. 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. 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. 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. 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. 14. goroutine rule • “Do not communicate by sharing memory; instead, share memory by communicating.” Wednesday, July 31, 13
  15. 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. 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
  17. 17. Resources • Testify: https://github.com/stretchr/testify • Goclipse: https://code.google.com/p/ goclipse/ Wednesday, July 31, 13

×