Categories
Go

array in go lang

In this post, we will learn about array in go lang

In my previous tutorial, I have explained the installation of Go in windows. Please check out the below URL for that.
https://beginnersbug.com/install-go-lang-in-windows/

Arrays are basic in all programming languages which is used for data operations. We can have a collection of elements here.

Creating array in Go lang

Similar to other programming languages, we can create arrays with length and without length.

In the below example I created two arrays with indexes of 5 & 3 with the datatype of int and string

package main

// fmt package for user input & output
import "fmt"

// Main Method
func main() {
	// Creating an int array with index of 5
	var intarr = [5]int{10, 4, 6, 2, 12}
	// Creating an string array with index of 3
	var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
	// Printing arrays
	fmt.Println(intarr)
	fmt.Println(strarr)
}

Output

[10 4 6 2 12]
[Iron Man Hulk Thor]

Printing index of array

In the below example, we will print the element based on the index value.
Here we are printing element at index intarr(1) & strarr(2)

package main

// fmt package for user input & output
import "fmt"

// Main Method
func main() {
	// Creating an int array with index of 5
	var intarr = [5]int{10, 4, 6, 2, 12}
	// Creating an string array with index of 3
	var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
	// Printing arrays
	fmt.Println(intarr)
	fmt.Println(strarr)

	// Printing index of an array
	fmt.Println(intarr[1])
	fmt.Println(strarr[2])
}

Output

[10 4 6 2 12]
[Iron Man Hulk Thor]
4
Thor

The below snippet will show you to print the length of an array.
len() method is used to print the length of the array

package main

// fmt package for user input & output
import "fmt"

// Main Method
func main() {
	// Creating an int array with index of 5
	var intarr = [5]int{10, 4, 6, 2, 12}
	// Creating an string array with index of 3
	var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
	// Printing arrays
	fmt.Println(intarr)
	fmt.Println(strarr)

	// Printing index of an array
	fmt.Println(intarr[1])
	fmt.Println(strarr[2])

	fmt.Println("Lenght of int array:", len(intarr))
	fmt.Println("Lenght of string array:", len(strarr))
}

Output

[10 4 6 2 12]
[Iron Man Hulk Thor]
4
Thor
Lenght of int array: 5
Lenght of string array: 3

Iterate array using for loop

Here we are printing each and every element with the help of for loop

package main

// fmt package for user input & output
import "fmt"

// Main Method
func main() {
	// Creating an int array with index of 5
	var intarr = [5]int{10, 4, 6, 2, 12}
	// Creating an string array with index of 3
	var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
	// Printing arrays
	fmt.Println(intarr)
	fmt.Println(strarr)

	// Printing index of an array
	fmt.Println(intarr[1])
	fmt.Println(strarr[2])

	fmt.Println("Lenght of int array:", len(intarr))
	fmt.Println("Lenght of string array:", len(strarr))

	// Iterating array using for loop
	for i := 1; i < len(intarr); i++ {
		fmt.Print(intarr[i], " ")
	}
	fmt.Println()
	// Iterating array using for loop
	for i := 0; i < len(strarr); i++ {
		fmt.Print(strarr[i], " ")
	}

}

Output

[10 4 6 2 12]
[Iron Man Hulk Thor]
4
Thor
Lenght of int array: 5
Lenght of string array: 3
4 6 2 12 
Iron Man Hulk Thor

Go lang play

You can play with the array using their playgrounds also https://go.dev/tour/moretypes/6

Categories
Go

install go lang in windows

In this article, We will learn to install go lang in windows

GO lang is created by Google in 2007. It is the most prominent language now. It is easy to build fast and scale massive application

It is an open-source programming language supported by Google. Most of the cloud providers like google cloud, AWS, azure are supporting go lang in their ecosystem

Download MSI package

  1. Navigate to the this URL https://go.dev/doc/install 
  2. Click on the Download Go for Windows as like with below image 
  3. Now your download will get start automatically and ready to install
  4. Click on the downloaded .msi file
  5. Click on the next button on the installer prompt 
  6. Click the next for the consequtive steps.
  7. Once you installed you will get below screen in the installer prompt 

Verify the installation

Once you finished the installation you can verify the go lang via command prompt
Open the command prompt and run this command

go version