Go Tutorial #7: Structs, Methods, and Composition
In the previous tutorial, you learned about slices and maps. Now it is time to learn how to create your own types with structs. Go does not have classes. Instead, it uses structs and methods. Structs hold data, and methods add behavior. This is simpler than class-based languages like Java or Python. Defining a Struct A struct groups related fields together: package main import "fmt" type User struct { Name string Email string Age int } func main() { // Create a struct with field names user1 := User{ Name: "Alex", Email: "alex@example.com", Age: 25, } fmt.Println(user1) // Access fields with dot notation fmt.Println("Name:", user1.Name) fmt.Println("Email:", user1.Email) // Update a field user1.Age = 26 fmt.Println("New age:", user1.Age) } Output: ...