FAQ
Hi guys!

I have a weird problem. At least, I'm not used to see this happening in
other languages, so hence my question :)

I have a function, which returns a *[]interface{}*, and in the end of that
function, if the slice is empty, I would like instead to return *nil*. The
problem is, I'm not getting *nil* in the caller function. I'm placing
example code:

func getChildren(paramStuff string) []interface{} {
    var values []interface{}
    // Do Stuff
    if len(values) == 0 {
       fmt.Println("Returning nil")
       return nil
    }
    return values
}

func stuff() {
    fmt.Printf("%+v", getChildren("params"))
}

The output of this code, is:



*Returning nil[]*I was expecting to have *nil* as a return value, instead
of an empty array. Is this expected behaviour?

Thanks!

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jérôme Champion at Jun 5, 2015 at 12:50 pm
    It does return null, it's just the way Printf work:


    Le vendredi 5 juin 2015 14:38:12 UTC+2, João Gonçalves a écrit :
    Hi guys!

    I have a weird problem. At least, I'm not used to see this happening in
    other languages, so hence my question :)

    I have a function, which returns a *[]interface{}*, and in the end of
    that function, if the slice is empty, I would like instead to return *nil*.
    The problem is, I'm not getting *nil* in the caller function. I'm placing
    example code:

    func getChildren(paramStuff string) []interface{} {
    var values []interface{}
    // Do Stuff
    if len(values) == 0 {
    fmt.Println("Returning nil")
    return nil
    }
    return values
    }

    func stuff() {
    fmt.Printf("%+v", getChildren("params"))
    }

    The output of this code, is:



    *Returning nil[]*I was expecting to have *nil* as a return value, instead
    of an empty array. Is this expected behaviour?

    Thanks!
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nigel Tao at Jun 5, 2015 at 11:16 pm

    On Fri, Jun 5, 2015 at 8:44 PM, João Gonçalves wrote:
    I was expecting to have nil as a return value, instead of an empty array.
    A nil slice is a valid (empty) slice: it has length 0, you can range
    over it (0 times), append to it, etc.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 5, '15 at 12:38p
activeJun 5, '15 at 11:16p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase