GOLang Slices Guide

slc1 := []int{58, 69, 40, 45, 11, 56, 67, 21, 65}slc2 := make([]int, 5)var slc3 []int
arr := []int{1,2,3,4,5}var slc1 = arr[1:3] //between indexes range: [2,3]slc2 := arr[0:] //0(include) to length of array: [1,2,3,4,5]slc3 := arr[:2] //upto index 2(exclude): [1,2]slc4 := arr[:] // create entire array as slice: [1,2,3,4,5] 
orgnlSlc := []int{1,2,3,4,5}var slc1 = orgnlSlc[1:4] //between indexes range: [2,3,4]slc2 := orgnlSlc[0:] //0(include) to length of array: [1,2,3,4,5]slc3 := orgnlSlc[:4] //upto index 4(exclude): [1,2,3,4]slc4 := orgnlSlc[:] // create entire array as slice: [1,2,3,4,5]slc5 := slc3[2:4] //between indexes range: [3,4]
slc1 := make([]int, 3, 6)
// Using len function........for i := 0; i < len(originalSlice); i++ {    fmt.Println(originalSlice[i])}//Using range......for index, element := range originalSlice {fmt.Printf("Index = %d and element = %s\n", index, element)}//With out index.......for _, ele := range originalSlice {    fmt.Printf("Element = %s\n", ele)}
arr := [6]int{1, 4, 3, 5}slc := arr[0:3]
// Before modifying
fmt.Println("Original_Array: ", arr) //[1,4,3,5]
fmt.Println("Original_Slice: ", slc) //[1,4,3]
/ After modification// these changes will effect on array aswell because slice is a reference type
slc[0] = 100
slc[1] = 200
slc[2] = 300
fmt.Println("\nNew_Array: ", arr) //[100,200,300,5]
fmt.Println("New_Slice: ", slc) //[100,200,300]
If you try to compare two slices with the help of == operator then it will give you an error.You can only use == operator to check the given slice is nill or not.func compareSlices(slice1, slice2 []int) {fmt.Println(slice1 == nil)// fmt.Println(slice1 == slice2)// this line gives error}//Note: If you want to compare two slices, then use range for loop to match each element or you can use DeepEqual function.
// multi-dimensional slice
multiDimnsnSlc1 := [][]int{{1, 2, 3}, {4, 7, 6}, {8, 5, 9}}
// Diffrent slices....
slc1 := []int{1,2,3,4,5,6}
slc2 := []int{8,9,7}
slc3 := make([]int, 3)
var slc4 []int
fmt.Println("Slice_1:", slc1) //[1,2,3,4,5,6]
fmt.Println("Slice_2:", slc2) //[8,9,7]
fmt.Println("Slice_3:", slc3) //[0,0,0,0]
fmt.Println("Slice_4:", slc4) //[]
copy1 := copy(slc4, slc1)
fmt.Println("No of elements copied into Slice_4 : ", copy1)//0
//nothing is coppied because slc4 is nill
fmt.Println("Slice_4 after copy: ", slc4)
copy2 := copy(slc3, slc1)
fmt.Println("No of elements copied into Slice_3 : ", copy2) //3
fmt.Println("Slice_3 after copy: ", slc3) //[1,2,3,4]
copy3 := copy(slc2, slc1)
fmt.Println("No of elements copied into Slice_2 : ", copy3) //3
fmt.Println("Slice_2 after copy: ", slc2) //[1,2,3]
func main() {// declaring a slice of slices of type integer with a length of 3.slice_of_slices := make([][]int , 3)for i := 0; i < 3; i++ { // looping through the slice to declare slice of slice of length 3
slice_of_slices[i] = make([]int, 3)
// assigning values to each slice of a slice
for j := 0; j < 3; j++{
slice_of_slices[i][j] = i * j
}
}// printing the slice of slices matrix
for i := 0; i < 3; i++ {
fmt.Println(slice_of_slices[i])
}
fmt.Println("Slice of slices: ", slice_of_slices)
}
func main() {
// declaring a slice of slices of type integer with a length of 3
slice_of_slices := make([][]int , 3) for i := 0; i < 3; i++ {
new_length := i * 2 + 1
// looping through the slice to declare slice of slice of a variable lengthslice_of_slices[i] = make([]int, new_length)// assigning values to each slice of a slice for j := 0; j < new_length; j++{
slice_of_slices[i][j] = i * j + 1
}
}// printing the slice of slices matrixfor i := 0; i < 3; i++ {
fmt.Println(slice_of_slices[i])
}
fmt.Println("Slice of slices: ", slice_of_slices)}
slc2 := []int{78, 50,70} //create slicefmt.Println("Slc1 before append :", slc1) //[78,50,70]slc1 = append(slc1, 10, 20)fmt.Println("Slc1 after append :", slc1) //[78,50,70,10,20]
fmt.Println("before update : ", slc1) //[78,50,70]slc1[2] = 27 //updated index 2 value(70) in slc1 to 27fmt.Println("After update : ", slc1) //[78,50,27]
fmt.Println(slc1[0]) // 78
fmt.Println(slc1[1]) // 50
fmt.Println(slc1[0:3]) // [78,50,70]
slc2 := []int{2,3,7}
fmt.Println("Before Append : ", slc2)//[2,3,7]
slc2 = append(slc2, slc1...)
fmt.Println("After Append : ", slc2)//[78,50,70,2,3,7]
slc1 = removeIndex(slc1, index)
func removeIndex(slc1 []int, index int) []int {
return append(slc1[:index], slc1[index+1:]...)
}
func removeIndex(slc1 []int, index int) []int {
return append(slc1[:index], slc1[index+1:]...)
}
func isItemExists(slc1 []int, item int) bool {
for _, ele := range slc1 {
if ele == item {
return true
}
}
return false
}

--

--

--

Full Stack Software Developer| Programming enthusiast | Loves DS-Algo and System Design

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Part Two: The Basics of Programming and Software

Compliance Documents for your Tenancy on Oracle Cloud

Can You Teach C# as a First Language for Kids?

Extracting more values from application logs

How to Use the 4chan Json API With Python — Building a Dataset for Machine Learning Part 1

Astronaut crawling up mountain on mars with space ship behind him.

Demystifying the ways of creating partitions in Glue Catalog on partitioned S3 data for faster…

Day in the Life at Flatiron School

Ruby on Rails — Testing and code quality (part 4)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sivareddy Uppathi

Sivareddy Uppathi

Full Stack Software Developer| Programming enthusiast | Loves DS-Algo and System Design

More from Medium

Golang language variable declaration methods and use scenarios

In-depth analysis of Golang memory escape

Golang Cheatsheet — Variable

Golang Intellij Idea setup