Trending October 2023 # How To Initialize Array In Go Language With Examples? # Suggested November 2023 # Top 15 Popular | Restaurant12h.com

Trending October 2023 # How To Initialize Array In Go Language With Examples? # Suggested November 2023 # Top 15 Popular

You are reading the article How To Initialize Array In Go Language With Examples? updated in October 2023 on the website Restaurant12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How To Initialize Array In Go Language With Examples?

Introduction to Golang Array

Array in the Go language is a place to store similar types of data; for example, if we want to store a list of students, then we can define an array of strings and store all the students in that array. In Go language, we can perform all important operations like comparing two arrays, running a loop on the array and getting length, etc. like operations, we can create either single dimension array or a multidimensional array (multidimensional arrays are the array that holds another array on the index), every array index starts with 00 and goes up to the length-1 to the array, here length is the number of elements inside the array.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

var name_of_array[n]Data Type

OR

var name_of_array[n]Data Type {element1, element2, element3, ...elementN} How to define Array in Go Language?

Defining array in go language is very easy. We need to use the var keyword and write the name of the array; we can write any name for the array which is more relevant to the type of data we are going to store inside the array. Next, we need to define the array’s length; we can leave this blank also, which means we are not fixing the array’s length.

var name_of_array [5] int How to Initialize Array in Go Language?

Below, we have given a small format of how we can initialize the array in go language. We have written the name of the array and defined the length as the 5, and we set the data type for the array as the int and array attributes we have defined as the 23,12 and 44. These attributes can be accessed with index 0,1,2.

var name_of_array [5] int {23 ,12 ,44} Types of Array in Golang

On the basis of the structure, we can say that there are two types of array in the go language: a single dimension array and a multidimensional array; let us understand each with examples.

1. Single Dimension Array

Single-dimension arrays are an array that contains the attribute on each index, and the data type of these attributes are the same as the data type which we have defined for the array.

Code:

package main import "fmt" func main() { arrDetail:= [3]string {"Ranjan", "Ajay", "Vijay"} fmt.Println("Here we are printing the element of the array are") for j:= 0; j < 3; j++{ fmt.Println(arrDetail[j]) } }

Output:

2. Multidimensional Array

Multidimensional arrays are an array that holds another array on the index, as compared to the single dimension array, which holds the attribute; here, in the case of the multi-dimensional array, it holds another array. And data type of each array element will be of the same type.

Code:

package main import "fmt" func main() { arrDetail:= [3][3]string{{"Ranjan", "Ajay", "Vijay"}, {"Suman", "Akash", "Vikash"}, {"Sunita", "Nikita", "Ankita"},} fmt.Println("Here we are printing the element of the array are") for i:= 0; i < 3; i++{ for j:= 0; j < 3; j++{ fmt.Println(arrDetail[i][j]) } } }

Output:

Examples to Implement Golang Array Example #1

Below is a simple example where we are taking a string array and assigning various values for the index of the array and printing them.

Code:

package main import "fmt" func main() { var arrayData[4]string arrayData[0] = "Ranjan" arrayData[1] = "Kumar" arrayData[2] = "Pandey" arrayData[3] = "Chennai" fmt.Println("Printing the array elements:") fmt.Println("Printing the array element 1: ", arrayData[0]) fmt.Println("Printing the array element 2: ", arrayData[1]) fmt.Println("Printing the array element 3: ", arrayData[2]) fmt.Println("Printing the array elements 4: ", arrayData[3]) }

Output:

Example #2

In the below example, we are running a loop on the array, the array contains a message, and we are running a loop on the message array and printing each element of the array.

Code:

package main import "fmt" func main() { arrDetail:= [6]string{"We", "should", "strong", "for" ,"every","situation"} fmt.Println("Printing every element of the array:") for e:= 0; e < 6; e++{ fmt.Println(arrDetail[e]) } }

Output:

Example #3

Code:

package main import "fmt" func main() { numericArray1:= [5]int{12,8,3,24,23} numericArray2:= [...]int{19,23,1,41,5,31,12,14} numericArray3:= [3]int{9,3,5} fmt.Println("The Length numeric array 1 is:", len(numericArray1)) fmt.Println("The Length numeric array 1 is:", len(numericArray2)) fmt.Println("The Length numeric array 1 is:", len(numericArray3)) }

Output:

Example #4

In the below example, we are performing the comparison operation on the arrays, which returns true and false.

Code:

package main import "fmt" func main() { numericArray1:= [3]int{11,7,12} numericArray2:= [...]int{11,7,12} numericArray3:= [3]int{13,51,31} fmt.Println(numericArray1==numericArray2) fmt.Println(numericArray2==numericArray3) fmt.Println(numericArray1==numericArray3) numericArray4:= [3]int{11,7,12} fmt.Println(numericArray1==numericArray4) }

Output:

Conclusion

From this tutorial, we learned about the basic concept of the array in the Go language, and we learned the working and its types with examples. We learned its important features with the help of some good examples. We also learned how to define and initialize the array in the go language.

Recommended Articles

This is a guide to Golang Array. Here we discuss an introduction to Golang Array, syntax, working, and types with programming examples. You can also go through our other related articles to learn more –

You're reading How To Initialize Array In Go Language With Examples?

Update the detailed information about How To Initialize Array In Go Language With Examples? on the Restaurant12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!