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 an overloaded operator that functions as a binary arithmetic operator for numeric addition, a binary operator for string concatenation, and a unary operator for numeric identity. Due to Go’s strict type system, binary operations require both operands to resolve to the identical type; Go does not perform implicit type coercion.
// Unary syntax
+operand

// Binary syntax
operand1 + operand2

Binary Numeric Addition

When applied to numeric types (integers, floating-point numbers, and complex numbers), the + operator computes the mathematical sum of the two operands.
  • Type Strictness: Both operands must be of the exact same type (e.g., int32 + int32). Attempting to add an int32 to an int64 results in a compile-time type mismatch error. Explicit type conversion is required to align the operand types prior to evaluation.
  • Integer Overflow: Go handles integer overflow silently. If the sum of two integers exceeds the maximum representable value for their bit-width, the result wraps around according to two’s complement arithmetic (for signed integers) or standard modulo arithmetic (for unsigned integers). It does not trigger a runtime panic.
  • Floating-Point and Complex: Addition follows IEEE-754 standards, including the handling of infinities and NaN (Not a Number) values.
var a int8 = 127
var b int8 = 1
var c int8 = a + b // c is -128 (silent overflow)

var x float32 = 1.5
var y float64 = 2.0
// var z = x + y   // Compilation error: mismatched types
var z = float64(x) + y // Valid explicit conversion

Binary String Concatenation

When applied to operands of type string, the + operator performs byte-wise concatenation.
  • Immutability: Because strings in Go are immutable, the + operator allocates a new backing array in memory and copies the byte sequences of the left operand followed by the right operand into this new array.
  • Type Strictness: Both operands must be strings. Go will not implicitly convert a numeric or boolean type to a string during concatenation.
str1 := "foo"
str2 := "bar"
result := str1 + str2 // Yields a new string "foobar"

Unary Identity

When used as a prefix to a single numeric operand, + acts as the unary identity operator. It yields the unmodified value of the operand. It cannot be applied to strings or booleans.
var a int = 5
var b int = +a // b is 5

Untyped Constants

If the operands are untyped constants, the + operator is evaluated at compile-time rather than runtime. Go’s compiler performs constant arithmetic with arbitrary precision (at least 256 bits). The result of adding two untyped constants is a new untyped constant, which only assumes a concrete type when assigned to a variable or used in a typed context.
const a = 1e100
const b = 2e100
const c = a + b // Evaluated at compile-time, no overflow
Master Go with Deep Grasping Methodology!Learn More