Files
go-playground/go-by-example/slices/slices.go
2026-03-01 21:03:32 +01:00

49 lines
763 B
Go

package main
import (
"fmt"
// "slices"
)
func main() {
var s []string
fmt.Println("uninit", s, s == nil, len(s) == 0)
s = make([]string, 3)
fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s))
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("set:", s)
fmt.Println("get:", s[2])
s = append(s, "d")
s = append(s, "e", "f")
fmt.Println("apd:", s)
f := make([]int, 3, 10)
fmt.Println("f:", f, "len:", len(f), "cap:", cap(f))
c := make([]string, len(s))
copy(c, s)
fmt.Println("cpy:", c)
l := s[2:5]
fmt.Println("sl1:", l)
// two d slices
twoD := make([][]int, 10)
for i := range len(twoD) {
innerLen := i + 1
twoD[i] = make([]int, innerLen)
for j := range innerLen {
twoD[i][j] = 1 + j
}
}
fmt.Println("2d: ", twoD)
}