Determining underlying type from interface

suggest change

In go it can sometimes be useful to know which underlying type you have been passed. This can be done with a type switch. This assumes we have two structs:

type Rembrandt struct{}

func (r Rembrandt) Paint() {}

type Picasso struct{}

func (r Picasso) Paint() {}

That implement the Painter interface:

type Painter interface {

Then we can use this switch to determine the underlying type:

func WhichPainter(painter Painter) {
    switch painter.(type) {
    case Rembrandt:
        fmt.Println("The underlying type is Rembrandt")
    case Picasso:
        fmt.Println("The underlying type is Picasso")
        fmt.Println("Unknown type")

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents