HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GoVariablesData TypesOperatorsStringsArray & SliceMap (Dictionary)Conditions (if / switch)Loops (for)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Slice
slice [ ]append()
🗂️ Map
🔁 Condition & Loop
🧩 Function
Go›REFERENCE›Array & Slice
📋

Array & Slice

Working with flexible lists (slices).

CommandDescription
slice [ ]List of dynamic length.
append()Appends an element to the slice.

slice [ ]

Try it yourself →

List of dynamic length.

Syntax

name := []type{e1, e2, ...}

Description

Slices are the most commonly used in Go (arrays have a fixed size). Elements are accessed by index starting from 0.

Example

package main
import "fmt"
func main() {
    colors := []string{"red", "purple"}
    fmt.Println(colors[0])
    fmt.Println(len(colors))
}

Expected output

red
2

When to use?

Storing lists.

append()

Try it yourself →

Appends an element to the slice.

Syntax

slice = append(slice, element)

Description

Appends the new element to the end and returns the RESULT — you must reassign it.

Example

package main
import "fmt"
func main() {
    s := []int{1, 2}
    s = append(s, 3)
    fmt.Println(s)
}

Expected output

[1 2 3]

When to use?

Growing lists.

Common mistakes

  • ⚠️ If the result of append is not reassigned, the addition is lost: s = append(s, x).
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code