By Michael Wasser


2015-02-12 20:21:46 8 Comments

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()?

2 comments

@Inanc Gumus 2019-02-23 09:48:02

In addition to the explanation of @icza:

  • I don't recommend using a helper function there. Especially if the Hello function is your own function.
  • However, if you can't control it, then it's fine to use a helper.
  • If it's your own function, it's better to change the signature of your function. Probably, you made a design mistake somewhere.

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)
    }
}

@icza 2015-02-12 20:35:31

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.

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)

@Michael Wasser 2015-02-12 20:53:18

I had suspected as much, but wasn't sure as I had trouble googling for the docs / a definitive answer. Thanks for the reference to the docs as well!

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Accessing a map using its reference

14 Answered Questions

[SOLVED] How do I return multiple values from a function?

1 Answered Questions

[SOLVED] Function call in golang channel

  • 2018-10-18 10:09:55
  • fantasitcalbeastly
  • 193 View
  • -4 Score
  • 1 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] Map of strings and generic functions in Golang

  • 2018-10-10 10:37:05
  • Lucas Beier
  • 238 View
  • -1 Score
  • 1 Answer
  • Tags:   go

3 Answered Questions

[SOLVED] Golang return child struct in place of parent like in c++

  • 2017-03-03 07:01:23
  • Sakib
  • 1108 View
  • 0 Score
  • 3 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] Golang: Why Is Catching Certain Function Return Values Optional?

  • 2016-11-29 19:48:38
  • PressingOnAlways
  • 184 View
  • 1 Score
  • 1 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] Golang when trying to make method concurrent nothing returns

  • 2016-07-11 03:58:45
  • user1591668
  • 71 View
  • 1 Score
  • 1 Answer
  • Tags:   go

8 Answered Questions

[SOLVED] How to elegantly ignore some return values of a MATLAB function?

2 Answered Questions

[SOLVED] Is it possible to "return" a function from another function?

  • 2014-07-03 05:30:01
  • hey
  • 101 View
  • 2 Score
  • 2 Answer
  • Tags:   go

Sponsored Content