0

i'm a new gopher and got really confused by variable types, if i'm defining a custom error

    import (
        "fmt"
        "reflect"
    )

    // custom errors
    type myError struct {
        msg string
    }

    func (m *myError) Error() string {
        return m.msg
    }

    func errorGen() error {
        return &myError{"custom error"}
    }

generate a new error and check the type of it

    func main() {
        e := errorGen()
        fmt.Println(reflect.TypeOf(e).Kind()) // type = pointer

        // first type assertion
        _, ok := e.(error)
        if ok {
            fmt.Println("type assertion error") // type = error
        }

        // second type assertion
        _, ok = e.(*myError)
        if ok {
            fmt.Println("type assertion *myError") // type = pointer
        }
    }

so in above code variable 'e' shows 2 types at the same time. What exactly is e's type? and why "error" is an interface and also can be used as return type?

thank you very much

2 Answers 2

4

You have to differentiate between the "static type" and the "dynamic type" of a variable.

Each and every variable in Go has exactly one static type. Some example:

  • After declaring a := 5 the static type of a is int and that is all to know about a.
  • The static type of e in your code is error(because that is what errorGen returns).
  • You can be explicit about the static type like this: var b uint16 = 9.

Now there are interface types in Go. error is such an interface type, see Peter's answer. Some variables will have error as their static type; your e is an example. Now, the whole purpose of a variable of interface type is to store values of various types which implement this interface. So a interface variable which is not nil somehow "contains" an other variable (actually a value). The type of this contained value can be any one which implements the interface. The type of the contained value is the "dynamic type". Type assertion let you extract values of this dynamic type.

Sign up to request clarification or add additional context in comments.

2 Comments

why e's statistic type is error not a pointer considering errorGen is returning an address
Take a look at errorGen func errorGen() error it returns an error. The type returned is the type in the function signature. The function body stores a *myError inside a error and that error is returned. The return type of a function is determined only by the function signature. Whatever you write after return is of no importance for the static return type (as long as it compiles).
1

The Go Programming Language Specification

Errors

The predeclared type error is defined as

type error interface {
    Error() string
}

It is the conventional interface for representing an error condition, with the nil value representing no error.

Interface types

An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface.


e implements the predeclared interface type error.

    // first type assertion
    _, ok := e.(error)
    if ok {
        fmt.Println("type assertion error") // type = error
    }

e's concrete type is a pointer to type myError.

    // second type assertion
    _, ok = e.(*myError)
    if ok {
        fmt.Println("type assertion *myError") // type = pointer
    }

Both type assertions are true (ok is true).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.