Golang Program To Append An Element Into An Array


In this tutorial, we will write a go language program to iterate over an array. An array is a data structure, which is used to store data at contiguous memory locations. There are many methods to append an element in an array. We shall discuss them in this program

Example 1: Add Values to an Array of Strings in the Main() Function

In this example, we will write a go language program to add values to an array of strings in the main() function. We will first initialize an array of strings of specific size and then add values to its every index

Algorithm

Step 1 − First, we need to import fmt package.

Step 2 − Now, start the main() function. Inside the main() initialize an array of strings.

Step 3 − In order to add values to this array we have to mention the index to which we wish to store a particular value.

Step 4 − So, mention the index after the name of the array and assign the value to that index using the equality operator

Step 5 − In the same way store values to every index of the array. then we need to print the array.

Step 6 − To print the array use fmt.Println() function and mention each index that should be printed.

Example

package main
import "fmt"
func main() {

   // initializing an array
   var array [3]string
   
   // adding values to it
   array[0] = "India"
   array[1] = "Canada"
   array[2] = "Japan"
   
   // printing the array
   fmt.Println("The first element of array is:", array[0])
   fmt.Println("The second element of array is:", array[1])
   fmt.Println("The third element of array is:", array[2])
}

Output

The first element of array is: India
The second element of array is: Canada
The third element of array is: Japan

Example 2: Add Values to an Array of Strings using an Internal Function

In this example, we will write a go language program to add values to an array of strings using the append() function. The append is a library function in the go language that is used to add values to an array or a map.

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 a 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 an array containing all the values.

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 and returns the slice that we can store in the variable.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Then, start the main() function. Inside the main() create an array of strings using the make() function.

Step 3 − Now, insert values to the array created above using the append() function.

Step 4 − The values are stored at respective indexes of the array starting from 0.

Step 5 − Now, we need to print the array on the screen for that use fmt.Println() function and print the value at each index.

Example

package main
import "fmt"
func main() {

   // initializing an array
   array := make([]string, 0, 3)
   array = append(array, "Apple", "Mango", "Banana")
   
   // printing the array
   fmt.Println("The first element of array is:", array[0])
   fmt.Println("The second element of array is:", array[1])
   fmt.Println("The third element of array is:", array[2])
}

Output

The first element of array is: Apple
The second element of array is: Mango
The third element of array is: Banana

Example 3: To Add Values to an Array of Strings through a Different Array using Internal Functions

In this program, we will write a go language program to append values in an array of strings through a different array in the main() function. We will first initialize two arrays and then will store all the values of the second array in the first one.

Algorithm

Step 1 − First, we need to import the fmt package.

Step 2 − Then, start the main() function. Inside the main() create an array of strings using the make() function.

Step 3 − Now, insert values to the array created above using the append() function and print the array on the screen.

Step 4 − Similarly, create another array of string types and add values to it. Also, print the array on the screen.

Step 5 − Now, we need to add the values of the second array to the first array. for that use append() function.

Step 6 − The first argument to the function will be the array through which we wish to store values and the second argument will be the array through which we wish to store values

Step 7 − Now, we need to print the array on the screen for that use fmt.Println() function and print the value at each index.

Example

package main
import "fmt"
func main() {
   
   // initializing an array
   array := make([]string, 0, 3)
   array = append(array, "Apple", "Mango", "Banana")
   fmt.Println("The first array is:", array)
   fmt.Println()
   
   // creating second array
   array1 := make([]string, 0, 2)
   array1 = append(array1, "pine-apple", "cherry")
   fmt.Println("The second array is:", array1)
   array = append(array, array1...)
   fmt.Println()

   // printing the array
   fmt.Println("The new array thus formed by combining the two arrays is:")
   for i := 0; i < 5; i++ {
      fmt.Println(array[i])
   }
}

Output

The first array is: [Apple Mango Banana]
The second array is: [pine-apple cherry]
The new array thus formed by combining the two arrays is:
Apple
Mango
Banana
pine-apple
cherry

Conclusion

We have successfully compiled and executed a go language program to append values to an array along with examples. In the first program, we used the equality operator to add values at respective indexes, and in the second and third programs, we used a library function called append() to achieve the result.

Updated on: 10-Jan-2023

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements