|
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_errorRoss Light | Software Engineer |
[email protected]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
[email protected].
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
[email protected].
For more options, visit
https://groups.google.com/groups/opt_out.