How does data flow when using interface

I have written a Go program while I was learning interface
Here is the program:

package main

import (

type person struct {
name string
age int

func (p *person) speak() {
// we have to use & p.age instead of name and age
// to let compiler know that speak wants name and age
// value from person type
// receiver are given to attach a method to a type so that
// the method can use it’s identifiers
// it can be of pointer type and non pointer type
fmt.Printf(“My name is %s and I am %d years old\n”,, p.age)

type human interface {

func saySomething(h human) {

func main() {
p1 := person{
name: “James Bond”,
age: 32,
// We have to pass the address of person type variable because
// the speak method which is attached to a pointer type person
// expects the address of person type identifier
saySomething(&p1) // this will work
//saySomething(p1) // this won’t work

i want to know how does interface works, i mean how does the compiler reads it.
I have tried many time to read it my self. But i always get stuck at interface.

Take the Tour of Go, especially the part about Methods. In this part interfaces are explained.

Maybe also worth a look:

1 Like

Thanks, it helped a lot. :grinning:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.