Golang program to fill an array with a specific element


In this tutorial, we will learn how to fill an array with a specific element. We will explore few examples to know more about this program. The output will be printed on console using fmt.Println() function. Let’s have a look and understand the program.

We have used the following make() function in the examples.

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.

Method 1: Using for Loop and Make Function in Main Function

In this method, we will see how a specific element is being assigned to the array using a for loop. Let’s dive into the algorithm and the code to get a crystal-clear view of the method.

Algorithm

Step 1 − Declare a package main and import fmt package in the program.

Step 2 − Create a function main and further create an array using make function and assign the size of array equals to 6.

Step 3 − Run a loop till the length of array and assign a specific value to each index of array.

Step 4 − After the values are assigned, print the array on console using fmt.Println() function.

Example

Golang program to fill an array with specific element using for loop and make function in main function

package main
import (
   "fmt"
)
func main() {
   arr := make([]int, 6) 
   //creating array using make function
   for index := range arr {
      arr[index] = 60 
      //assign the specific element to the array indexes
   }
   fmt.Println("The specific element to be added in array is:", 60)
   fmt.Println("The array with the specific element is:", arr)
}

Output

The specific element to be added in array is: 60
The array with the specific element is: [60 60 60 60 60 60]

Method 2: Using Make Function and for loop in External Function

In this method, we will see how to fill an array with a specific element using an external function. The function takes the size of array and the specific element as its parameters. The output is printed on the screen using print statement in Golang.

Algorithm

Step 1 − Declare a package main and import fmt package in the program.

Step 2 − Create a function print_specific with size of array and the specific elements as its parameters.

Step 3 − Create an array inside the function using make function with size of array and empty array as its parameter.

Step 4 − Run a loop till the length of array and assign a specific value to each index of array.

Step 5 − After the values are assigned, return the array back to the function print_specific.

Step 6 − The function is further called by main function and printed there with the help of fmt.Println() function.

Example

Golang program to fil an array with specific element using make function and for loop on external function

package main
import (
   "fmt"
)
func print_specific(size, specific int) []int { 

   //creating function
   arr := make([]int, size)
   for i := range arr {
      arr[i] = specific 
      //assign specific element to the array indexes
   }
   return arr
}
func main() {
   arr := print_specific(6,20) 
   // call the function with size of array and the specific element
   fmt.Println("The specific element to be added in array is:", 20)
   fmt.Println("The array with a specific element is:")
   fmt.Println(arr)
}

Output

The specific element to be added in array is: 20
The array with a specific element is:
[20 20 20 20 20 20]

Conclusion

We executed the program of filling an array with specific element using two methods. In the first method we used the main approach to fill the element in array and in the second method we used the external function approach in which the array’s size and the specific element are passed as an argument through main function. Similar outputs are obtained in both cases. Hence program executed successfully.

Updated on: 13-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements