Structs
A struct is a named group of fields that you handle as a single value. Go has no classes, so structs are how you model "a thing with data" — and methods are attached to them separately.
Detailed Explanation
Say you are storing user sessions. Each session has a token, a user ID, and an expiry time. You could pass three separate arguments everywhere, but they belong together. A struct lets you declare that grouping once and then pass one Session value around.
type Session struct {
Token string
UserID int64
Expires time.Time
}
That is the whole idea. A struct type is a fixed list of named, typed fields. The list is decided at compile time and cannot change at runtime, which is the main difference from a Python dict or a JavaScript object.
Structs are values, not references. This is the part that trips up people coming from Java. In Java, Session s = other; copies a reference and both names point at the same object. In Go, s := other copies every field. Mutating s.UserID does not touch other. If you want shared, mutable state you must pass a pointer (*Session) explicitly. Go makes the copy visible in the type, instead of hiding it.
Because structs are values, a struct occupies exactly the space of its fields laid out in memory, plus a little padding for alignment. There is no object header, no vtable pointer, no separate allocation. A []Session is one contiguous block of session data, which is why Go code can be cache-friendly without much effort.
Why Go chose this over classes. A class bundles three things: data layout, method set, and an inheritance hierarchy. Go splits them apart. Structs define data layout. Methods are declared separately with a receiver. Reuse comes from embedding, not inheritance. Polymorphism comes from interfaces, which a struct satisfies implicitly by having the right methods. The result is that you can add behaviour to a type without editing its declaration, and you can satisfy an interface you have never heard of.
Zero values are usable by design. Declaring var s Session does not give you nil. It gives you a real Session with Token == "", UserID == 0, and Expires as the zero time. Well-designed Go types make that zero value meaningful — sync.Mutex and bytes.Buffer are both ready to use with no constructor call. Go has no constructors; when initialisation is genuinely required, you write a plain function like NewSession(...) and return the value.
What it costs. Copying a large struct on every call is real work, so past a few dozen bytes you usually pass a pointer. Structs are also rigid: adding a field changes the type for every caller, and comparing structs with == only works if every field is comparable (a struct containing a slice or a map cannot be compared with ==, and that is a compile error, not a runtime surprise).
Code Examples
package main
import (
"fmt"
"time"
)
// A struct type is a fixed list of named, typed fields.
type Session struct {
Token string
UserID int64
Expires time.Time
}
func main() {
// Preferred: keyed literal. Survives field reordering and new fields.
s := Session{
Token: "abc123",
UserID: 42,
// Expires is omitted, so it gets the zero value for time.Time.
}
// The zero value is a real, usable Session. Nothing is nil.
var empty Session
fmt.Println(empty.Token == "", empty.UserID) // true 0
// Assignment copies every field. These are two independent values.
copyOfS := s
copyOfS.UserID = 99
fmt.Println(s.UserID, copyOfS.UserID) // 42 99
// A pointer lets callers share and mutate one value.
touch(&s)
fmt.Println(s.Expires.IsZero()) // false
}
// Taking *Session means the caller sees the change.
func touch(s *Session) {
// Go auto-dereferences: s.Expires means (*s).Expires.
s.Expires = time.Now().Add(time.Hour)
}Structs with only comparable fields support == and work as map keys.
// Structs with only comparable fields support == and work as map keys.
type Point struct{ X, Y int }
func demo() {
seen := map[Point]bool{}
seen[Point{1, 2}] = true
fmt.Println(seen[Point{1, 2}]) // true
fmt.Println(Point{1, 2} == Point{1, 2}) // true, field-by-field
// An empty struct carries no data and costs zero bytes. Common as a
// set value or as a channel signal.
set := map[string]struct{}{"go": {}}
_, ok := set["go"]
fmt.Println(ok) // true
}Common Gotchas
Positional literals break silently on refactor
`Session{"abc", 42, t}` compiles today, but adding or reordering a field changes what each argument means. Always use keyed literals (`Session{Token: "abc"}`) outside of tiny two-field types.
Not every struct is comparable
`==` on a struct compares field by field, but only if all fields are comparable. A struct holding a slice, map, or function fails to compile under `==`. Use `reflect.DeepEqual` in tests, or write your own `Equal` method.
Copies are shallow
copying a struct copies its fields, and a slice or map field is itself a reference to shared data. Two "independent" copies can still write to the same backing array.
Unexported fields stay inside the package
a lowercase field name is invisible outside its package, which also means `encoding/json` will not marshal it.