#golang #go #arrays
Arrays
-------
1.Array Declaration - var a [3]int
2.Array Declare and assign (initialize) - b:= [5]int{2,4,6,8,10}
3.Array Declaration without Size - c:=[...]int{10,20,30,40,506,70,80,90,100}
4.Array Initialize with specific elements - d:=[4]int{1:30}
5.Array of Pointers - a:=[5]*int{0:new(int),1:new(int)}
Pointers are variables used to point the address of another variable. Using pointers we can access both value and address of a variable
Wehenver we want to access a value using a pointer, we need to use "*" with the pointer variable
6.Assigning/Copying Arrays
7.Assigning/Copying Array of Pointers
8.Multi-Dimensional Arrays
9.Passing Arrays to Functions
arraydemo.go
------------
package main
import "fmt"
func main(){
var a [3]int //Declaring variable a as an array of size 3 belongs to data type integer
//Integer variables defaulted to Zero, String will be defaulted to "" and Objects will be defaulted nil
a[0]=10;
a[1]=20;
a[2]=30;
b:= [5]int{2,4,6,8,10} //Initialization of Arrays
c:=[...]int{10,20,30,40,506,70,80,90,100} //Used to identify the array dimension based on the initialization values
d:=[4]int{1:30} //Initialize array variable d of integer, size [3] with value 30 on its a[1] index
fmt.Printf("The size of array c is %d\n",len(c))
a=d //Copying or assigning arrays is possible only if the data type and dimension of both the arrays are alike
//Use for loop to print array values as shown in the video
}
arraypointersdemo.go
--------------------
package main
import "fmt"
func main(){
var b [5]*int
a:=[5]*int{0:new(int),1:new(int)} //Initializing only th first two locations of the pointer array to two integer pointers,rest of the indices left nil
*a[0]=100 //pointer a[0] will store value 100
*a[1]=200 //Pointer a[1] will store value 200
//Use For loop
fmt.Println(*a[i]) //a[0] is pointing to value 100 whereas a[1] is poiting to value 200
b=a //Copy Array of Pointers not the values. Both the pointers will start pointing to the same set of values
//Use For loop
fmt.Println(*b[i]) //b[0] is pointing to value 100 whereas b[1] is pointing to value 200
}
multiarraydemo.go
-------------------
package main
import "fmt"
func main(){
var a [2][2]int //Two Dimensional array of two rows and two columns
b:=[2][2]int{{10,20},{30,40}}
a=b //Copying Two Dimesional Array
//Use nested For loops to iterate
fmt.Printf("A=%d, B=%d",a[i][j],b[i][j])
}
funcarraydemo.go
----------------
package main
import "fmt"
func main(){
a:=[2]string{"One","two"}
displayarray(a) //Passing Array to function
}
func displayarray(x [2]string){
//Use for loop as in the video
fmt.Println(x[i])
}
Watch video Go (golang) Tutorials - Arrays and Array of Pointers online, duration hours minute second in high quality that is uploaded to the channel Ambasoft Java 24 March 2018. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 498 times and liked it 7 visitors.