Golang program to use different types of a collection


In Golang, a collection is a data structure that stores a number of elements, usually of the same type. The collection types in Go offers arrays, slices, maps, and channels. Slices are considered as dynamic arrays whose size can be changed whereas arrays have a fixed size. Channels give goroutines a method to communicate and synchronize their execution, whereas maps are used for key-value storage. Here, we will show execution of maps, slices and arrays using different ways as shown below in examples.

Method 1: Using map, slice and array

This method shows how to use arrays, slices, and maps in Go. An array that may be dynamically resized and used to store elements of any type is called a slice. A map is a group of key-value pairs with a unique key for each value. A fixed-size, arranged collection of identical-type elements is known as an array.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function make a slice of countries of the type []string and give it a few elements.

  • Step 3 − Print the slice on the console using Print ln statement.

  • Step 4 − Make a map of type map[string]int called Map and give it a few key-value pairs.

  • Step 5 − Use the Print ln function to print the map Map.

  • Step 6 − Make a [3]int type number array and add a few elements to it.

  • Step 7 − Use the Print ln function to output the array's integers.

  • Step 8 − With the help of this technique, you can see how to generate and use several collections in Go, including slices, maps, and arrays.

Example

In this example we will use slice, map and array to demonstrate different types of collection.

package main
import "fmt"

func main() {
   // slice
   countries := []string{"Canada", "Italy", "India"}
   fmt.Println("The slice created here is:", countries)

   // map
   Map := map[string]int{"Canada": 3, "Italy": 2, "India": 1}
   fmt.Println("The map created here is:", Map)
   
   // array
   var values [3]int
   values[0] = 10
   values[1] = 20
   values[2] = 30
   fmt.Println("The array created here is:", values)
}

Output

The slice created here is: [Canada Italy India]
The map created here is: map[Canada:3 India:1 Italy:2]
The array created here is: [10 20 30]

Method 2: Using slice, map and array composite literals

This method depicts an alternate approach for accessing various collections in Go, which initializes slices, maps, and arrays using composite literals.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function using a composite literal, make a slice countries of type []string and attach some elements to it.

  • Step 3 − Use the Print ln function to print the slice countries.

  • Step 4 − Using a composite literal, make a map Map of type map[string]int and give it a few key-value pairs.

  • Step 5 − Use the Print ln function to print the map Map.

  • Step 6 − Using a composite literal, make a variable of type [3]int and assign some elements to it.

  • Step 7 − Use the Print ln function to output the array's integers.

  • Step 8 − The code for this technique is shorter and easier to read than the previous one since it initializes slices, maps, and arrays using composite literals.

Example

In this example we will use slice, map and array composite literals to execute the program.

package main
import "fmt"

func main() {
   // slice using composite literal
   countries := []string{"Canada", "Italy", "India"}
   fmt.Println("The slice created here is:", countries)

   // map using composite literal
   Map := map[string]int{"Canada": 3, "Italy": 2, "India": 1}
   fmt.Println("The map created here is:", Map)

   // array using composite literal
   values := [3]int{10, 20, 30}
   fmt.Println("The array created here is:", values)
}

Output

The slice created here is: [Canada Italy India]
The map created here is: map[Canada:3 India:1 Italy:2]
The array created here is: [10 20 30]

Conclusion

We executed the program of using different types of collection using two methods. In the first method we used slice, array and map and in the second example we used similar collections constructed with the help of composite literals.

Updated on: 22-Feb-2023

729 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements