#golang #go #slices
Slices - Part II
-----------------
1.Append values to a slice
2.Append to a slice from a slice
3.Iterating slice - for.. range
slicedemo.go
-------------
package main
import "fmt"
func main(){
names:=[]string{"one","two","three","four"} //length and capacity is 4
names=append(names,"five")//length is 5, capacity got doubled and is now 8
fmt.Printf("The length of names is %d and capacity of names is %d\n",len(names),cap(names))
}
appendslicedemo.go
-------------------
package main
import "fmt"
func main(){
x:=[]int{10,20,30}
y:=[]int{40,50,60}
fmt.Println(append(x,y...)) //Values from both the slices were appended
//Use For loop
fmt.Println(x[i])
//For range loop - for each loop
for i, value := range y { //"value" is the variable which can hold or contain the 'i'th value from slice "y", where "i" is the index of iterative for loop
fmt.Printf("index is %d, value is %d\n",i,value)
}
}
Watch video Go (golang) Tutorials - Slices Part II online, duration hours minute second in high quality that is uploaded to the channel Ambasoft Java 27 March 2018. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 141 times and liked it 4 visitors.