Array and Slice in Golang (Basic)

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.

--

--

--

Break the rules act like a fool -Guruji

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

Recommended from Medium

Jenkins and its Industrial Use Cases

Entering Digits

Multipass — A linux VM orchestrator, simples and fast

Diagnose and edit your data files with CSV Studio

How to Improve the Performance of Old iPhone or Android Phone

[Road to Cloud Security Engineer] Series 1 ~Understand Microservices~

#40 — One minute a week, Learn web design a bit

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
Takdirul Islam Sisir

Takdirul Islam Sisir

Break the rules act like a fool -Guruji

More from Medium

Sorting Algorithms: Insertion Sort in Golang

Golang pprof singleton performance

GoFrame 101: Swagger UI on GoFrame framework

How To Upgrade An Indirect Dependency In GoLang