Golang program to convert array into 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.

Method 1: Using [:]

In this method, we will using [:] for slicing an array. After the conversion of array into slice, we will print the output on the console using print statement in Golang.

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 an array with a fixed constant value and put some values inside that array which is to be converted to slice.

  • Step 3 − The array [:] syntax converts the array into slice and stores the values inside the slice named myslice.

  • Step 4 − Print the slice on the console as the program is executed.

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

Example

In the following example, we will use [:] for slicing while using array

package main
import "fmt"
func main() {
   array := [4]int{10, 20, 30, 40} //create an array with four elements
   myslice := array[:]// Convert the array to a slice
   fmt.Println("The array converted to slice is presented here:")
   fmt.Println(myslice) //print the slice
}

Output

The array converted to slice is presented here:
[10 20 30 40]

Method 2: Using Copy And Make Function

In this method, a function will create a new slice in which array elements will be copied using copy function which is also a type of built-in function.

Syntax

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.

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 an array inside the main function with a fixed constant value and put some values inside that array which is to be converted to slice.

  • Step 3 − Create an empty slice using make function equal to length of array whose elements are to be copied to that slice.

  • Step 4 − Use copy function to copy the values of array to the new slice created and the purpose of program is satisfied.

  • Step 5 − The slice will be printed on the screen using fmt.println() function where ln means new line.

Example

In the following example, we are using copy and make function to convert array into slice

package main
import "fmt"
func main() {
   // Declare an array of integers with 5 elements
   array := [4]int{10, 20, 30, 40}
   
   // Convert the array to a slice using the make function
   myslice := make([]int, len(array))
   copy(myslice, array[:])
   fmt.Println("The array after converted to slice is presented as:")
   fmt.Println(myslice) //print slice
}

Output

The array after converted to slice is presented as:
[10 20 30 40]

Method 3: Using Append() Function

In this method, we will convert the array into slice using the built-in append function in for loop to append array elements in slices. The output will be printed on the console using print statement in Golang.

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 an array inside the main function with a fixed constant value and put some values inside that array which is to be converted to slice.

  • Step 3 − Create an empty slice and use for loop till the range of array , in the loop append the array elements in the slice and keep repeating until the loop is terminated.

  • Step 4 − After the loop is terminated print the slice on the console using fmt.Println() function where ln means new line.

Example

Golang program to convert array into slice using the built-in append function in the example

package main
import "fmt"
func main() {
   array := [4]int{10, 20, 30, 40} //create an array of size 4
   var myslice []int
   for _, element := range array {
      myslice = append(myslice, element) //run a for loop to append elements
   }
   
   // Print the slice
   fmt.Println("The array after being converted to slice is:")
   fmt.Println(myslice) //print slice
}

Output

The array after being converted to slice is:
[10 20 30 40]

Conclusion

In the above program, we used three examples to convert array into slice. In the first example we used a syntax in the method to convert array into slice. In the second example we used make function to execute program and in the third example we used append function. Hence, program executed successfully.

Updated on: 13-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements