1 min read 208 words Updated Sep 24, 2026 Created Sep 24, 2026
#Go#Programming

Interface = set of method signatures

It basically groups types together, based on their methods.

type shape interface {
	area() float64
	circumf() float64
}

Better example of an interface:

package main

import "fmt"

type Man struct {
	Name string
	Age  int
}

type Woman struct {
	Name string
	Age  int
}
// Just a set of method signatures 
type Person interface {
	Greet() string
}

func (w Woman) Greet() string {
	return "Hello " + w.Name
}

func (m Man) Greet() string {
	return "Hello " + m.Name
}

// now we can create a method using the interface:
func GreetPerson(p Person) string {
	return p.Greet()
}

func main() {

	var max = Man{"Max", 22}
	var jane = Woman{"Jane", 26}

	jane.Greet()

	max.Greet()

	fmt.Println(GreetPerson(jane))

}

A type implements an interface just by implementing its methods. There is no need to do this explicitly:

type I interface {
	M()
}

type T struct {
	S string
}

// This method means type T implements the interface I,
// but we don't need to explicitly declare that it does so.
func (t T) M() {
	fmt.Println(t.S)
}

func main() {
	var i I = T{"hello"}
	i.M()
}

When to use interfaces

Next chapter: Defer