FAQ
Consider the following:

type DynMap struct {
Map map[string]interface{}
}

func (this *DynMap) Get(key string) (interface{}, bool) {
return this.Map[key]
}

gives compile error "not enough arguments to return"

Why?

Thanks much,
Dustin



--

Search Discussions

  • Patrick Mylund Nielsen at Dec 6, 2012 at 12:16 am
    it returns a variable number of arguments, e.g. x := this.Map[key] and x,
    found := this.Map[key]

    do

    func (this *DynMap) Get(key string) (interface{}, bool) {
    x, found := this.Map[key]
    return x, found
    }

    On Wed, Dec 5, 2012 at 8:19 PM, wrote:

    Consider the following:

    type DynMap struct {
    Map map[string]interface{}
    }

    func (this *DynMap) Get(key string) (interface{}, bool) {
    return this.Map[key]
    }

    gives compile error "not enough arguments to return"

    Why?

    Thanks much,
    Dustin



    --

    --
  • Jan Mercl at Dec 6, 2012 at 6:48 am

    On Wed, Dec 5, 2012 at 8:19 PM, wrote:
    type DynMap struct {
    Map map[string]interface{}
    }

    func (this *DynMap) Get(key string) (interface{}, bool) {
    return this.Map[key]
    }
    package main

    import "fmt"

    type DynMap map[string]interface{}

    func (d DynMap) Get(key string) (x interface{}, ok bool) {
    x, ok = d[key]
    return
    }

    func main() {
    d := DynMap{"hello": 42, "42": "world"}
    x, ok := d.Get("42")
    fmt.Println(x, ok)
    }

    (http://play.golang.org/p/9HCP-CE25f)

    However, note that the Get method gets used exactly in the same way as
    would a plain map be used:

    var m map[string]interface{}

    // ...

    x, ok := m["42"]

    i.e. in this case "Get" only introduces unnecessary overhead.

    -j

    --
  • Dave Cheney at Dec 6, 2012 at 7:01 am
    I don't see what value the getter adds, it's not even protecting the
    underlying map accessor

    http://play.golang.org/p/Nqz_On-RlU
    On Thu, Dec 6, 2012 at 5:47 PM, Jan Mercl wrote:
    On Wed, Dec 5, 2012 at 8:19 PM, wrote:
    type DynMap struct {
    Map map[string]interface{}
    }

    func (this *DynMap) Get(key string) (interface{}, bool) {
    return this.Map[key]
    }
    package main

    import "fmt"

    type DynMap map[string]interface{}

    func (d DynMap) Get(key string) (x interface{}, ok bool) {
    x, ok = d[key]
    return
    }

    func main() {
    d := DynMap{"hello": 42, "42": "world"}
    x, ok := d.Get("42")
    fmt.Println(x, ok)
    }

    (http://play.golang.org/p/9HCP-CE25f)

    However, note that the Get method gets used exactly in the same way as
    would a plain map be used:

    var m map[string]interface{}

    // ...

    x, ok := m["42"]

    i.e. in this case "Get" only introduces unnecessary overhead.

    -j

    --
    --
  • Rory McGuire at Dec 7, 2012 at 12:21 pm
    He is just asking why it doesn't work, I doubt that is the full code for
    what he is working on.

    One could argue that the only reason it doesn't work is because the
    compiler is not complex enough to realise that
    he requires both results from the built in m[k]
    On Thursday, 6 December 2012 09:01:49 UTC+2, Dave Cheney wrote:

    I don't see what value the getter adds, it's not even protecting the
    underlying map accessor

    http://play.golang.org/p/Nqz_On-RlU

    On Thu, Dec 6, 2012 at 5:47 PM, Jan Mercl <0xj...@gmail.com <javascript:>>
    wrote:
    On Wed, Dec 5, 2012 at 8:19 PM, <dus...@gmail.com <javascript:>>
    wrote:
    type DynMap struct {
    Map map[string]interface{}
    }

    func (this *DynMap) Get(key string) (interface{}, bool) {
    return this.Map[key]
    }
    package main

    import "fmt"

    type DynMap map[string]interface{}

    func (d DynMap) Get(key string) (x interface{}, ok bool) {
    x, ok = d[key]
    return
    }

    func main() {
    d := DynMap{"hello": 42, "42": "world"}
    x, ok := d.Get("42")
    fmt.Println(x, ok)
    }

    (http://play.golang.org/p/9HCP-CE25f)

    However, note that the Get method gets used exactly in the same way as
    would a plain map be used:

    var m map[string]interface{}

    // ...

    x, ok := m["42"]

    i.e. in this case "Get" only introduces unnecessary overhead.

    -j

    --
    --
  • Dustin Norlander at Dec 7, 2012 at 5:13 pm
    Thanks guys,

    Like Rory said, this is not production code, I just want to know why it
    doesn't work. I am still curious about the reasoning behind why you cant
    return multiple values in this way. Is it a deficiency in the compiler, or
    was this a specific design decision? Anyhow, have plenty of workarounds,
    so thanks

    -Dustin

    On Fri, Dec 7, 2012 at 4:21 AM, Rory McGuire wrote:

    He is just asking why it doesn't work, I doubt that is the full code for
    what he is working on.

    One could argue that the only reason it doesn't work is because the
    compiler is not complex enough to realise that
    he requires both results from the built in m[k]

    On Thursday, 6 December 2012 09:01:49 UTC+2, Dave Cheney wrote:

    I don't see what value the getter adds, it's not even protecting the
    underlying map accessor

    http://play.golang.org/p/Nqz_**On-RlU<http://play.golang.org/p/Nqz_On-RlU>
    On Thu, Dec 6, 2012 at 5:47 PM, Jan Mercl wrote:
    On Wed, Dec 5, 2012 at 8:19 PM, wrote:
    type DynMap struct {
    Map map[string]interface{}
    }

    func (this *DynMap) Get(key string) (interface{}, bool) {
    return this.Map[key]
    }
    package main

    import "fmt"

    type DynMap map[string]interface{}

    func (d DynMap) Get(key string) (x interface{}, ok bool) {
    x, ok = d[key]
    return
    }

    func main() {
    d := DynMap{"hello": 42, "42": "world"}
    x, ok := d.Get("42")
    fmt.Println(x, ok)
    }

    (http://play.golang.org/p/**9HCP-CE25f<http://play.golang.org/p/9HCP-CE25f>)
    However, note that the Get method gets used exactly in the same way as
    would a plain map be used:

    var m map[string]interface{}

    // ...

    x, ok := m["42"]

    i.e. in this case "Get" only introduces unnecessary overhead.

    -j

    --
    --
  • Ian Lance Taylor at Dec 7, 2012 at 9:18 pm

    On Fri, Dec 7, 2012 at 9:12 AM, Dustin Norlander wrote:
    Like Rory said, this is not production code, I just want to know why it
    doesn't work. I am still curious about the reasoning behind why you cant
    return multiple values in this way. Is it a deficiency in the compiler, or
    was this a specific design decision?
    It is a specific design decision. Consider

    func F(m1, m2 map[string]bool) (bool, bool, bool) {
    return m1["a"], m2["b"]
    }

    We could write a rule explaining which map fetch returns two values
    and which returns one, but I think the result could only be described
    as confusing. Or we could write a rule saying that this is only OK
    when the map fetch is the only value being returned, but that seems
    like a special case that adds little value.

    Ian

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 5, '12 at 11:53p
activeDec 7, '12 at 9:18p
posts7
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase