What is a Pointer?
Part of the Fundamentals section of Coddy's GO journey — lesson 60 of 109.
A pointer is a variable that stores the memory address of another variable. Pointers help us access or modify variables indirectly.
Declaring a pointer variable: Use the * symbol before the type to declare a variable that can hold a pointer:
var pointerToInt *int // Declares a pointer variable that can point to an int
var pointerToString *string // Declares a pointer variable that can point to a stringCreating a pointer to an existing variable: Use the & (address-of) operator to get the memory address of a variable:
x := 10
pointerToX := &x // pointerToX now holds the memory address of x
fmt.Println(pointerToX)Accessing the value a pointer points to: Use the * (dereference) operator:
fmt.Println(*pointerToX) // Prints the value stored at the addressThe output will be:
0xc000018030 // Memory address (your address will differ)
10 // The value at that addressCheat sheet
A pointer is a variable that stores the memory address of another variable.
Use the & (address-of) operator to get a pointer to a variable:
x := 10
pointerToX := &x // pointerToX holds the memory address of xUse the * (dereference) operator to access the value a pointer points to:
fmt.Println(*pointerToX) // Prints the value stored at the addressTry it yourself
This lesson doesn't include a code challenge.
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