In this tutorial, we will explore structs in the Go programming language. Structs allow us to create our own complex types that aggregate together values of other types. They are the building blocks for creating objects and classes in Go.
What you will learn:
- What are structs in Go?
- How to define and create structs.
- How to define methods on structs.
Prerequisites:
- Basic understanding of the Go programming language.
- Familiarity with Go's basic data types (int, float, string, etc.).
A struct in Go is a user-defined type which allows us to group/combine items of possibly different types into a single type. You can consider a struct a lightweight class that supports encapsulation but lacks inheritance.
To define a struct, we use the type keyword, followed by the name of the struct, the struct keyword, and a pair of curly braces {}. Inside the braces, we define the fields of the struct, each on its own line and followed by its type.
For example:
type Person struct {
Name string
Age int
}
Here, Person is a struct that contains two fields: Name of type string, and Age of type int.
To create a new instance of a struct, we use the Person type followed by a pair of curly braces {}. Inside the braces, we initialize each field with a value.
For example:
p := Person{
Name: "John",
Age: 30,
}
fmt.Println(p) // prints: {John 30}
In Go, you can define methods on user-defined types. A method is a function that has a special receiver argument. The receiver appears in its own argument list between the func keyword and the method name.
For example:
func (p Person) SayHello() {
fmt.Println("Hello, my name is", p.Name)
}
Here, SayHello is a method on the Person struct. We can call this method on instances of Person:
p := Person{Name: "John", Age: 30}
p.SayHello() // prints: Hello, my name is John
Let's go through a few practical examples.
// Define a new struct type
type Employee struct {
ID int
Name string
Position string
}
// Create an instance of Employee
e := Employee{
ID: 1,
Name: "John Doe",
Position: "Developer",
}
// Access fields in the struct
fmt.Println(e.Name) // prints: John Doe
// Define a new struct type
type Rectangle struct {
Width float64
Height float64
}
// Define a method to calculate the area of the rectangle
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
// Create an instance of Rectangle
r := Rectangle{Width: 10, Height: 20}
// Call the Area method
fmt.Println(r.Area()) // prints: 200
In this tutorial, you learned:
Next, you could explore more about how to use pointers with structs, anonymous structs, and nested structs.
Define a Circle struct with a Radius field. Then, define a method Area that calculates the area of the circle (hint: Area = πr^2).
Define a Book struct with Title, Author, and Pages fields. Then, define a method Summary that returns a string summary of the book.
Define a Triangle struct with Base and Height fields. Then, define a method Area that calculates the area of the triangle (hint: Area = 0.5 * Base * Height).