slice in golang
// Use of golang slice
func main() {
// initialise the slice with 10 zero ints
slice := make([]int, 10)
// fillup with random ints
fillSliceWithRand(slice)
// prints the slice
print(slice)
// passes the slice by reference and reverse the slice
reverse(slice)
// prints the slice again
print(slice)
}
// reverse method reverse the slice
func reverse(a []int) {
l := len(a)
for i := 0; i < l/2; i ++ {
x := a[l-i-1]
a[l-i-1] = a[i]
a[i] = x
}
}
// print method prints all ints of the slice
func print(a []int) {
for _, v := range a {
fmt.Printf("%d ", v)
}
fmt.Println()
}
// fillSliceWithRand method fills the slice with random ints
func fillSliceWithRand(a []int) {
for i := 0; i < len(a); i++ {
a[i] = rand.Intn(100)
}
}
/**
## Slice in golang
- Slices hold references to an underlying array, and if you
assign one slice to another, both refer to the same array.
- If a function takes a slice argument, changes it makes to
the elements of the slice will be visible to the caller,
analogous to passing a pointer to the underlying array.
**/
4.17
6
b := [2]string{"Penn", "Teller"}
Thank you!
6
0
4.13
8
package main
import (
"fmt"
)
func main() {
var slice = []int{1, 2, 3, 4, 5}
var store = make([]int, 3)
push := append(slice, 6, 7, 8, 9)
copy(store, slice)
fmt.Println("before append value", slice)
fmt.Println("after append value", push)
fmt.Println("render value all value exist", push[:])
fmt.Println("render value after second value", push[2:])
fmt.Println("render value before third index", push[:3])
fmt.Println("render value start from zero and last value before second index", push[0:2])
fmt.Println("merge slice value", store)
fmt.Println("read size slice", len(slice))
fmt.Println("read capacity slice", cap(slice))
}
Thank you!
8
0
4.2
10
func main() {
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)
}
Thank you!
10
0
3.57
7
// loop over an array/a slice
for i, e := range a {
// i is the index, e the element
}
// if you only need e:
for _, e := range a {
// e is the element
}
// ...and if you only need the index
for i := range a {
}
// In Go pre-1.4, you'll get a compiler error if you're not using i and e.
// Go 1.4 introduced a variable-free form, so that you can do this
for range time.Tick(time.Second) {
// do it once a sec
}
Thank you!
7
0
Are there any code examples left?
New code examples in category Go
-
Go 2022-03-27 22:30:23 golang check if ip is v6
-
Go 2022-03-27 16:20:18 make image go to center of page
-
Go 2022-03-24 15:20:01 why does my discord bot not work when i take code from googl
-
Go 2022-03-21 18:40:22 pass method as props vue
-
Go 2022-03-19 23:40:33 reverse a string in golang
-
Go 2022-03-18 12:45:06 golang while loop
-
Go 2022-03-17 19:30:08 instancing in godot
-
Go 2022-03-17 09:55:17 brew upgrae golang-migrate
-
Go 2022-03-17 06:15:06 Tushar Jadhav
-
Go 2022-03-17 00:00:12 golang cwd