Today I Learned

hashrocket A Hashrocket project

Three syntactical elements that return 2 values

golang supports multiple return values from functions but there are also 3 different syntactical elements or accessors that also return 2 values.

The most straight forward is accessing values from a map.

colors := map[string]string{
  "ocean":  "blue",
  "forest": "green",
  "jet":   "black"
}

x, ok := colors["ocean"]

Type assertions also want to let you know that they're ok, like when we try to assert an error as a json.SyntaxError. Yeah, its a SyntaxError, ok?

synerr, ok := error.(*json.SyntaxError)

Channels can also let the program that things are going ok when using the receive syntax <-.

receivedValue, ok := <- ch
See More #go TILs