create a struct with a field of type pointer in go

To create a struct with a field of type pointer in Go, you can define a field with the * symbol, followed by the type you want the pointer to point to. Here's an example:

main.go
type Person struct {
    Name string
    Age  int
    Address *Address
}

type Address struct {
    Street string
    City   string
    State  string
    Zip    string
}
170 chars
13 lines

In this example, the Person struct has a field called Address which is defined as a pointer to the Address struct. This allows us to create a Person object and assign a different Address object to each person as needed.

related categories

gistlibby LogSnag