Operator and syntax

Go index and slice expressions

Indexing accesses elements or bytes. A slice expression selects a range with exclusive high bound; the full form controls capacity and is not permitted on strings. Bounds errors panic for runtime indices. Example output: '1 [2 3] 2 2'.

Formal Syntax & Structure

go syntaxgo
a[i]; a[low:high]; a[low:high:max]

Working Example

Example Snippetgo
package main
import (  "fmt")
func main() {  a := []int{1, 2, 3, 4}  b := a[1:3:3]  fmt.Println(a[0], b, len(b), cap(b))}
Go Reference