Golang program to print an array?


Data plays an essential role when we write programs. It can be stored in multiple ways. For storing a collection of data with the same data type, an array can be used. In this tutorial, we will be discussing the approach to printing an array in Golang programming.

But before writing the code for this, let’s briefly discuss the array.

What is an Array?

An array has a fixed length and its length cannot be altered. It is homogeneous in nature, i.e., all the elements stored in an array should be of the same data type.

If an array has ‘n’ elements, then the first element in an array is stored at the 0th index and the last element is located at the n−1 index.

Example

  • n=6

    This means the array has six elements then −

    • first value of the array will be at the 0th index, and

    • last value will be at the n−1 index, i.e. 5th index.

Method 1: Directly Printing the Array Elements

Algorithm

  • Step 1 − Declare an array ‘arr’ with few elements stored in it.

  • Step 2 − Print the elements stored in it directly.

Example

// Go program to print an array directly
package main

// fmt package allows us to print formatted strings
import "fmt"

func main() {
	// Declaration of array elements using the shorthand method
	arr := [7]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
fmt.Println("Elements of the array:") // Directly printing the array fmt.Println(arr) }

Output

Elements of the array:
[Monday Tuesday Wednesday Thursday Friday Saturday Sunday]

Description of code

  • arr := [7]string{"Monday", "Tuesday", “Wednesday”, “Thursday”, "Friday", “Saturday”, “Sunday”} − In this line, we are declaring the array ‘arr’ of data type string, with size 7, and storing 7 elements, namely the days in a week.

Method 2: Using for loop for Printing an Array

Algorithm

  • Step 1 − Declare an array ‘arr’ with few elements stored in it.

  • Step 2 − Create a for loop starting from 0 as the array indexing starts from 0 and the first element is present at the 0th location. We are running it until the value of index ‘i’ is less than the length of the array ‘arr’. The length of the array can be determined using the ‘len’ function.

  • Step 3 − Print the array elements.

Example

// Go program to print an array using for loop
package main

// fmt package allows us to print formatted strings
import "fmt"

func main() {
	// Declaration of array elements using the shorthand method
	arr := [7]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}

	// Accessing the values of the array using for loop
	fmt.Println("Elements of Array:")

	for i := 0; i < len(arr); i++ {
           // printing the days of the week
		fmt.Printf("Day %d is %s.\n", i+1, arr[i])
	}
}

Output

Elements of Array:
Day 1 is Monday.
Day 2 is Tuesday.
Day 3 is Wednesday.
Day 4 is Thursday.
Day 5 is Friday.
Day 6 is Saturday.
Day 7 is Sunday.

Description of Code

  • fmt.Printf("Day %d is %s.\n", i+1, arr[i]) − We are printing arr[i] as the value of ‘i’ keeps on increasing with the for loop. It will start from arr[0] which is Monday, then it will be arr[1] which is Tuesday and will keep on increasing and printing until the value reaches arr[6] in the end which is Sunday.

Conclusion

This is all about printing the elements of an array using two ways. The latter method of printing it with the help of for loop is much better in comparison to printing it directly as each element can be easily accessed in that method. You can explore more about Golang programming using these tutorials.

Updated on: 28-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements