In Go you create an object by declaring a struct and usually taking its address with &T{...}, then manipulate it through methods with pointer receivers. Pointers let several parts of a program share and modify the same value, and they avoid copying large structs when you pass them around.
Go has no classes, so the pattern is struct plus methods plus a constructor function. This article shows how to create, share and modify objects safely, and where pointers can cause bugs.
There are three common ways to get a pointer to a new struct:
&User{...} is the idiomatic form because it sets fields at creation. new(T) returns a pointer to a zeroed T. Go auto-dereferences, so a.Name is the same as (*a).Name. Whether the value lives on the stack or heap is decided by the compiler's escape analysis, not by the syntax.
Wrap creation in a NewT function to validate input and set defaults. Methods that change state use pointer receivers:
Unexported fields plus a constructor keep invariants in one place. Use pointer receivers on all methods of a type when any of them mutates it, so the method set stays consistent.
Passing a pointer lets a function change the caller's object. Passing a struct by value copies it.
Pointers also model optional values: a *int field can be nil meaning "not set", which matters in JSON payloads and partial updates. Since Go 1.26 new accepts an expression, such as new(30), which makes optional fields easier to set; on earlier versions use a small helper like func ptr[T any](v T) *T { return &v }.
Slices and maps of pointers share the same objects, while slices of values hold copies:
Since Go 1.22 each loop iteration has its own variable, so taking &it inside a loop no longer aliases one variable as in older versions. Remember that copying a struct that holds a pointer copies only the pointer (a shallow copy).
sync.Mutex; pass a pointer.*int unless you need optionality or sharing; they add allocations and GC work.go test -race.&T{} or new(T) better?&T{} is more common because it lets you set fields inline. new(T) is fine when you only need a zero value.
To change the caller's struct from a function or method, yes. Maps, slices and channels already share underlying data, so changing elements works without a pointer, but resizing a slice does not.
Not necessarily. The compiler keeps it on the stack if it does not escape. Check with go build -gcflags=-m.
Assignment copies the pointer only. Write a Clone method that copies nested slices, maps and pointed-to values.
Create objects with &T{} or a NewT constructor, give them pointer receivers when they mutate, and pass pointers to share or change them. Watch for nil pointers, shallow copies and shared state across goroutines.