Skip to content

append builtin

The append builtin appends one or more elements to a slice (array) and returns the resulting slice. It mirrors Go-like behavior.

  • Name: append
  • Signature: append(slice, elems...) -> slice
  • Variadic: yes
  • Type safety:
  • Untyped arrays allow mixed element types (existing language behavior)
  • Typed arrays enforce that appended element types match the element type
  • Returns: a new slice value containing original elements plus appended elements

Examples

Append Examples
package main
import fmt

var a = [1, 2]
var b = append(a, 3, 4)
fmt.Println(b) // [1, 2, 3, 4]

var m = ["x", 1]
var m2 = append(m, true)
fmt.Println(m2) // [x, 1, true]

var ta = int[2]{1, 2}
var tb = append(ta, 3, 4)
fmt.Println(tb) // int[4]{1, 2, 3, 4}

Notes: - The returned slice may or may not share storage with the original (implementation detail). Treat the result as the new slice. - For typed arrays, appending an incompatible type is a compile-time error (when possible) and a runtime error otherwise.