The Golang Trinity: Functions, Methods, Interfaces

Published: (May 27, 2026 at 09:32 PM EDT)
2 min read
Source: Dev.to

Source: Dev.to

Functions

A function performs an operation with given inputs.

// Function
func Add(a, b int) int { return a + b }

Methods

A method is a function with a receiver, attaching behavior to a specific type.

type Counter struct{ val int }
// Method (receiver is the "attachment")

Interfaces

An interface defines a set of methods that a type must implement. Types satisfy an interface automatically—no explicit implements keyword or inheritance is needed.

type Stringer interface {
    // method signatures go here
}

type User struct{ Name string }
// User now implements Stringer automatically

func Print(s Stringer) { fmt.Println(s.String()) }

The Twist: Functions Can Be Interfaces

A function type can satisfy an interface by giving the function a method. This pattern often confuses people.

// An interface
type Handler interface {
    Handle(string)
}

// A function type
type HandleFunc func(string)

// The trick: attach the Handle method to the function type
func (hf HandleFunc) Handle(s string) { hf(s) }

// Now any function with signature func(string) works as a Handler

Putting It All Together

Combining functions, methods, and interfaces:

type Greeter interface { Greet() string }

type Person struct{ Name string }

type GreetFunc func() string

func (gf GreetFunc) Greet() string { return gf() }

func Party(g Greeter) { fmt.Println(g.Greet()) }

func main() {
    p := Person{Name: "Alice"}
    greet := GreetFunc(func() string { return "Hello, " + p.Name })
    Party(greet)
}

Summary

  • Functions orchestrate the flow of a program.
  • Methods define behavior on specific types.
  • Interfaces abstract the what from the how, allowing different types to be used interchangeably.

Your Turn

Drop your own examples or questions below 👇

0 views
Back to Blog

Related posts

Read more »

First Post: A Little Biography

Introduction Hello, my name is Jay. Growing up, I wanted to follow in my dad's footsteps and become an engineer—and I did, just not in the way I originally exp...