Table of contents
No headings in the article.
A slice is defined as a continuous segment of an underlying array and it provides access to a numbered sequence of elements from that array.
Slices provide access to parts of an array in sequential order.
Slices are more flexible and more powerful than arrays since arrays had limitations of being fixed size.
Unlike array they are variable type (elements can be added or removed)
Due to this they offer more flexibility as compared to arrays.
Components of a slice
Slice has 3 major component Pointer ,length ,capacity.
Pointer are just variables that hold memory addresses.
The Length is number of elements it contains.
The length and capacity can be obtain by len() cap()
declaring and initializing a slice
slice_name := []data_type{values}
grades := []int{10, 20, 30}
main.go
package main
import "fmt"
func main() {
slice := []int{10, 20, 30}
fmt.Println(slice)
}
>>> go run main.go
[10 20 30]
main.go
package main
import "fmt"
func main() {
arr := [10]int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
slice := arr[1:8]
fmt.Println(slice_1)
}
>>> go run main.go
[20, 30, 40, 50, 60, 70, 80]
main.go
package main
import "fmt"
func main() {
arr := [10]int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
slice := arr[1:8]
fmt.Println(slice)
sub_slice := slice[0:3]
fmt.Println(sub_slice)
}
>>> go run main.go
[20, 30, 40, 50, 60, 70, 80]
[20 30 40]
make function
main.go
package main
import "fmt"
func main() {
arr := [10]int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
slice := make([]int, 5, 8)
fmt.Println(slice)
fmt.Println(len(slice))
fmt.Println(cap(slice))
}
>>> go run main.go
[20, 30, 40, 50, 60, 70, 80]
[20 30 40]
capacity function
main.go
package main
import "fmt"
func main() {
arr := [10]int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
slice := arr[1:8]
fmt.Println(cap(arr))
fmt.Println(cap(slice))
}
>>> go run main.go
10
9
slice and index numbers
main.go
package main
import "fmt"
func main() {
arr := [5]int{10, 20, 30, 40, 50}
slice := arr[:3]
fmt.Println(arr)
fmt.Println(slice)
slice[1] = 9000
fmt.Println("after modification")
fmt.Println(arr)
fmt.Println(slice)
}
>>> go run main.go
[10 20 30 40 50]
[10 20 30]
after modification
[10 9000 30 40 50]
[10 9000 30]
appending to a slice
func append(s []T, vs ...T) []T
slice = append(slice, element-1, element-2)
slice = append(slice, 10, 20, 30)
main.go
package main
import "fmt"
func main() {
arr := [4]int{10, 20, 30, 40}
slice := arr[1:3]
fmt.Println(slice)
fmt.Println(len(slice))
fmt.Println(cap(slice))
slice = append(slice, 900, -90, 50)
fmt.Println(slice)
fmt.Println(len(slice))
fmt.Println(cap(slice))
}
>>> go run main.go
[20 30]
2
3
[20 30 900 -90 50]
5
6
deleting from a slice
main.go
package main
import "fmt"
func main() {
arr := [10]int{10, 20, 30, 40, 50}
i := 2
fmt.Println(arr)
slice_1 := arr[:i]
slice_2 := arr[i+1:]
new_slice := append(slice_1, slice_2...)
fmt.Println(new_slide)
}
>>> go run main.go
[10 20 30 40 50]
[10 20 40 50]
copying from a slice
func copy(dst, src [] Type) int
num := copy(dest_slice, src_slice)
main.go
package main
import "fmt"
func main() {
src_slice := []int{10, 20, 30, 40, 50}
dest_slice := make([]int, 3)
num := copy(dest_slice, src_slice)
fmt.Println(dest_slice)
fmt.Println("Number of elements copied: ", num)
}
>>> go run main.go
[10 20 30]
Number of elements copied: 3
looping through a slice
main.go
package main
import "fmt"
func main() {
arr := [10]int{10, 20, 30, 40, 50}
for index, value := range arr {
fmt.Println(index, "=>", value)
}
}
>>> go run main.go
0 => 10
1 => 20
2 => 30
3 => 40
4 => 50
main.go
package main
import "fmt"
func main() {
arr := [10]int{10, 20, 30, 40, 50}
for _, value := range arr {
fmt.Println(value)
}
}
>>> go run main.go
10
20
30
40
50
That's great if you have make till here you have covered basic of Golang Slices.
If you liked what you read, do follow and any feedback for further improvement will be highly appreciated!
Thank you and Happy Learning!👏