Array and Slice in Golang (Basic)

Takdirul Islam Sisir
2 min readOct 17, 2021

--

Array Syntax :

Like most programming languages Go also has arrays, but arrays are rarely used in Go.

/* Declearing array here [5] is the limit of array, int is the type of elements and {elements} */ var arry = [5]int {1,2,3,4,5} /* Declearing array here [...] means array limit is not fixed, int is the type of elements and {elements} */ var arry = [...]int {1,2,3,4,5,6,... ...}

Slice Syntax :

In Go language we use a slice more than an array, it holds the sequence of values. Slice removes the limitations of arrays.

// One dimensional Slice. var slice = []int {1,2,3,4,5} // Multidimensional Slice. var multi_slice = [][]int

Difference between Array and Slice :

Using [...] makes an Array and using [] makes a Slice

// Array var arry = [5]int {1,2,3,4,5} //Slice var slice = []int {1,2,3,4,5}

Slicing the Slice :

package main
import ( "fmt" )
func main() { var x = []int {4, 3, 4, 5, 6, 7}
fmt.Println(x[:4])
}
//Output : 4 3 4 5

Note: When we are slicing the slice we must use a Positive number cause the index number should be a positive number other wise it will casue errors.

Append Function :

The built-in append function is used for appending data in Slice.

package main 
import ( "fmt" )
func main() {
var x []int
x = append(x, 4, 3, 4, 5, 6, 7)
fmt.Println(x)
}// Output : 4 3 4 5 6 7

Let me know if there is any miss information or mistake, I just moved to Go from Python.

--

--