In Go, the following works (note one use of the map has one return, the other has two returns)
package main import "fmt" var someMap = map[string]string { "some key": "hello" } func main() { if value, ok := someMap["some key"]; ok { fmt.Println(value) } value := someMap["some key"] fmt.Println(value) }
However, I have no idea how to do this same thing with my own function. Is it possible to have similar behavior with an optional return like map
?
For example:
package main import "fmt" func Hello() (string, bool) { return "hello", true } func main() { if value, ok := Hello(); ok { fmt.Println(value) } value := Hello() fmt.Println(value) }
Wont compile (due to the error multiple-value Hello() in single-value context
) ... is there a way to make this syntax work for the function Hello()
?
Golang allows you to name the return values of a function. We can also name the return value by defining variables, here a variable total of integer type is defined in the function declaration for the value that the function returns.
Basics. When you index a map in Go you get two return values; the second one (which is optional) is a boolean that indicates if the key exists. If the key doesn't exist, the first value will be the default zero value.
In Golang, we can return multiple values at a time from a single function. Multiple return values can be achieved by changing the return type of the function in the function signature. The (int, int) in this function signature explains that the return type is two integers.
Map() Function in Golang is used to return a copy of the string given string with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.
map
is different because it is a built-in type and not a function. The 2 forms of accessing an element of a map
is specified by the Go Language Specification: Index Expressions and backed by the compiler.
With functions you can't do this. If a function has 2 return values, you have to "expect" both of them or none at all.
However you are allowed to assign any of the return values to the Blank identifier:
s, b := Hello() // Storing both of the return values s2, _ := Hello() // Storing only the first _, b3 := Hello() // Storing only the second
You can also choose not to store any of the return values:
Hello() // Just executing it, but storing none of the return values
Note: you could also assign both of the return values to the blank identifier, although it has no use (other than validating that it has exactly 2 return values):
_, _ = Hello() // Storing none of the return values; note the = instead of :=
You can also try these on the Go Playground.
Helper function
If you use it many times and you don't want to use the blank identifier, create a helper function which discards the 2nd return value:
func Hello2() string { s, _ := Hello() return s }
And now you can do:
value := Hello2() fmt.Println(value)
In addition to the explanation of @icza:
Hello
function is your own function.You can also do this:
package main import "fmt" func Hello() (string, bool) { return "hello", true } func main() { // Just move it one line above: don't use a short-if value, ok := Hello() if ok { fmt.Println(value) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With