- 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 insert an element into the array at the specified index
In this tutorial, we will write a go language program to insert an element into an array at the specified index. There are many methods to add elements to an array. it can be done by either using indexing or by using for loops. There are some inbuilt functions too using which we can add elements at the specified indexes.
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
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 − First, we need to import the fmt package.
STEP 2 − Now, we need to start the main() function.
STEP 3 − Here, initialize an array of integers using make() function and append values to the array using append() function. further print the array on the screen by using fmt.Println() function.
STEP 4 − Store the index of element in a variable that should be changed. Now by using name_of_array[index] notation change the element present at that location to the new value.
STEP 5 − Further, print the new array thus formed on the screen using fmt.Println() function.
Example
In this example we will write a go language program to add an element into an array at specified index using the concept of indexing in arrays.
package main import "fmt" func main() { // initializing array array := make([]int, 0, 8) array = append(array, 11, 20, 13, 44, 56, 96, 54, 97) fmt.Println("The given array is:", array) // getting the index var index int = 4 array[index] = 65 fmt.Println() fmt.Println("The new array obtained after changing the element at", index, "index is:", array) }
Output
The given array is: [11 20 13 44 56 96 54 97] The new array obtained after changing the element at 4 index is: [11 20 13 44 65 96 54 97]
Conclusion
We have successfully compiled and executed a go language program to add elements into an array at specified index along with examples. we have created two programs here in the first program we have used the concept of indexing to add element at a particular index while in the second examples we have used a different array to append values to an array.
- Related Articles
- Golang Program to insert multiple elements into the array from the specified index
- Insert an element into the ArrayList at the specified index in C#
- Insert an element into Collection at specified index in C#
- Swift Program to insert multiple elements into the array from the specified index
- Golang Program To Append An Element Into An Array
- Golang Program to insert an item into the array without using library function
- How to insert the elements of a collection into the List at the specified index in C#?
- Golang Program to add an element in the array at the beginning
- Insert the specified element at the specified position in Java CopyOnWriteArrayList
- Insert at the specified index in StringCollection in C#
- Insert into OrderedDictionary with key and value at specified index in C#
- Golang program to insert a node at the ith index node, when the index is at the mid-index position in the linked list.
- Replace an element at a specified index of the Vector in Java
- Golang Program to insert a node at the ith index node, when the index is at the 0th position in the linked list.
- Golang program to insert a node at the ith index node, when the index is at the last position in the linked list.
