Created
August 26, 2016 21:29
-
-
Save noahlt/3b0874eca0b03b900d5ab8f945501b4e to your computer and use it in GitHub Desktop.
Passing through multiple-value-return doesn't work well in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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