Go Interfaces Type Assertion


You can access the real data type of interface with Type Assertion.


Example of struct MyType which implement interface Subber:

package main

import (

type Subber interface {
    Sub(a, b int) int

type MyType struct {
    Msg string
//Implement method Sub(a,b int) int
func (m *MyType) Sub(a, b int) int {
    m.Msg = "SUB!!!"

    return a - b;

func main() {
    var interfaceVar Subber = &MyType{}

Without .(*MyType) we wouldn't able to access Msg Field. If we try interfaceVar.Msg it will show compile error:

interfaceVar.Msg undefined (type Subber has no field or method Msg)