Slices

Slice is very powerful data structure of Go. Unlike array they don’t have fixed-length. They are very flexible and can grow and shrink according your program need.

Slices is an abstract form on underlying array.

A slice has three parts
1. Pointer: Points to the first element of an array that can be accessed through slice.
2. Length: Number of elements in slice
3. Capacity: number of elements between start of slice and end of underlying array

You can use inbuilt functions len and cap to get length and capacity of a slice respectively.

Declaration and initialization of a slice

example.go Run Code

package main

import (
"fmt"
)

func main() {
// A nil slice (declaration without initization)
var nums []int
fmt.Println(nums)

// Declare a slice using slice literal
nums1 := []int{} // empty slice
fmt.Println(nums1)

nums1 = []int{10, 20, 30}
fmt.Println(nums1)

// Declare a slice using slice literal
nums2 := make([]int, 0) // empty slice
fmt.Println(nums2)

nums2 = make([]int, 2) // slice with length 2
fmt.Println(nums2)
}

E:\golang\excercise > go run example.go
[]
[]
[10 20 30]
[]
[0 0]

len and cap functions

example.go Run code

package main

import (
"fmt"
)

func main() {
// Declare a slice of integer type
nums := make([]int, 2) // length and capacity of slice is equal here i.e. 2
fmt.Printf("Length = %d, Capacity = %d\n", len(nums), cap(nums))

nums1 := make([]int, 2, 5)
fmt.Printf("Length = %d, Capacity = %d\n", len(nums1), cap(nums1))
}

E:\golang\excercise > go run example.go
Length = 2, Capacity = 2
Length = 2, Capacity = 5

Slice of a slice

example.go Run code

package main

import (
"fmt"
)

func main() {
// Use slice on slice
nums := []int{10, 20, 50, 30, 40, 80, 60, 70, 100, 90} // Length is 10, Capacity is 10, assume capacity, c = 10

// Create a new slice
newNums := nums[2:5] // Assume, i = 2 and j = 5

// Calculate length and capacity of newNums
// Length is j - i
// Capacity is c - i

l := 5 - 2
c := 10 - 2

fmt.Printf("Length = %d, Capacity = %d\n", l, c)
fmt.Printf("Length = %d, Capacity = %d\n", len(newNums), cap(newNums))

// Do more slicing on a slice
newNums = nums[:7]
fmt.Println(newNums)

newNums = nums[2:]
fmt.Println(newNums)

newNums = nums[:]
fmt.Println(newNums)
}

E:\golang\excercise > go run example.go
Length = 3, Capacity = 8
Length = 3, Capacity = 8
[10 20 50 30 40 80 60]
[50 30 40 80 60 70 100 90]
[10 20 50 30 40 80 60 70 100 90]

use of append function

example.go Run code

package main

import (
"fmt"
)

func main() {
// Use append

nums := make([]int, 3)
nums[1] = 10

fmt.Println(nums) // [0 10 0]

nums = append(nums, 40)
fmt.Println(nums) // [0 10 0 40]

nums = append(nums, 1, 2)
fmt.Println(nums) // [0 10 0 40 1 2]

}

E:\golang\excercise > go run example.go
[0 10 0]
[0 10 0 40]
[0 10 0 40 1 2]

Iterate over a slice

example.go Run code

package main

import (
"fmt"
)

func main() {
// Create a slice on string
s := []string{"Hi", "my", "name", "is", "bhawani"}

// Iterate over a slice using range
for i, v := range s {
fmt.Printf("index: %d, value: %s\n", i, v)
}

// Iterage over a slice using fool loop
for i := 0; i < len(s); i++ {
fmt.Printf("index: %d, value: %s\n", i, s[i])
}
}

E:\golang\excercise > go run example.go
index: 0, value: Hi
index: 1, value: my
index: 2, value: name
index: 3, value: is
index: 4, value: bhawani
index: 0, value: Hi
index: 1, value: my
index: 2, value: name
index: 3, value: is
index: 4, value: bhawani

Leave a Reply

Your email address will not be published. Required fields are marked *