[Go]Array in Go

Array is fixed in size hence it is not flexible, I need to declare the size of the array, and the values cannot exceed the size which I declared.

package main

import "fmt"


func main() {
	// firstarray is an integer array that has size for only two values.
	var firstarray [2] int
	// values assignment can be on a single line.
	firstarray[0], firstarray[1] = 1, 2
	// the for loop to enumerate from 0 until index 1
	for i:=0; i<2; i++ {
		// first method of printing out the number on each index
		fmt.Println(firstarray[i])
		// this is another method that does the same as above.
		fmt.Printf("%d\n", firstarray[i])
	}
	
	// You can also provide the values and insert into secondarray variable.
	// This method is the same as var secondarray [3]int
	// secondarray[0], secondarray[1], secondarray[2] = 1,2,3
	secondarray := [3]int{1,2,3}
	for i:=0; i<3; i++ {
		fmt.Println(secondarray[i])
	}
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s