The Address-Of Operator
Part of the Fundamentals section of Coddy's GO journey — lesson 62 of 109.
The address-of operator (&) gets the memory address of a variable. It's how we create pointers in Go.
Create a variable and get its memory address:
func main() {
number := 42
addressOfNumber := &number
fmt.Println("Value:", number)
fmt.Println("Address:", addressOfNumber)
}The output shows the value and its memory address:
Value: 42
Address: 0xc000018030The address (like 0xc000018030) is a hexadecimal number representing the memory location where the value is stored. This address will be different each time you run the program.
Challenge
Beginner&) in Go. The address-of operator returns the memory address of a variable, which is where the variable's value is stored in the computer's memory.Your task is to use the address-of operator to get the memory address of the provided variable and store it in the pointer variable that's already declared.
Try it yourself
package main
import "fmt"
func main() {
// This is our sample variable
name := "Gopher"
// This declares a pointer variable to store the address of a string
var namePointer *string
// TODO: Use the address-of operator (&) to get the memory address of 'name'
// and store it in the 'namePointer' variable
// Print the value that the pointer points to
fmt.Printf("The value at that memory address is: %v\n", *namePointer)
}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