0
0
GoComparisonBeginner · 4 min read

Struct vs Class in Go: Key Differences and Usage

Go does not have class types like some other languages; instead, it uses struct to define custom data types. Structs hold data fields, and methods can be attached to them to provide behavior, effectively replacing classes in Go.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of struct in Go and the concept of class in other languages.

FeatureStruct in GoClass in Other Languages
DefinitionComposite data type grouping fieldsBlueprint for objects with fields and methods
MethodsCan attach methods to structsMethods defined inside class body
InheritanceNo inheritance, uses compositionSupports inheritance and polymorphism
EncapsulationExported/unexported fields control visibilityAccess modifiers like private/public/protected
InstantiationCreate instances with struct literalsCreate objects with constructors
MemoryValue type, copied on assignment unless pointer usedReference type, objects accessed via references
⚖️

Key Differences

Go uses struct as its primary way to group data fields together. Unlike class in languages like Java or C++, Go structs do not support inheritance. Instead, Go encourages composition by embedding structs within other structs to reuse code.

Methods in Go are defined separately but can be attached to structs, giving them behavior similar to classes. However, Go does not have access modifiers like private or protected; instead, it uses capitalization to control visibility: fields or methods starting with a capital letter are exported (public), while lowercase means unexported (private).

Another key difference is that structs are value types in Go, meaning when you assign a struct to a new variable, it copies the data. To share data or modify the original, you use pointers to structs. Classes in other languages are usually reference types, so variables hold references to the same object.

⚖️

Code Comparison

This Go code shows a struct with fields and a method attached to it.

go
package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func (p Person) Greet() {
    fmt.Printf("Hello, my name is %s and I am %d years old.\n", p.Name, p.Age)
}

func main() {
    p := Person{Name: "Alice", Age: 30}
    p.Greet()
}
Output
Hello, my name is Alice and I am 30 years old.
↔️

Class Equivalent in Other Languages

Here is a similar example in a language with class, like Java, showing how a class holds data and methods.

java
public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void greet() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }

    public static void main(String[] args) {
        Person p = new Person("Alice", 30);
        p.greet();
    }
}
Output
Hello, my name is Alice and I am 30 years old.
🎯

When to Use Which

In Go, always use struct to define your data types and attach methods to them for behavior. Since Go does not have classes, embrace composition over inheritance by embedding structs to share functionality.

Choose structs when you want simple, efficient data grouping with clear visibility rules. If you come from a class-based language, think of Go structs plus methods as your class equivalent, but without inheritance and with value semantics by default.

Key Takeaways

Go uses structs with attached methods instead of classes.
Structs are value types; use pointers to share or modify data.
Go has no inheritance; use composition by embedding structs.
Visibility is controlled by capitalization, not access modifiers.
Use structs for data grouping and behavior in Go programs.