Arrays, Slices, and Maps in Go

 Arrays, Slices, and Maps in Go


Go has built-in support for arrays, slices, and maps.


An array is a fixed-size sequence of elements of the same type.



var a [3]int           // declare an integer array of size 3

a[0] = 1

a[1] = 2

a[2] = 3

A slice is a dynamic array that can grow or shrink in size. Slices are built on top of arrays, and they provide a more convenient way to work with arrays.



var s []int = []int{1, 2, 3} // declare and initialize a slice

s = append(s, 4)             // add an element to the slice

A map is a collection of key-value pairs.



var m map[string]int = make(map[string]int) // declare and initialize a map

m["one"] = 1

m["two"] = 2

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...