Defining and Using Structs
Struct literals, field access, and embedding
Defining and Using Structs is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Struct?
A struct groups related fields of different types under one named type. It's Go's primary mechanism for defining custom data structures — similar to a class but without inheritance.
Struct Declaration
Define a struct type with the type keyword:
package main
import "fmt"
type Person struct {
Name string
Age int
City string
}
func main() {
p := Person{Name: "Alice", Age: 30, City: "London"}
fmt.Println(p)
}Struct Literals
Two styles: named fields (preferred) and positional (order-dependent, fragile):
package main
type Point struct{ X, Y int }
func main() {
p1 := Point{X: 1, Y: 2} // named — preferred
p2 := Point{3, 4} // positional — avoid in non-test code
_ = p1; _ = p2
}Accessing Fields
Use dot notation to read and write fields:
package main
import "fmt"
type Car struct{ Brand string; Speed int }
func main() {
c := Car{Brand: "Tesla", Speed: 200}
fmt.Println(c.Brand) // Tesla
c.Speed = 250
fmt.Println(c.Speed) // 250
}Zero Value of a Struct
A struct's zero value has all fields set to their respective zero values. You can use it safely without initialization:
package main
import "fmt"
type Config struct{ Debug bool; Port int; Host string }
func main() {
var cfg Config
fmt.Println(cfg) // {false 0 }
cfg.Port = 8080
fmt.Println(cfg) // {false 8080 }
}Struct Comparison
Structs are comparable with == if all their fields are comparable:
package main
import "fmt"
type Point struct{ X, Y int }
func main() {
a := Point{1, 2}
b := Point{1, 2}
c := Point{3, 4}
fmt.Println(a == b) // true
fmt.Println(a == c) // false
}Structs Are Value Types
Assigning a struct copies all its fields. Changes to the copy don't affect the original:
package main
import "fmt"
type Rect struct{ W, H int }
func main() {
r1 := Rect{10, 20}
r2 := r1
r2.W = 99
fmt.Println(r1) // {10 20}
fmt.Println(r2) // {99 20}
}Nested Structs
A struct field can itself be a struct:
package main
import "fmt"
type Address struct{ City, Country string }
type Employee struct {
Name string
Age int
Address Address
}
func main() {
e := Employee{Name: "Bob", Age: 28, Address: Address{"Berlin", "DE"}}
fmt.Println(e.Address.City) // Berlin
}Exported vs Unexported Fields
Fields starting with an uppercase letter are exported (public). Lowercase fields are unexported (package-private):
package main
type config struct {
host string // unexported
Port int // exported
}
func main() {
c := config{host: "localhost", Port: 8080}
_ = c
}Struct Tags
Struct tags provide metadata (e.g., for JSON encoding). They are string literals after the field type:
package main
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email,omitempty"`
}
func main() { _ = User{} }Quick Check
What is the zero value of a bool field in a struct?
Recap: Structs
Key takeaways:
- Structs group related fields into a named type
- Use named field literals — avoid positional
- Structs are value types — assignment copies
- Fields starting with uppercase are exported
- Struct tags add metadata for libraries like encoding/json
Practice Prompt
Define a Product struct with fields Name string, Price float64, and InStock bool. Create two instances, compare them with ==, and print the result.
Frequently asked questions
Is the “Defining and Using Structs” lesson free?
Yes — the full text of “Defining and Using Structs” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defining and Using Structs”?
Struct literals, field access, and embedding You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining and Using Structs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining and Using Structs
- Methods on Structs
- Constructor Patterns
- Anonymous Structs and Composition