object
For object
like types, Go has the struct
type:
type Person struct { Name string Age int}func main() { p := Person{Name: "John", Age: 30} fmt.Println(p.Name) // John fmt.Println(p.Age) // 30}
Like with normal variables, each field in a struct is initialized to its zero value if not explicitly set:
type Person struct { Name string Age int}func main() { p := Person{} // No fields set fmt.Println(p.Name) // "" - zero value of string fmt.Println(p.Age) // 0 - zero value of int}
Methods
Section titled “Methods”Structs can have methods associated with them, similar to classes in JavaScript/TypeScript:
type Person struct { Name string Age int}func (p Person) Greet() string { return "Hello, " + p.Name}func main() { p := Person{Name: "John", Age: 30} fmt.Println(p.Greet()) // Hello, John}
type Person struct { Name string Age int}func (p *Person) Birthday() { p.Age++}
func main() { p := Person{Name: "John", Age: 30} p.Birthday() fmt.Println(p.Age) // 31}
Note the *
before Person
in the method signature. This indicates that p
is a pointer to a Person
struct. This allows you to modify the original struct when calling the method.
Without it, Go would create a copy of the struct and any modifications would not affect the original struct.
In this case, the Birthday
method receives a copy of p
, increments the age of the copy, and then discards it. The original p
remains unchanged.