Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The * operator in Go is a context-dependent token that serves three distinct syntactic and semantic functions: pointer indirection (dereferencing), pointer type declaration, and arithmetic multiplication.

1. Pointer Indirection (Dereferencing)

As a unary prefix operator, * performs indirection on a pointer operand. It resolves the memory address held by the pointer and yields the underlying value. The result of a pointer indirection is addressable, meaning it can be used both to evaluate the value at an address and to assign a new value to mutate the underlying memory. If the pointer operand evaluates to nil, evaluating or assigning to the indirection will cause a runtime panic.
x := 10
ptr := &x           // ptr is initialized with the address of x

var val int = *ptr  // Evaluates the value at the memory address stored in ptr
*ptr = 42           // Mutates the memory at the address stored in ptr (x becomes 42)

2. Pointer Type Declaration

In type declarations, variable declarations, and function signatures, * acts as a type constructor. It denotes a composite type that represents a pointer to a specific base type. The base type dictates the memory layout, size, and alignment of the data being pointed to.
type Node struct{}
type DataStruct struct{}

var p *int                                // Declares p as type "pointer to int"
type NodePtr *Node                        // Defines a custom pointer type

func process(data *DataStruct) *byte {    // Used in parameter and return type signatures
    return nil
}

3. Arithmetic Multiplication

As a binary infix operator, * performs arithmetic multiplication. It requires two numeric operands of identical types (or untyped constants that can be implicitly converted to a common type) and yields a product of that same type.
var product float64 = 3.14 * 2.0  // Binary multiplication
Master Go with Deep Grasping Methodology!Learn More