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 roles: pointer indirection (dereferencing), pointer type declaration, and binary arithmetic multiplication.

1. Pointer Indirection (Dereferencing)

When used as a prefix unary operator in an expression, * performs pointer indirection. It operates on a pointer operand to yield the underlying value stored at the memory address the pointer holds. In Go semantics, a pointer indirection (*ptr) denotes a variable. This makes it an addressable operand, meaning it can be read from or assigned to directly. Attempting to dereference a nil pointer results in a runtime panic. Automatic Dereferencing: Go features automatic indirection in specific contexts, meaning the explicit * operator is often omitted. The compiler automatically dereferences pointers in the following scenarios:
  • Accessing a field on a pointer to a struct (ptr.Field becomes (*ptr).Field).
  • Calling a method with a value receiver on a pointer to any defined type (ptr.Method() becomes (*ptr).Method()).
  • Using the index operator on a pointer to an array (arrPtr[0] becomes (*arrPtr)[0]).
type CustomInt int

func (c CustomInt) Process() {}

func indirectionExamples() {
    var val int = 42
    var ptr *int = &val

    // Reading the value at the memory address
    var readVal int = *ptr 
    _ = readVal

    // *ptr denotes a variable, making it an addressable operand for assignment
    *ptr = 100 

    // Automatic dereferencing: Struct field access
    type Wrapper struct { Data int }
    var wPtr *Wrapper = &Wrapper{Data: 5}
    _ = wPtr.Data // Implicitly evaluated as (*wPtr).Data

    // Automatic dereferencing: Array indexing
    var arrPtr *[3]int = &[3]int{1, 2, 3}
    _ = arrPtr[0] // Implicitly evaluated as (*arrPtr)[0]

    // Automatic dereferencing: Method call on a defined type
    var cPtr *CustomInt = new(CustomInt)
    cPtr.Process() // Implicitly evaluated as (*cPtr).Process()
}

2. Pointer Type Declaration

When used in a type signature or type expression, * acts as a type constructor. It precedes a base type to denote a new composite type: a pointer to that base type. This informs the compiler that the variable will store a memory address rather than a direct value.
// Variable declaration
var strPtr *string 

// Function parameter and return type signatures
func compute(input *float64) *int {
    return nil
}

// Struct field definition
type Node struct {
    Value int
    Next  *Node 
}

3. Arithmetic Multiplication

When used as a binary operator between two numeric operands, * performs arithmetic multiplication. It is applicable to all integer, floating-point, and complex numeric types in Go. Due to Go’s strict typing rules, both operands must generally be of the same type or explicitly converted to the same type. However, this rule does not apply to untyped constants. A typed variable can be multiplied by an untyped constant without explicit conversion, provided the constant’s value is representable by the variable’s type.
func multiplicationExamples() {
    var a int = 5
    var b int = 10

    // Binary arithmetic expression with matching types
    var product int = a * b 
    _ = product

    var c float64 = 2.5

    // Multiplication using a typed variable and an untyped constant (4)
    var floatProduct float64 = c * 4 
    _ = floatProduct
}
Master Go with Deep Grasping Methodology!Learn More