Skip to content

Instantly share code, notes, and snippets.

@noahlt
Created August 26, 2016 21:29
Show Gist options
  • Save noahlt/3b0874eca0b03b900d5ab8f945501b4e to your computer and use it in GitHub Desktop.
Save noahlt/3b0874eca0b03b900d5ab8f945501b4e to your computer and use it in GitHub Desktop.
Passing through multiple-value-return doesn't work well in Go
package main
import "fmt"
var m = map[string]int{"a": 1, "b": 2, "c": 3}
func lookup(k string) (int, bool) {
// Can't do this :(
//
//return m[k]
//
// You'll get:
//
// ./go_multiple_return.go:8: not enough arguments to return
//
// Instead, have to:
v, ok := m[k]
return v, ok
}
func main() {
if v, ok := lookup("b"); ok {
fmt.Printf("m[\"b\"] = %d\n", v)
} else {
fmt.Printf("key not found")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment