Golang program to convert slice into array


A slice can also be called as a dynamic array as its value is dynamic whereas a normal array is static. This makes the slice more efficient and faster. They are passed by reference instead of value. Here we are going to learn different techniques of converting a slice into array using various examples.

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.

func copy(dst, str[] type) int

The copy function in go language is used to copy the values of one source array to the destination array and returns the number of elements copied as the result. It takes the two arrays as an argument.

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 slice and add some values in the slice using append function.

  • Step 3 − Initialize an array with a size so that values can be copied into it.

  • Step 4 − Use a copy function to copy the elements of slice into the array.

  • Step 5 − Print the array on the console using fmt.Println() function where ln means new line.

Example 1

In this example we will learn how to convert slice into array using a built-in function copy. Here, we will copy the slice elements into the array created. Let’s deep dive into the code and algorithm to see how it’s done.

package main
import "fmt"

//create main function to execute the program
func main() {
   var slice []int // initialize slice
   slice = append(slice, 10) //fill the slice using append function
   slice = append(slice, 20)
   slice = append(slice, 30)
   
   // Convert the slice to an array
   array := [3]int{} //initialized an empty array
   copy(array[:], slice) //copy the elements of slice in newly created array
   fmt.Println("The slice is converted into array and printed as:")
   fmt.Println(array) // prints the output: [10 20 30]
}

Output

The slice is converted into array and printed as:
[10 20 30]

Example 2

In this example we will see how we can convert slice into the array using for loop. The elements of slice will be assigned to array. Let’s look through the code to get our concept crystal-clear.

package main
import "fmt"

//create main function to execute the program
func main() {
   var slice []int // initialize slice
   slice = append(slice, 10) //fill the slice using append function
   slice = append(slice, 20)
   slice = append(slice, 30)
   
   // Convert the slice to an array
   var array [3]int
   for i, element := range slice {
      array[i] = element // store slice elements in the newly created array
   }
   fmt.Println("The array is printed after conversion from slice:")
   fmt.Println(array) // prints the output: [1 2 3]
}

Output

The array is printed after conversion from slice:
[10 20 30]

Conclusion

In the above program, we used two examples to convert slice into the array. In the first example we used copy function to get an array. In the second example we used for loop and two built-in functions are used in above examples-append and copy. Append is used to add elements in the slice. Hence, program executed successfully.

Updated on: 13-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements