Welcome To Golang By Example

Print underlying type and value of an interface in Go (Golang)

Table of Contents

Overview

Golang provides format identifiers to print the underlying type and underlying value represented by the interface value.

Assume we have an interface animal as below

type animal interface {
    breathe()
    walk()
}

We also have a lion struct implementing this animal interface

type lion struct {
    age int
}

Code

package main

import "fmt"

type animal interface {
    breathe()
    walk()
}

type lion struct {
    age int
}

func (l lion) breathe() {
    fmt.Println("Lion breathes")
}

func (l lion) walk() {
    fmt.Println("Lion walk")
}

func main() {
    var a animal
    a = lion{age: 10}
    fmt.Printf("Underlying Type: %T\n", a)
    fmt.Printf("Underlying Value: %v\n", a)
}

Output

Concrete Type: main.lion
Concrete Value: {10}