- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 replace elements in a slice
In this tutorial,, we will grasp how to replace elements in a slice using different set of examples. A slice is a dynamic array which means that its value is not fixed like array. The output will be printed on the screen using fmt.Println() function. Let’s see how it can be implemented with crystal-clear examples.
Method 1: Using built-in copy function
In this method, we will use the built-in function copy to replace elements in slice which means at the place of original element and new element will be placed. The built-in functions shorten the code and easily solve the problems. All the outputs will be printed on the console using fmt.Println() function. Let’s go through it.
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 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 main function and in that function create two slices using append function named array1 and array2.
Step 3 − Use copy function with two parameters destination as array1 and source as array2.
Step 4 − Copy the elements from the array2 in array1 from second index and print the array1 on the console.
Step 5 − The print statement is executed using fmt.Println() function where ln refers to next line.
Example
Golang program to replace elements in slice using built-in copy function
package main import "fmt" // create main function to execute the program func main() { var array1 []int // create slice1 var array2 []int // create slice2 array1 = append(array1, 6) //append elements in slice1 array1 = append(array1, 8) array1 = append(array1, 0) array1 = append(array1, 0) array2 = append(array2, 9) // append elements in slice2 array2 = append(array2, 10) copy(array1[2:], array2) // copy the elements from source slice to destination slice fmt.Println("The original array1 is:",array1) fmt.Println("The original array2 is",array2) fmt.Println("The array after replacing elements in a slice is") fmt.Println(array1) // print the destination slice }
Output
The original array1 is: [6 8 0 0] The original array2 is: [9 10] The array after replacing elements in a slice is: [6 8 9 10]
Method 2: Using indexing of elements
In this method, we will use indexing to replace elements in slice. All the outputs will be printed on the console using fmt.Println() function. Let’s inculcate how to solve this problem.
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 main function and in that function create a slice using append function named array.
Step 3 − Fetch the index value which is to be replaced and assign it new value using array[]index=new value.
Step 4 − Print the new value on console using fmt.Println() function where ln signifies new line.
Example
Golang program to replace elements in slice using indexing of elements
package main import "fmt" // create main function to execute the program func main() { var array []string // create a slice of type string array = append(array, "Delhi") // append elements in the slice array = append(array, "Jaipur") array = append(array, "Coimbatore") fmt.Println("The array before replacing value is:", array) array[1] = "Udaipur" // replace elements in slice using indexing fmt.Println("The array after replacing value is:") fmt.Println(array) // print the slice replaced with new element }
Output
The array before replacing value is: [Delhi Jaipur Coimbatore] The array after replacing value is: [Delhi Udaipur Coimbatore]
Conclusion
We executed the program of replacing elements of a slice using two examples. In the first example we used built-in copy function and in the second example we used indexing to replace elements. Both the examples give similar output. Hence the program executed successfully.
- Related Articles
- Golang program to add elements to a slice
- Golang program to rotate elements of a slice
- How to replace all the elements in slice of bytes in Golang?
- Golang program to count the elements of a slice
- Delete Elements in a Slice in Golang
- How to replace a specified element in slice of bytes in Golang?
- Golang program to initialize a slice
- Golang program to reverse a slice
- Golang program to print a slice
- Golang program to iterate over a Slice
- Golang program to remove a subset from a slice
- Golang program to find the largest element in a slice
- Golang program to split a slice into two halves
- Golang program to remove an element from a slice
- Golang program to convert array into slice
