51 lines
814 B
Go
51 lines
814 B
Go
package learning
|
|
|
|
import "fmt"
|
|
|
|
func SlicesLearning() {
|
|
primes := [6]int{2, 3, 5, 7, 11, 13}
|
|
var s []int = primes[1:4]
|
|
var ss []int = primes[:]
|
|
fmt.Println(s)
|
|
fmt.Println(ss)
|
|
}
|
|
|
|
func SliceEnhanced() {
|
|
s := []int{2, 3, 5, 7, 11, 13}
|
|
printSlice(s)
|
|
|
|
// Slice the slice to give it zero length.
|
|
s = s[:0]
|
|
printSlice(s)
|
|
|
|
// Extend its length.
|
|
s = s[:4]
|
|
printSlice(s)
|
|
|
|
// Drop its first two values.
|
|
s = s[2:]
|
|
printSlice(s)
|
|
}
|
|
|
|
func printSlice(s []int) {
|
|
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
|
|
}
|
|
|
|
func SlicesMake() {
|
|
a := make([]int, 5)
|
|
printSliceMake("a", a)
|
|
|
|
b := make([]int, 0, 5)
|
|
printSliceMake("b", b)
|
|
|
|
c := b[:2]
|
|
printSliceMake("c", c)
|
|
|
|
d := c[2:5]
|
|
printSliceMake("d", d)
|
|
}
|
|
|
|
func printSliceMake(s string, x []int) {
|
|
fmt.Printf("%s len=%d cap=%d %v\n",
|
|
s, len(x), cap(x), x)
|
|
}
|