Struct vs Class in Go: Key Differences and Usage
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.
| Feature | Struct in Go | Class in Other Languages |
|---|---|---|
| Definition | Composite data type grouping fields | Blueprint for objects with fields and methods |
| Methods | Can attach methods to structs | Methods defined inside class body |
| Inheritance | No inheritance, uses composition | Supports inheritance and polymorphism |
| Encapsulation | Exported/unexported fields control visibility | Access modifiers like private/public/protected |
| Instantiation | Create instances with struct literals | Create objects with constructors |
| Memory | Value type, copied on assignment unless pointer used | Reference 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.
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() }
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.
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(); } }
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.