
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Add Elements to a Slice
In this tutorial, we will learn how to add elements to a slice using different examples. 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 go through the example to understand things.
Method 1: Using append function with strings
In this method, we will use append function to add string elements to the slice. We have used an append function which is a built-in function whose features are described below. Let's have a look and see how its executed.
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 a slice with name countries and print it on console.
Step 3 ? Now use append function to add new string elements to the slice and assign this to a variable named new_countries.
Step 4 ? Print the value stored in new_countries on console using fmt.Println() function where ln refers to new line.
Example
Golang program to add elements in a slice using append function with strings
package main import "fmt" func main() { // create a slice of type string Countries := []string{"Iceland", "Switzerland"} fmt.Println("The slice before adding of elements is:", Countries) // append new elements and old slice in new slice new_countries := append(Countries, "Canada", "Italy") fmt.Println("The new slice after adding elements is:") fmt.Println(new_countries) // print new slice }
Output
The slice before adding of elements is: [Iceland Switzerland] The new slice after adding elements is: [Iceland Switzerland Canada Italy]
Method 2: Using copy function
Syntax
In this method, we will use copy function to add elements to the slice. The copy function is a built-in function whose features are described below. Let's inculcate the steps required to execute the program.
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 function main and in that function create a slice with name countries and print it on console.
Step 3 ? Create another slice of type string and copy its contents to the old slice.
Step 4 ? Print the contents of old slice after elements are copied on the screen using fmt.Println() function.
Example
Golang program to add elements in a slice using copy function
package main import "fmt" func main() { // create a slice of type string countries := []string{"Canada", "Italy"} fmt.Println("The slice before adding element is:", countries) morecountries := []string{"Finland", "Switzerland"} // create a new slice to copy the elements of slice new_slice := copy(countries, morecountries) fmt.Println("The slice after adding element in slice is:") fmt.Println("The new slice we created has", new_slice, "elements->", countries) }
Output
The slice before adding element is: [Canada Italy] The slice after adding element in slice is: The new slice we created has 2 elements-> [Finland Switzerland]
Method 3: Using append function with integer values
In this method, we will use append function to add integer elements to the slice. We have used an append function which is a built-in function whose features are described below. Let's have a look and see how its executed.
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 a slice with name numbers and print it on console.
Step 3 ? Now use append function to add new integer elements to the slice and assign this to a variable named new_numbers.
Step 4 ? Print the value stored in new_numbers on console using fmt.Println() function where ln refers to new line.
Example
package main import "fmt" func main() { // create a slice of type int numbers := []int{1, 2} fmt.Println("The slice before adding of elements is:", numbers) // append new elements in the slice new_numbers := append(numbers, 3, 4) fmt.Println("The new slice after adding elements is:") // print new slice fmt.Println(new_numbers) }
Output
The slice before adding of elements is: [1 2] The new slice after adding elements is: [1 2 3 4]
Conclusion
We executed the program of adding the elements to a slice using three examples. In the first example we have used an append function to add the string elements, in the second example we have used the copy function to add values and in the third example we have used an append function to add the integer elements.Both the examples give similar output. Hence the program executed successfully.