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 functions as both a unary operator for numeric types and an overloaded binary operator that performs arithmetic addition on numeric types and concatenation on string types.
// Unary operation
result := +operand

// Binary operation
result := operand1 + operand2

Unary Operator Mechanics

When used as a unary operator (+x), the + token is defined for all numeric types (integers, floating-point, and complex numbers). It evaluates to the operand itself, yielding the exact value and type of the operand without modification.

Binary Operator Type Strictness

Go enforces strict typing for binary operations. The + operator requires both operands to be of the identical type; Go does not perform implicit type coercion between distinct typed variables. Attempting to add variables of different types results in a compile-time mismatched types error. The exception to this strictness involves untyped constants. When an untyped constant is added to a typed variable, the compiler implicitly converts the untyped constant to the exact type of the typed operand. When two untyped constants are added together, the compiler evaluates them at compile time, yielding a new untyped constant (e.g., adding an untyped integer and an untyped float yields an untyped float).
var a int32 = 10
var b int32 = 20
sum := a + b // Valid: both are int32

var c float64 = 5.5
// invalid operation: a + c (mismatched types int32 and float64)

// Untyped constant mixed with a typed variable
var x float64 = 1.5
result := x + 2 // Valid: untyped constant '2' is implicitly converted to float64

Numeric Addition Mechanics

When applied as a binary operator to numeric types, + behaves according to the underlying hardware architecture and specific type rules:
  • Integers (int, uint, int8, etc.): Performs standard binary addition. If the result exceeds the maximum value representable by the type, Go performs silent wrap-around without triggering a runtime panic or overflow error. For unsigned integers, this wrap-around strictly follows modulo 2^N arithmetic (where N is the bit width of the type). For signed integers, the wrap-around behavior is based on two’s complement arithmetic.
  • Floating-Point (float32, float64): Performs addition conforming to the IEEE-754 standard, including the standard handling of infinities and NaN (Not a Number).
  • Complex Numbers (complex64, complex128): Performs vector addition, adding the real parts together and the imaginary parts together independently.

String Concatenation Mechanics

When applied to string types, the binary + operator joins two strings end-to-end. In Go, strings are immutable sequences of bytes. They are strictly distinct from byte slices (e.g., a string’s underlying memory header contains a pointer and length, but lacks the capacity field found in a slice header). Because strings are immutable, the + operator cannot append data in place. Instead, evaluating s1 + s2 triggers a new contiguous memory allocation large enough to hold the combined byte sequence of both operands. The bytes from the first string are copied into the new memory space, followed immediately by the bytes from the second string.
str1 := "Hello, "
str2 := "World"
combined := str1 + str2 // Allocates new memory for "Hello, World"
Master Go with Deep Grasping Methodology!Learn More