Accessing Struct Fields
Part of the Fundamentals section of Coddy's GO journey — lesson 96 of 109.
To access fields in a struct, use the dot notation (structVariable.fieldName).
Create a Person struct and access its fields:
type Person struct {
name string
age int
}
func main() {
person := Person{name: "Alice", age: 30}
// Access individual fields
fmt.Println(person.name)
fmt.Println(person.age)
}The output shows the individual field values:
Alice
30You can also modify field values using the same dot notation:
person.age = 31
fmt.Println(person.age) // Prints: 31Challenge
EasyIn this challenge, you'll practice accessing fields from a struct. We've defined a Person struct with name, age, and isStudent fields.
Your task is to access these fields and print them using the correct format verbs.
Cheat sheet
To access fields in a struct, use dot notation (structVariable.fieldName):
type Person struct {
name string
age int
}
func main() {
person := Person{name: "Alice", age: 30}
// Access individual fields
fmt.Println(person.name)
fmt.Println(person.age)
}You can also modify field values using dot notation:
person.age = 31
fmt.Println(person.age) // Prints: 31Try it yourself
package main
import "fmt"
func main() {
// Person struct with three fields
type Person struct {
name string
age int
isStudent bool
}
// A sample person
john := Person{
name: "John Doe",
age: 25,
isStudent: true,
}
// TODO: Print john's name using fmt.Printf and the %s format verb
fmt.Printf("Name: %s\n", ?)
// TODO: Print john's age using fmt.Printf and the %d format verb
fmt.Printf("Age: %d\n", ?)
// TODO: Print whether john is a student using fmt.Printf and the %t format verb
fmt.Printf("Is student: %t\n", ?)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions10Composite Types: Arrays
Introduction to ArraysDeclaring ArraysInitializing ArraysAccessing Array ElementsArray Length with `len`Iterating Over ArraysMulti-dimensional Arrays13Composite Types: Structs
Defining Custom TypesCreating Struct InstancesAccessing Struct FieldsPointers to StructsInitializing StructsEmbedded StructsAnonymous StructsRecap - Custom Data Structures2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions