Declaring Pointer Variables
Part of the Fundamentals section of Coddy's GO journey — lesson 61 of 109.
In Go, you can declare pointers in two main ways: using var with a pointer type or using the address operator with :=.
Declare a pointer using var with a pointer type:
func main() {
var x int = 42
var ptr *int // Declare a pointer to an integer
ptr = &x // Assign the address of x to ptr
fmt.Println("Value of x:", x)
fmt.Println("Value of ptr:", ptr)
fmt.Println("Value at ptr:", *ptr)
}The output shows the pointer stores the address of x:
Value of x: 42
Value of ptr: 0xc000018030
Value at ptr: 42You can also declare and initialize a pointer in one line using :=:
y := 100
ptr := &y // Declare and initialize pointer in one stepChallenge
BeginnerIn this challenge, you'll practice declaring and using pointers in Go. A pointer is already set up for you, and your task is to create a new pointer that points to the same value.
Complete the TODO in the code to declare a new pointer variable called 'secondPointer' that points to the same memory address as the existing pointer.
Cheat sheet
In Go, you can declare pointers using var with a pointer type or using the address operator with :=.
Declare a pointer using var:
var x int = 42
var ptr *int // Declare a pointer to an integer
ptr = &x // Assign the address of x to ptrDeclare and initialize a pointer in one line using :=:
y := 100
ptr := &y // Declare and initialize pointer in one stepAccess pointer values:
ptr- the memory address*ptr- the value at that address
Try it yourself
package main
import "fmt"
func main() {
// Here's a variable and a pointer to it
originalValue := 42
pointerToValue := &originalValue
// TODO: Declare a new pointer variable called 'secondPointer'
// that points to the same memory address as 'pointerToValue'
// Don't modify the code below
fmt.Printf("Original value: %v\n", originalValue)
fmt.Printf("Value through first pointer: %v\n", *pointerToValue)
fmt.Printf("Value through second pointer: %v\n", *secondPointer)
// Let's change the original value and see all pointers reflect the change
originalValue = 100
fmt.Printf("\nAfter changing original value to 100:\n")
fmt.Printf("Original value: %v\n", originalValue)
fmt.Printf("Value through first pointer: %v\n", *pointerToValue)
fmt.Printf("Value through second pointer: %v\n", *secondPointer)
}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 Decisions9Pointers
What is a Pointer?Declaring Pointer VariablesThe Address-Of OperatorDereferencing PointersUsing Pointers in FunctionsNil PointersRecap - Understanding Pointers