Pointers to Structs
Part of the Fundamentals section of Coddy's GO journey — lesson 97 of 109.
When working with structs, you can create pointers to them just like with other types. This is useful for passing large structs efficiently.
Define a Person struct and create a pointer to it:
type Person struct {
name string
age int
}
func main() {
person := Person{name: "Alice", age: 30}
// Create a pointer to the struct
personPtr := &person
// Access fields through pointer
fmt.Println(personPtr.name)
fmt.Println(personPtr.age)
}The output shows we can access fields through the pointer:
Alice
30You can also modify fields through the pointer:
personPtr.age = 31
fmt.Println(person.age) // Prints: 31Challenge
BeginnerIn this challenge, you'll practice working with pointers to structs in Go. You have a Person struct with name and age fields. Your task is to update the person's information using a pointer to the struct.
The code already creates a Person struct and a pointer to it. You need to modify the person's information through the pointer.
Cheat sheet
Create pointers to structs using the & operator:
type Person struct {
name string
age int
}
person := Person{name: "Alice", age: 30}
personPtr := &personAccess and modify struct fields through pointers:
// Access fields
fmt.Println(personPtr.name)
fmt.Println(personPtr.age)
// Modify fields
personPtr.age = 31Try it yourself
package main
import "fmt"
// Person struct with name and age fields
type Person struct {
name string
age int
}
func main() {
// Create a Person struct
person := Person{
name: "John",
age: 25,
}
// Create a pointer to the Person struct
personPtr := &person
// Print the original person information
fmt.Println("Original person:")
fmt.Printf("Name: %s, Age: %d\n", person.name, person.age)
// TODO: Use the pointer to change the person's name to "Jane" and age to 30
// Hint: You can access struct fields through a pointer using the dot notation
// Print the updated person information
fmt.Println("Updated person:")
fmt.Printf("Name: %s, Age: %d\n", person.name, person.age)
}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