You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0 KiB
Go

1 year ago
// Illustrates how slices capacity will increase as elements are added.
package main
import "fmt"
func main() {
var data = []int{1, 2, 3}
fmt.Println(data, len(data), cap(data))
data = append(data, 10)
fmt.Println(data, len(data), cap(data))
data = append(data, 20)
fmt.Println(data, len(data), cap(data))
data = append(data, 30)
fmt.Println(data, len(data), cap(data))
data = append(data, 40)
fmt.Println(data, len(data), cap(data))
//creates a slice of 3 ints using slice literal.
// specifies the indices with values in the slice literal
var x = []int{1, 5: 4, 6, 10: 100, 15}
// [1 0 0 0 0 4 6 0 0 0 100 15]
fmt.Println(x)
// multi-dimensional slice "simulatation"
var y [][]int
fmt.Println(y)
//reading and writing using bracket syntax
y = append(y, []int{1, 2, 3})
fmt.Println(y, len(y), cap(y))
fmt.Println(y[0])
// Forloop
// we can us := inside of functions not in globals of module
numbers := []int{1, 3, 5, 7, 9}
for i := 0; i < len(numbers); i++ {
fmt.Println(numbers[i])
}
}