Dereferencing Pointers
Part of the Fundamentals section of Coddy's GO journey — lesson 63 of 109.
Dereferencing pointers (*) lets you access the value stored at a memory address. This is called "dereferencing" the pointer.
Create a variable and a pointer to it:
func main() {
number := 42
pointer := &number
// Dereference the pointer to get the value
value := *pointer
fmt.Println("Pointer:", pointer)
fmt.Println("Dereferenced value:", value)
}The output shows both the pointer and the value it points to:
Pointer: 0xc000018030
Dereferenced value: 42The asterisk (*) before a pointer variable accesses the actual value at that memory address.
Challenge
BeginnerIn this challenge, you'll practice dereferencing pointers in Go. We have a variable message with a string value, and a pointer messagePtr that points to it.
Your task is to dereference the pointer to access and print the value it points to.
Cheat sheet
Use the asterisk (*) operator to dereference a pointer and access the value at the memory address:
number := 42
pointer := &number
// Dereference the pointer to get the value
value := *pointer
fmt.Println("Pointer:", pointer) // 0xc000018030
fmt.Println("Dereferenced value:", value) // 42The * operator before a pointer variable accesses the actual value stored at that memory address.
Try it yourself
package main
import "fmt"
func main() {
// Here's our string variable
message := "Hello, pointers!"
// This is a pointer to our message variable
messagePtr := &message
// TODO: Dereference the messagePtr to get the value it points to
// and store it in the variable 'value'
value := ""
// Print the results
fmt.Println("The pointer points to the value:", value)
}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 Actions2Variables 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