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

1 comments

@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

2 Answered Questions

[SOLVED] In Go, using a setter for a struct type does not work as anticipated

  • 2011-06-25 06:51:08
  • cc young
  • 761 View
  • 6 Score
  • 2 Answer
  • Tags:   struct go

14 Answered Questions

[SOLVED] How to return multiple values from a function?

5 Answered Questions

[SOLVED] Why can't I put the opening braces on the next line?

  • 2011-08-15 06:39:21
  • Anuj Verma
  • 640 View
  • 1 Score
  • 5 Answer
  • Tags:   error-handling go

2 Answered Questions

[SOLVED] How to define a function type which accepts any number of arguments in Go?

  • 2011-05-18 15:48:57
  • ceving
  • 1220 View
  • 6 Score
  • 2 Answer
  • Tags:   go

2 Answered Questions

[SOLVED] Golang: Where is an Interface method called?

  • 2015-12-11 17:57:50
  • Kosie
  • 400 View
  • 9 Score
  • 2 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] how to convert java annotation to go code?

  • 2015-09-15 14:01:54
  • Sandeep Jaluthairya
  • 1031 View
  • 2 Score
  • 1 Answer
  • Tags:   java go

1 Answered Questions

how to get skewness value from array in golang

4 Answered Questions

[SOLVED] Go map of functions

  • 2011-07-20 21:54:12
  • Conceited Code
  • 25266 View
  • 38 Score
  • 4 Answer
  • Tags:   map go key-value func

2 Answered Questions

1 Answered Questions

[SOLVED] golang - function in map is <nil>

  • 2015-01-10 03:59:11
  • Melbourne2991
  • 701 View
  • 0 Score
  • 1 Answer
  • Tags:   go

Sponsored Content