In Go, pointer arithmetic (performing operations like addition or subtraction on pointers) is not allowed. This differs from languages like C or C++, where you can manipulate pointers using arithmetic.
đ The key reason pointer arithmetic is not allowed in Go is to prevent unsafe memory manipulation. Go prioritizes memory safety, and by disallowing pointer arithmetic, it ensures that memory is handled in a controlled and predictable manner.
1ī¸âŖ What is Pointer Arithmetic?
Pointer arithmetic involves modifying a pointerâs value to point to a different memory location by adding or subtracting from the memory address it holds.
đš Example in C (where pointer arithmetic is allowed):
int arr[] = {1, 2, 3};
int *ptr = arr;
ptr++; // Moves the pointer to the next memory address
In the above C example, ptr++
moves the pointer to the next element in the array. This is not allowed in Go.
2ī¸âŖ Why Pointer Arithmetic is Not Allowed in Go
Go restricts pointer arithmetic to protect memory safety. Allowing arithmetic on pointers could lead to:
Go prefers to keep pointer usage simple and controlled, ensuring that you cannot manipulate memory addresses in unsafe ways.
3ī¸âŖ What You Can Do With Pointers in Go
In Go, you can:
&
operator.*
operator to access the value stored at that memory address.đš Example:
package main
import "fmt"
func main() {
var x int = 10
var ptr *int = &x // Pointer to x
fmt.Println("Value of x:", x) // Prints: 10
fmt.Println("Pointer value:", ptr) // Prints: Memory address of x
*ptr = 20 // Modify the value at the pointer address
fmt.Println("Updated value of x:", x) // Prints: 20
}
đš Output:
Value of x: 10
Pointer value: 0xc000012090 // Example memory address
Updated value of x: 20
4ī¸âŖ What You Cannot Do in Go
In Go, you cannot perform pointer arithmetic like adding or subtracting from pointers, such as ptr++
or ptr--
.
đš Example (Invalid in Go):
package main
import "fmt"
func main() {
var x int = 5
var ptr *int = &x
// This would cause a compile-time error in Go
// ptr++ // Invalid: Go does not allow pointer arithmetic
fmt.Println("Pointer value:", ptr)
}
đš Output:
Invalid operation: ptr++ (Go does not support pointer arithmetic)
ptr++
, ptr--
), unlike C or C++.Pointers in Go are made simple and safe to use, avoiding potential risks associated with direct memory manipulation.
Go āϤā§, pointer arithmetic (āϝā§āĻŽāύ: pointer āĻāϰ āĻāĻĒāϰ āϝā§āĻ āĻŦāĻž āĻŦāĻŋāϝāĻŧā§āĻ āĻāϰāĻž) āĻ āύā§āĻŽā§āĻĻāĻŋāϤ āύāϝāĻŧāĨ¤ āĻāĻāĻŋ C āĻŦāĻž C++ āĻāϰ āĻŽāϤ⧠āĻāĻžāώāĻžāϰ āϤā§āϞāύāĻžāϝāĻŧ āĻāϞāĻžāĻĻāĻž, āϝā§āĻāĻžāύ⧠pointer arithmetic āϏāĻžāϧāĻžāϰāĻŖāĻāĻžāĻŦā§ āĻŦā§āϝāĻŦāĻšā§āϤ āĻšāϝāĻŧāĨ¤
đ Go āĻāĻžāώāĻžāĻāĻŋ āĻŽā§āĻŽā§āϰāĻŋ āύāĻŋāϰāĻžāĻĒāϤā§āϤāĻž āύāĻŋāĻļā§āĻāĻŋāϤ āĻāϰāĻžāϰ āĻāύā§āϝ pointer arithmetic āύāĻŋāώāĻŋāĻĻā§āϧ āĻāϰā§āĻā§āĨ¤
1ī¸âŖ Pointer Arithmetic āĻā§?
Pointer arithmetic āĻŦāϞāϤ⧠āĻŦā§āĻāĻžāϝāĻŧ āĻāĻāĻāĻŋ pointer āĻāϰ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰāĻž, āϝā§āĻŽāύ C āĻŦāĻž C++ āĻ āĻāĻĒāύāĻŋ ptr++
āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠pointer āĻā§ āĻĒāϰāĻŦāϰā§āϤ⧠āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻžāϝāĻŧ āύāĻŋāϝāĻŧā§ āϝā§āϤ⧠āĻĒāĻžāϰā§āύāĨ¤
đš āĻāĻĻāĻžāĻšāϰāĻŖ (C āϤā§, āϝā§āĻāĻžāύ⧠pointer arithmetic āĻ āύā§āĻŽā§āĻĻāĻŋāϤ):
int arr[] = {1, 2, 3};
int *ptr = arr;
ptr++; // Pointer āĻā§ array āĻāϰ āĻĒāϰāĻŦāϰā§āϤ⧠āĻāĻĒāĻžāĻĻāĻžāύ⧠āύāĻŋāϝāĻŧā§ āϝāĻžāϝāĻŧ
āϤāĻŦā§ Go āϤā§, āĻāĻāĻŋ āĻ
āύā§āĻŽā§āĻĻāĻŋāϤ āύāϝāĻŧāĨ¤ āĻāĻĒāύāĻŋ ptr++
āĻŦāĻž ptr--
āĻāϰ āĻŽāϤ⧠āĻā§āϰāĻŋāϝāĻŧāĻž āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āύ āύāĻžāĨ¤
2ī¸âŖ Go āϤ⧠Pointer Arithmetic āύāĻŋāώāĻŋāĻĻā§āϧ āĻā§āύ?
Go āĻāĻžāώāĻžāϝāĻŧ pointer arithmetic āύāĻŋāώāĻŋāĻĻā§āϧ āĻāϰāĻž āĻšāϝāĻŧā§āĻā§ āĻŽā§āĻŽā§āϰāĻŋ āύāĻŋāϰāĻžāĻĒāϤā§āϤāĻž āύāĻŋāĻļā§āĻāĻŋāϤ āĻāϰāĻžāϰ āĻāύā§āϝāĨ¤ Pointer arithmetic āĻ āύā§āĻŽā§āĻĻāĻŋāϤ āĻšāϞā§:
āĻāĻ āĻā§āĻāĻāĻŋāĻā§āϞ⧠āĻāĻĄāĻŧāĻžāύā§āϰ āĻāύā§āϝ, Go āĻāĻžāώāĻžāϝāĻŧ pointer arithmetic āϏāĻŽā§āĻĒā§āϰā§āĻŖ āύāĻŋāώāĻŋāĻĻā§āϧ āĻāϰ⧠āĻŽā§āĻŽā§āϰāĻŋ āĻŦā§āϝāĻŦāϏā§āĻĨāĻžāĻĒāύāĻž āύāĻŋāϰāĻžāĻĒāĻĻ āĻ āύāĻŋāϝāĻŧāύā§āϤā§āϰāĻŋāϤ āϰāĻžāĻāĻž āĻšāϝāĻŧā§āĻā§āĨ¤
3ī¸âŖ Go āϤ⧠Pointers āĻĻāĻŋāϝāĻŧā§ āĻā§ āĻāϰāĻž āϝāĻžāϝāĻŧ
Go āϤ⧠pointers āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻāĻĒāύāĻŋ:
Pointer arithmetic āĻāϰ āĻĒāϰāĻŋāĻŦāϰā§āϤā§, Go āĻŽā§āĻŽā§āϰāĻŋāϰ āϏāĻžāĻĨā§ āύāĻŋāϰāĻžāĻĒāĻĻ āĻāĻŦāĻ āϏā§āĻĒāώā§āĻāĻāĻžāĻŦā§ āĻāĻžāĻ āĻāϰāĻžāϰ āĻāĻĒāĻžāϝāĻŧ āĻĻā§āϝāĻŧāĨ¤ āύāĻŋāĻā§ āĻā§ āĻāϰāĻž āϝāĻžāϝāĻŧ āϤāĻžāϰ āĻāĻāĻāĻŋ āĻāĻĻāĻžāĻšāϰāĻŖ āĻĻā§āĻāϝāĻŧāĻž āĻšāϞā§:
đš āĻāĻĻāĻžāĻšāϰāĻŖ:
package main
import "fmt"
func main() {
var x int = 10
var ptr *int = &x // x āĻāϰ āĻāύā§āϝ pointer
fmt.Println("x āĻāϰ āĻŽāĻžāύ:", x) // Prints: 10
fmt.Println("Pointer āĻāϰ āĻŽāĻžāύ:", ptr) // Prints: x āĻāϰ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž
*ptr = 20 // Pointer āĻāϰ āĻ āĻŋāĻāĻžāύāĻžāϝāĻŧ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰāĻž āĻšāϞā§
fmt.Println("x āĻāϰ āĻāĻĒāĻĄā§āĻā§āĻĄ āĻŽāĻžāύ:", x) // Prints: 20
}
đš Output:
x āĻāϰ āĻŽāĻžāύ: 10
Pointer āĻāϰ āĻŽāĻžāύ: 0xc000012090 // āĻāĻĻāĻžāĻšāϰāĻŖāϏā§āĻŦāϰā§āĻĒ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž
x āĻāϰ āĻāĻĒāĻĄā§āĻā§āĻĄ āĻŽāĻžāύ: 20
āĻāĻ āĻāĻĻāĻžāĻšāϰāĻŖā§, ptr
x āĻāϰ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž āϧāĻžāϰāĻŖ āĻāϰā§āĨ¤ āĻāĻĒāύāĻŋ ptr
āĻĻāĻŋāϝāĻŧā§ x
āĻāϰ āĻŽāĻžāύ āĻĒāϰāĻŋāĻŦāϰā§āϤāύ āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āύ, āϤāĻŦā§ ptr++
āĻāϰ āĻŽāϤ⧠arithmetic āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āύ āύāĻžāĨ¤
4ī¸âŖ Go āϤ⧠āĻā§ āĻāϰāĻž āϝāĻžāϝāĻŧ āύāĻž
Go āϤ⧠pointer arithmetic āϏāĻŽā§āĻĒā§āϰā§āĻŖ āύāĻŋāώāĻŋāĻĻā§āϧāĨ¤ āĻāĻĒāύāĻŋ pointers āĻāϰ āĻāĻĒāϰ āϝā§āĻ āĻŦāĻž āĻŦāĻŋāϝāĻŧā§āĻ āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§āύ āύāĻž, āϝā§āĻŽāύ ptr++
āĻŦāĻž ptr--
āĨ¤
đš āĻāĻĻāĻžāĻšāϰāĻŖ (Go āϤ⧠āĻ āĻŦā§āϧ):
package main
import "fmt"
func main() {
var x int = 5
var ptr *int = &x
// Pointer arithmetic āĻāϰāϞ⧠āĻāĻŽā§āĻĒāĻžāĻāϞ-āĻāĻžāĻāĻŽ āĻ āĻāϰāϰ āĻĻāĻŋāĻŦā§
// ptr++ // āĻ
āĻŦā§āϧ: Go āϤ⧠pointer arithmetic āĻ
āύā§āĻŽā§āĻĻāĻŋāϤ āύāϝāĻŧ
fmt.Println("Pointer āĻāϰ āĻŽāĻžāύ:", ptr)
}
đš Output:
Invalid operation: ptr++ (Go does not support pointer arithmetic)
Go āĻāϰ āĻāĻ āύāĻŋāϝāĻŧāĻŽ āĻā§āĻĄāĻā§ āύāĻŋāϰāĻžāĻĒāĻĻ āĻāĻŦāĻ āĻĒā§āϰā§āĻŦāĻžāĻāĻžāϏāϝā§āĻā§āϝ āϰāĻžāĻā§āĨ¤
ptr++
āĻŦāĻž ptr--
) āĻ
āύā§āĻŽā§āĻĻāĻŋāϤ āύāϝāĻŧ, āϝāĻž C āĻŦāĻž C++ āϤ⧠āϏāĻžāϧāĻžāϰāĻŖāĨ¤Go āϤ⧠pointers āϏāĻšāĻ āĻ āύāĻŋāϰāĻžāĻĒāĻĻ, āϝāĻž āϏāϰāĻžāϏāϰāĻŋ āĻŽā§āĻŽā§āϰāĻŋ āĻ āĻŋāĻāĻžāύāĻž āĻŽā§āϝāĻžāύāĻŋāĻĒā§āϞā§āĻļāύ āĻāϰ⧠āϤā§āϰāĻŋ āĻšāĻāϝāĻŧāĻž āϏāĻžāϧāĻžāϰāĻŖ āϤā§āϰā§āĻāĻŋ āĻāĻĄāĻŧāĻžāϤ⧠āϏāĻšāĻžāϝāĻŧāĻāĨ¤