Golang Program to Create Slice Using Composite Literal


In Go programming language, a composite literal is used to create a slice, array, struct etc object of type given and initializes it. In this article, we will create a slice using three examples. In the first example, slices of numbers will be created, in the second example, slices of strings will be created and in the third example, built-in functions are used to create slices. In this way the creation of slices is demonstrated. Let’s see the examples to get a crystal-clear view of the concept.

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map.It accepts the type of variable to be created, its size and capacity as arguments.

funcappend(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes a number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

  • Import the required packages in the program

  • Create a slice using composite literals and add values in the slice

  • Print the slice on the console

  • The print statement is executed using Println function where ln means new line

Example 1

In this example, a main function is created in which a slice of integers is created using the composite literal. The output will be the slice printed on the console using Println function from the fmt package

package main

import "fmt"

func main() {
   slice := []int{10, 20, 30, 40, 50}

   fmt.Println("The values of the slice are:")
   fmt.Println(slice)
}

Output

The values of the slice are:
[10 20 30 40 50]

Example 2

In this illustration, a slice of chocolates string will be created inside the main using composite literal which will be printed later on the console using Println function from the fmt package where ln means new line.

package main

import "fmt"

func main() {
   chocolates := []string{"Munch", "Perk", "5star", "Dairymilk"}

   // Print the slice to the console
   fmt.Println("The chocolates in the slice are:")
   fmt.Println(chocolates)
}

Output

The chocolates in the slice are:
[Munch Perk 5star Dairymilk]

Example 3

In this example, a slice is created using the make function and the size of the slice is also set. Later, the elements are added in the slice using the append function and then finally the slice is printed on the console.

//Golang program to create slice using composite literal
package main

import "fmt"

func main() {
   values := make([]int, 0, 5)

   values = append(values, 10, 20, 30, 40, 50)

   // Print the slice on the console
   fmt.Println("The values of the slice are:")
   fmt.Println(values)
}

Output

The values of the slice are:
[10 20 30 40 50]

Conclusion

We compiled and executed the program of creating a slice using composite literal with the help of three examples. In the first example, slice of numbers was created using composite literals, in the second example, slice of strings was created and in the third example built-in functions are used to execute operations.

Updated on: 20-Jul-2023

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements