FAQ
Hello folks,

I'm trying to create an error value using reflect such that the value is ==
nil. Like this:

package main

import (
"errors"
"fmt"
"reflect"
)

func main() {
err := reflect.Zero(reflect.TypeOf(errors.New("An
error"))).Interface().(error)
fmt.Println(err, err == nil)
}

Using Go 1.1, it outputs

<nil> false

(It's curious that the value prints as <nil>, yet is != nil).

Does anyone know if and how I can create a reflect.Value with its runtime
dynamic type to be equal to nil when type-asserted into an error? (or when
passed to a function expecting an error argument using reflect.Call?)

Any help would really be appreciated!

Cheers,
Marcus

--
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/groups/opt_out.

Search Discussions

  • Ross Light at Sep 16, 2013 at 4:00 pm
    Slightly modified your snippet to show the problem:
    http://play.golang.org/p/WyzaqCa2Y-

    Explanation: http://golang.org/doc/faq#nil_error


    Ross Light | Software Engineer | light@google.com

    On Sun, Sep 15, 2013 at 10:43 PM, Marcus Westin wrote:

    Hello folks,

    I'm trying to create an error value using reflect such that the value is
    == nil. Like this:

    package main

    import (
    "errors"
    "fmt"
    "reflect"
    )

    func main() {
    err := reflect.Zero(reflect.TypeOf(errors.New("An
    error"))).Interface().(error)
    fmt.Println(err, err == nil)
    }

    Using Go 1.1, it outputs

    <nil> false

    (It's curious that the value prints as <nil>, yet is != nil).

    Does anyone know if and how I can create a reflect.Value with its runtime
    dynamic type to be equal to nil when type-asserted into an error? (or when
    passed to a function expecting an error argument using reflect.Call?)

    Any help would really be appreciated!

    Cheers,
    Marcus

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Marcus Westin at Sep 16, 2013 at 4:12 pm
    Ross, thanks for the clarifying link (http://golang.org/doc/faq#nil_error)




    The problem still remains.




    Jan: because I need to call functions with different argument signatures using reflect.Call. All the functions take an error as their last argument, and I wish to call be able to have the error argument == nil.




    Anyone? Is it not doable?

    -- while mobile
    On Mon, Sep 16, 2013 at 8:59 AM, Ross Light wrote:

    Slightly modified your snippet to show the problem:
    http://play.golang.org/p/WyzaqCa2Y-
    Explanation: http://golang.org/doc/faq#nil_error
    Ross Light | Software Engineer | light@google.com
    On Sun, Sep 15, 2013 at 10:43 PM, Marcus Westin wrote:
    Hello folks,

    I'm trying to create an error value using reflect such that the value is
    == nil. Like this:

    package main

    import (
    "errors"
    "fmt"
    "reflect"
    )

    func main() {
    err := reflect.Zero(reflect.TypeOf(errors.New("An
    error"))).Interface().(error)
    fmt.Println(err, err == nil)
    }

    Using Go 1.1, it outputs

    <nil> false

    (It's curious that the value prints as <nil>, yet is != nil).

    Does anyone know if and how I can create a reflect.Value with its runtime
    dynamic type to be equal to nil when type-asserted into an error? (or when
    passed to a function expecting an error argument using reflect.Call?)

    Any help would really be appreciated!

    Cheers,
    Marcus

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Marcus Westin at Sep 16, 2013 at 5:28 pm
    Thanks for everyone's help. This is what I've ended up with:

    package main

    import (
    "errors"
    "fmt"
    "reflect"
    )

    func main() {
    nilError := reflect.Zero(reflect.TypeOf(&Error{}))
    nonNilError := reflect.ValueOf(&Error{err: errors.New("A non-nil error")})
    reflect.ValueOf(takesNilErrors).Call([]reflect.Value{nilError, nonNilError})
    }

    func takesNilErrors(nilError *Error, nonNilError *Error) {
    fmt.Println("Wanted:", true, false)
    fmt.Println("Actual:", nilError == nil, nonNilError == nil)
    }

    type Error struct {
    err error
    }

    func (e *Error) Error() string {
    return e.err.Error()
    }

    It's not ideal, but it gets the job done.

    Thanks for the help everyone!
    On Monday, September 16, 2013 9:12:07 AM UTC-7, Marcus Westin wrote:

    Ross, thanks for the clarifying link (*http://golang.org/doc/faq#nil_error
    *)

    The problem still remains.

    Jan: because I need to call functions with different argument signatures
    using reflect.Call. All the functions take an error as their last argument,
    and I wish to call be able to have the error argument == nil.

    Anyone? Is it not doable?
    -- while mobile

    On Mon, Sep 16, 2013 at 8:59 AM, Ross Light wrote:

    Slightly modified your snippet to show the problem:
    http://play.golang.org/p/WyzaqCa2Y-

    Explanation: http://golang.org/doc/faq#nil_error


    Ross Light | Software Engineer | light@google.com

    On Sun, Sep 15, 2013 at 10:43 PM, Marcus Westin wrote:

    Hello folks,

    I'm trying to create an error value using reflect such that the value is
    == nil. Like this:

    package main

    import (
    "errors"
    "fmt"
    "reflect"
    )

    func main() {
    err := reflect.Zero(reflect.TypeOf(errors.New("An
    error"))).Interface().(error)
    fmt.Println(err, err == nil)
    }

    Using Go 1.1, it outputs

    <nil> false

    (It's curious that the value prints as <nil>, yet is != nil).

    Does anyone know if and how I can create a reflect.Value with its
    runtime dynamic type to be equal to nil when type-asserted into an error?
    (or when passed to a function expecting an error argument using
    reflect.Call?)

    Any help would really be appreciated!

    Cheers,
    Marcus

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Rémy Oudompheng at Sep 16, 2013 at 5:47 pm

    2013/9/16 Marcus Westin wrote:
    Thanks for everyone's help. This is what I've ended up with:

    package main

    import (
    "errors"
    "fmt"
    "reflect"
    )

    func main() {
    nilError := reflect.Zero(reflect.TypeOf(&Error{}))
    nonNilError := reflect.ValueOf(&Error{err: errors.New("A non-nil error")})
    reflect.ValueOf(takesNilErrors).Call([]reflect.Value{nilError, nonNilError})
    }

    func takesNilErrors(nilError *Error, nonNilError *Error) {
    fmt.Println("Wanted:", true, false)
    fmt.Println("Actual:", nilError == nil, nonNilError == nil)
    }

    type Error struct {
    err error
    }

    func (e *Error) Error() string {
    return e.err.Error()
    }

    It's not ideal, but it gets the job done.

    Thanks for the help everyone!
    On Monday, September 16, 2013 9:12:07 AM UTC-7, Marcus Westin wrote:

    Ross, thanks for the clarifying link (http://golang.org/doc/faq#nil_error)

    The problem still remains.

    Jan: because I need to call functions with different argument signatures
    using reflect.Call. All the functions take an error as their last argument,
    and I wish to call be able to have the error argument == nil.

    Anyone? Is it not doable?
    -- while mobile
    I would have done this:
    http://play.golang.org/p/GhxBiEIyfX

    The program prints "hello".

    package main

    import (
         "fmt"
         "reflect"
    )

    func F(s string, err error) {
         if err == nil {
             fmt.Println(s)
         }
    }

    var nilerror = error(nil)

    func main() {
         f := reflect.ValueOf(F)
         h := reflect.ValueOf("hello")
         n := reflect.ValueOf(&nilerror).Elem()
         f.Call([]reflect.Value{h, n})
    }

    --
    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/groups/opt_out.
  • Marcus Westin at Sep 16, 2013 at 5:53 pm
    Aha! Yes, that's it!

    Thank you Remy, really helpful!

    *Marking as resolved*
    On Monday, September 16, 2013 10:46:57 AM UTC-7, Rémy Oudompheng wrote:

    2013/9/16 Marcus Westin <marcus...@gmail.com <javascript:>> wrote:
    Thanks for everyone's help. This is what I've ended up with:

    package main

    import (
    "errors"
    "fmt"
    "reflect"
    )

    func main() {
    nilError := reflect.Zero(reflect.TypeOf(&Error{}))
    nonNilError := reflect.ValueOf(&Error{err: errors.New("A non-nil error")})
    reflect.ValueOf(takesNilErrors).Call([]reflect.Value{nilError,
    nonNilError})
    }

    func takesNilErrors(nilError *Error, nonNilError *Error) {
    fmt.Println("Wanted:", true, false)
    fmt.Println("Actual:", nilError == nil, nonNilError == nil)
    }

    type Error struct {
    err error
    }

    func (e *Error) Error() string {
    return e.err.Error()
    }

    It's not ideal, but it gets the job done.

    Thanks for the help everyone!
    On Monday, September 16, 2013 9:12:07 AM UTC-7, Marcus Westin wrote:

    Ross, thanks for the clarifying link (
    http://golang.org/doc/faq#nil_error)
    The problem still remains.

    Jan: because I need to call functions with different argument
    signatures
    using reflect.Call. All the functions take an error as their last
    argument,
    and I wish to call be able to have the error argument == nil.

    Anyone? Is it not doable?
    -- while mobile
    I would have done this:
    http://play.golang.org/p/GhxBiEIyfX

    The program prints "hello".

    package main

    import (
    "fmt"
    "reflect"
    )

    func F(s string, err error) {
    if err == nil {
    fmt.Println(s)
    }
    }

    var nilerror = error(nil)

    func main() {
    f := reflect.ValueOf(F)
    h := reflect.ValueOf("hello")
    n := reflect.ValueOf(&nilerror).Elem()
    f.Call([]reflect.Value{h, n})
    }
    --
    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/groups/opt_out.
  • Steven Blenkinsop at Sep 16, 2013 at 7:35 pm

    On Monday, September 16, 2013, Rémy Oudompheng wrote:
    I would have done this:
    http://play.golang.org/p/GhxBiEIyfX

    The program prints "hello".

    package main

    import (
    "fmt"
    "reflect"
    )

    func F(s string, err error) {
    if err == nil {
    fmt.Println(s)
    }
    }

    var nilerror = error(nil)

    func main() {
    f := reflect.ValueOf(F)
    h := reflect.ValueOf("hello")
    n := reflect.ValueOf(&nilerror).Elem()
    f.Call([]reflect.Value{h, n})
    }

    Or you could have:
    var nilerror = reflect.ValueOf(new(error)).Elem()
    and skip making a new reflect value each time. But that's just nitpicking.
    I'm not sure if it really makes any real difference one way or another.

    --
    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/groups/opt_out.
  • Jan Mercl at Sep 16, 2013 at 4:02 pm

    On Mon, Sep 16, 2013 at 7:43 AM, Marcus Westin wrote:
    Hello folks,

    I'm trying to create an error value using reflect such that the value is ==
    nil. Like this:
    Why via reflection?

    http://play.golang.org/p/t0zoD8-FVJ

    -j

    --
    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/groups/opt_out.
  • Roger peppe at Sep 16, 2013 at 4:11 pm

    On 16 September 2013 06:43, Marcus Westin wrote:
    Does anyone know if and how I can create a reflect.Value with its runtime
    dynamic type to be equal to nil when type-asserted into an error? (or when
    passed to a function expecting an error argument using reflect.Call?)
    Interface types and nil don't play easily together with reflect (see issue 6231
    for an example).

    To create a nil reflect.Value with type error:

    errValue := reflect.Zero(reflect.TypeOf((*error)(nil)).Elem())

    But you still can't assert its interface value as error, because the type
    is lost when converting to interface{}.

    In general, you need to do something like this:

    var err error
    if i := errValue.Interface().(error); i != nil {
         err = i
    }

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 16, '13 at 3:22p
activeSep 16, '13 at 7:35p
posts9
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase