Golang program to print a slice


In this tutorial, we will learn different methods to print a slice. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas slice is a dynamic array which means its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover they are passed by reference instead by value. Let’s understand this basic concept using different set of examples and algorithms based upon them.

Method 1: Using make function

In this method, we will create a slice with the help of make function and print it on the console using print statement in Go language. The make function is used in creating a slice and returning a reference to it. Let’s dive deep into this example to learn 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

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 function main and in that function create a slice using make function.

  • Step 3 − The make function initializes a slice and contains three arguments where the capacity is adjusted to size of length if length is greater than the capacity.

  • Step 4 − In the example below 2 signifies the length and 4 signifies the capacity.

  • Step 5 − Print the slice using fmt.Println() function in Golang where ln signifies the new line.

Example

Golang program to print a slice by creating it using make function

package main
import "fmt"
func main() {

   slice_demo := make([]int, 2, 4) // create slice using make function

   fmt.Println("The slice created here has a length of:", slice_demo)
}

Output

The slice created here has a length of: [0 0]

Method 2: Using composite literal

In this method, we will see how to print a slice by creating it using composite literals. We will simply set the type of elements we are putting inside the slice and then print the slice using print statement in GO language. Let’s inculcate all this through the algorithm and the code.

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 function main and in that function create a slice using composite literal of type int.

  • Step 3 − Fill the slice with the respective elements which are to be printed on the console.

  • Step 4 − The print statement in Golang is executed using fmt.Println() function where ln means new line.

Example

Golang program to print a slice by creating it using composite literal

package main
import "fmt"
func main() {

   slice_demo := []int{10, 20, 30, 40, 50} //create slice using composite literal
   fmt.Println("The slice after creating it using composite literal:")

   fmt.Println(slice_demo) // print the slice on console
}

Output

The slice after creating it using composite literal:
[10 20 30 40 50]

Method 3: Using append function

In this method, we will learn another way of creating a slice and printing it. The append function is used to add elements to the null slice that is initialized. The slice is printed on the console using print statement in Golang. Let’s see the code to get a crystal- clear understanding of this example.

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes 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

  • 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 function main and in that function create an empty slice and add elements into that slice using append function.

  • Step 3 − The slice is printed on the console using ftm.Println() function where ln stands for new line.

Example

Golang program to print a slice by creating it using append function

package main
import "fmt"
func main() {

   var slice_demo []int
   slice_demo = append(slice_demo, 2) // create slice using append function
   slice_demo = append(slice_demo, 3)
   slice_demo = append(slice_demo, 4)
   slice_demo = append(slice_demo, 5)
   fmt.Println("Slice created using the append function:")

   fmt.Println(slice_demo) // print slice on console
}

Output

Slice created using the append function:
[2 3 4 5]

Conclusion

We executed the program of printing the elements of a slice using three examples. In the first example we used built-in make function and in the second example we used the composite literal function to create a slice and print it. In the third example we have used append function to create a slice. All the examples give similar output. Hence the program executed successfully.

Updated on: 17-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements