0
0
GoComparisonBeginner · 4 min read

Interface vs Struct in Go: Key Differences and Usage

In Go, a struct is a concrete data type used to group related fields, while an interface defines a set of method signatures without implementation. Structs hold data and implement behavior, whereas interfaces specify behavior that structs or other types must provide.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of interface and struct in Go.

AspectStructInterface
PurposeHolds data fieldsDefines method set (behavior)
TypeConcrete typeAbstract type
Can be instantiated?YesNo
Contains data?YesNo
Implements methods?Yes, with codeNo, only method signatures
Used forRepresent objects with dataSpecify behavior contracts
⚖️

Key Differences

Structs in Go are used to group related data together into one unit. They are concrete types, meaning you can create variables of that type and store actual data. Structs can also have methods attached to them, which define behavior related to the data.

Interfaces, on the other hand, are abstract types that only specify a set of method signatures without any implementation. They describe what methods a type must have but not how those methods work. Any type (often a struct) that implements all the methods of an interface is said to satisfy that interface.

This means interfaces are used to define behavior and enable polymorphism, allowing different types to be treated the same way if they implement the same interface. Structs are used to hold data and implement the behavior defined by interfaces or their own methods.

⚖️

Code Comparison

This example shows a struct defining a data type with a method.

go
package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func (p Person) Greet() string {
    return "Hello, my name is " + p.Name
}

func main() {
    p := Person{Name: "Alice", Age: 30}
    fmt.Println(p.Greet())
}
Output
Hello, my name is Alice
↔️

Interface Equivalent

This example shows an interface defining behavior and a struct implementing it.

go
package main

import "fmt"

type Greeter interface {
    Greet() string
}

type Person struct {
    Name string
    Age  int
}

func (p Person) Greet() string {
    return "Hello, my name is " + p.Name
}

func main() {
    var g Greeter
    g = Person{Name: "Alice", Age: 30}
    fmt.Println(g.Greet())
}
Output
Hello, my name is Alice
🎯

When to Use Which

Choose struct when you need to store and organize related data together and possibly add methods to operate on that data. Use interface when you want to define a set of behaviors that multiple types can implement, enabling flexible and reusable code through polymorphism.

In practice, structs hold your data and implement interfaces to provide specific behaviors. Interfaces let you write functions that work with any type that satisfies the interface, making your code more modular and testable.

Key Takeaways

Structs are concrete types that hold data and can have methods with implementations.
Interfaces define method sets without implementation to specify behavior contracts.
Use structs to represent objects with data and interfaces to enable polymorphism.
A struct implements an interface by providing all its methods.
Interfaces improve code flexibility by allowing different types to be used interchangeably.