Skip to content

Instantly share code, notes, and snippets.

@krames
Last active January 20, 2016 01:52
Show Gist options
  • Save krames/c225ef4b8366b7a3ab63 to your computer and use it in GitHub Desktop.
Save krames/c225ef4b8366b7a3ab63 to your computer and use it in GitHub Desktop.

Go Bootcamp

All of the course material is open source and available here. I took the 3 day language class. You can find the agenda here.

Highlights

Slices of Slices

Slices of slices behave a little differently than I expected.

Given the following slice:

	slice1 := make([]string, 5, 8)
	slice1[0] = "Apple"
	slice1[1] = "Orange"
	slice1[2] = "Banana"
	slice1[3] = "Grape"
	slice1[4] = "Plum"

When you execute slice1[2:4] you would get a slice containing Banana and Grape. Slices of slices are considered views, so if we mutate our slice of slice it would change the original slice as well. You can see this in action here We can get around that by using tree index slicing.

Constants

Constants in go are implemented in a parallel type system and have much GREATER numerical size. On the mac, the maximum variable int size is 64, the maximum constant size is 256 bits. See example. Note: This is an implementation specific detail.

Constants can also be implicitly converted where as variables cannot be. Because of this fact we can do following:

    now := time.Now()
    lessFiveNanoseconds := now.Add(-5)

Whereas this:

var difference int = -5
var lessFiveNano = now.Add(difference)

Returns the following error:

./const.go:16: cannot use difference (type int) as type time.Duration in function argument

Methods

  • Methods declared with a pointer receiver, only implement the interface with pointer values.
  • Methods declared with a value receiver, implement the interface with both a value and pointer receiver.

In other words, unless we we expect to mutate a value we are better off have value receivers so we can use methods without a variable assignment. See example.

Embedding

You can embed types inside of other types. If the inner type is accessable to the outer type, the methods of the inter type will get promoted to the outer type. (Reminds me of composition and delegation) See example.

Error Handling

  • Use the default error value for static and simple formatted messages.
  • Create and return error variables to help the caller identify specific errors. These should be prefixed by Err. example
  • Create custom error types when the context of the error is more complex. These should be prefixed by Error. example
  • This example illustrates how errors are implemented in go.

Concurrency

  • Buffered channels provide no guarantee that another goroutine will receive it. The instructor strongly suggested using unbuffered channels or using one of the following Concurrency Patterns.
  • If you must use a buffered channel, start desigining with unbuffered channel to ensure you have properly handled growth issues and then go back and buffer it. He also warned about picking arbitrary buffer sizes.
  • Adding a -race flag to go build and go run will run race detection. To see that run the following example on the command line using the -race flag.
  • Check out the atomic package if you want to perform atomic operations. See atomic increment and atomic load/store examples.
  • Go provides a mutex as well as a read/write mutex.

Godebug

There is a special environmental variable named GODEBUG that will emit debugging information about the runtime as your program executes. For more information visit this page.

Try executing this from the commandline using this program:

GOMAXPROCS=2 GODEBUG=schedtrace=1000 go run godebug.go

For the lazy, this works as well, but it's not going to show multiple goroutines running:

GODEBUG=schedtrace=1000 docker images

Standard Library

  • Curl example
  • Recommended looking at the Time and OS packages for examples of idiomatic go.

Example REST API

  • Here is a sample code demonstrating an idiomatic RESTful API in Go.

Potpourri

  • The Go Playground is a 32-bit environment.
  • Implement code in the easiest way possible in order to maximize compiler/runtime optimization. Tweak the code later if need be.
  • If you are interested in performance checkout section on branch prediction and caching.
  • Go does have performance tools, but it requires an os patch for mac os x. For more info read this.
  • The instructor is trying to build a go a community on slack. Go here for an invite. This is a great resource if you have questions. There is also a newbie channel avaliable if you don't want to pose your question in main forum.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment