The * sign

Pointer Type

The * sign is used to define a pointer type. For example, *int represents a pointer to an integer type.

var num *int // Declares a pointer to an integer

Pointer Initialization

The * sign is used to initialize a pointer variable by assigning it the memory address of another variable.

var num int = 42
var ptr *int = &num // Initializes ptr with the memory address of num

Pointer Dereference

The * sign is used to dereference a pointer, accessing the value it points to.

var num int = 42
var ptr *int = &num // Assigns the memory address of num to ptr
fmt.Println(*ptr)  // Dereferences ptr and prints the value: 42

Function Parameter

The * sign is used in function parameter declarations to indicate that the parameter expects a pointer argument.

func doubleValue(ptr *int) {
  *ptr *= 2 // Modifies the value pointed to by ptr
}

Pointer Receiver

The * sign is used in method receivers to define a method for a pointer type, enabling modifications to the receiver’s value.

type Person struct {
	Name string
}
 
func (p *Person) UpdateName(newName string) {
	    p.Name = newName // Modifies the Name field of the Person
}

Memory Allocation

The * sign is used with the new keyword to allocate memory for a new value of a given type and return a pointer to that memory location.

var numPtr *int = new(int) // Allocates memory for an integer and returns a pointer to it
*numPtr = 42 // Sets the value of the allocated memory to 42

The & sign

Address-of Operator

The & sign is used as the address-of operator to get the memory address of a variable.

var num int = 42
var ptr *int = &num // Assigns the memory address of num to ptr

Function Arguments

The & sign is used when passing arguments to a function to pass the memory address (pointer) of a variable instead of its value.

func modifyValue(ptr *int) {
	*ptr = 42 // Modifies the value pointed to by ptr
}
var num int = 0
modifyValue(&num) // Passes the memory address of num to the function

Memory Allocation

The & sign is used in conjunction with the new keyword to allocate memory for a new value of a given type and return a pointer to that memory location.

var numPtr *int = new(int) // Allocates memory for an integer and returns a pointer to it

Struct Literals

The & sign is used with struct literals to create a pointer to a newly allocated struct.

type Person struct {
	Name string
}
 
var p *Person = &Person{Name: "John"}

Value vs. Pointer assignments

The difference between value and pointer assignments when modifications are made

type Mallard struct {
	Color string
}
 
func modifyValue(m Mallard) {
	m.Color = "Red"
}
 
func modifyPointer(m *Mallard) {
	m.Color = "Blue"
}
 
func main() {
	// Value Assignment
	var mallardVal Mallard = Mallard{Color: "Green"}
	modifyValue(mallardVal)
	fmt.Println("Value Assignment - After Modification:", mallardVal.Color) // Output: Green
 
	// Pointer Assignment
	var mallardPtr *Mallard = &Mallard{Color: "Green"}
	modifyPointer(mallardPtr)
	fmt.Println("Pointer Assignment - After Modification:", mallardPtr.Color) // Output: Blue
}