FAQ
Hi,

here's some code (go 1.0.3), which does not do what I expect:

package main

import "fmt"

type TestError struct {
}

// satisfy the error interface:
func (e *TestError) Error() string {
return "some error message"
}

func doSomething() (string, *TestError) {
return "something", nil
}

func main() {
var err error

a, err := doSomething()
_ = a

if err != nil {
fmt.Println("not nil")
return
}
fmt.Println("nil")
}

Since I return nil as second return value from doSomething(), I would
expect that the output is "nil". But it isn't. It prints "not nil". If I
leave out the declaration "var err error", it prints "nil". Or, if I
declare doSomething as "func doSomething() (string, error)", it prints
"nil", too. The difference seems to be that the type of the returned value
is *TestError and the err variable is of type error. But I don't understand
why this matters, because *TestError satifies the error interface. Am I
missing something?

Regards,
Sven

--
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

  • Rob Pike at Mar 2, 2013 at 4:24 pm
    http://golang.org/doc/faq#t_and_equal_interface

    --
    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.
  • Rob Pike at Mar 2, 2013 at 4:25 pm
    I mean, http://golang.org/doc/faq#nil_error

    --
    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.
  • Sven Steinseifer at Mar 2, 2013 at 4:58 pm
    Thank you - and sorry for not checking the faq!

    Sven

    Am Samstag, 2. März 2013 16:39:26 UTC+1 schrieb sven.ste...@googlemail.com:
    Hi,

    here's some code (go 1.0.3), which does not do what I expect:

    package main

    import "fmt"

    type TestError struct {
    }

    // satisfy the error interface:
    func (e *TestError) Error() string {
    return "some error message"
    }

    func doSomething() (string, *TestError) {
    return "something", nil
    }

    func main() {
    var err error

    a, err := doSomething()
    _ = a

    if err != nil {
    fmt.Println("not nil")
    return
    }
    fmt.Println("nil")
    }

    Since I return nil as second return value from doSomething(), I would
    expect that the output is "nil". But it isn't. It prints "not nil". If I
    leave out the declaration "var err error", it prints "nil". Or, if I
    declare doSomething as "func doSomething() (string, error)", it prints
    "nil", too. The difference seems to be that the type of the returned value
    is *TestError and the err variable is of type error. But I don't understand
    why this matters, because *TestError satifies the error interface. Am I
    missing something?

    Regards,
    Sven
    --
    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
postedMar 2, '13 at 4:22p
activeMar 2, '13 at 4:58p
posts4
users2
websitegolang.org

2 users in discussion

Rob Pike: 2 posts Sven Steinseifer: 2 posts

People

Translate

site design / logo © 2022 Grokbase