Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9afbe2b

Browse files
author
Rajeev Kumar Singh
committed
Array Examples
1 parent 7108014 commit 9afbe2b

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

‎tutorial6-arrays/array.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var x [5]int // An array of 5 integers
7+
fmt.Println(x)
8+
9+
var y [8]string // An array of 8 strings
10+
fmt.Println(y)
11+
12+
var z [3]complex128 // An array of 3 complex numbers
13+
fmt.Println(z)
14+
15+
// By default, all the array elements are assigned the zero value of the array type.
16+
// For example, if we declare an integer array, all the elements will be initialized with zero.
17+
// If we declare a string array, all the elements will be initialized with an empty string, and so on.
18+
}

‎tutorial6-arrays/array_indexing.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var x [5]int // An array of 5 integers
7+
8+
x[0] = 100
9+
x[1] = 101
10+
x[3] = 103
11+
x[4] = 105
12+
13+
fmt.Printf("x[0] = %d, x[1] = %d, x[2] = %d\n", x[0], x[1], x[2])
14+
fmt.Println("x = ", x)
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Declaring and initializing an array at the same time
7+
var a = [5]int{2, 4, 6, 8, 10}
8+
fmt.Println(a)
9+
10+
// Short hand declaration for declaring and initializing an array
11+
b := [5]int{2, 4, 6, 8, 10}
12+
fmt.Println(b)
13+
14+
// You don't need to initialize all the elements of the array.
15+
// The un-initialized elements will be assigned the zero value of the corresponding array type
16+
c := [5]int{2}
17+
fmt.Println(c)
18+
19+
// Letting Go compiler infer the length of the array
20+
d := [...]int{3, 5, 7, 9, 11, 13, 17}
21+
fmt.Println(d)
22+
}

‎tutorial6-arrays/array_iteration.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Iterating over and Array and printing its elements
7+
names := [3]string{"Mark Zuckerberg", "Bill Gates", "Larrt Page"}
8+
9+
for i := 0; i < len(names); i++ {
10+
fmt.Println(names[i])
11+
}
12+
13+
// Finding the Sum of an Array
14+
a := [4]float64{3.5, 7.2, 4.8, 9.5}
15+
sum := float64(0)
16+
17+
for i := 0; i < len(a); i++ {
18+
sum = sum + a[i]
19+
}
20+
21+
fmt.Printf("Sum of all the elements in array %v = %f\n", a, sum)
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Iterating over an array using range form of for loop
7+
daysOfWeek := [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
8+
9+
for index, value := range daysOfWeek {
10+
fmt.Printf("Day %d of week = %s\n", index, value)
11+
}
12+
13+
// Finding the sum of an array
14+
a := [4]float64{3.5, 7.2, 4.8, 9.5}
15+
sum := float64(0)
16+
17+
for _, value := range a {
18+
sum = sum + value
19+
}
20+
21+
fmt.Printf("Sum of all the elements in array %v = %f\n", a, sum)
22+
}

‎tutorial6-arrays/array_value_types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
a1 := [5]string{"English", "Japanese", "Spanish", "French", "Hindi"}
7+
a2 := a1 // A copy of the array `a1` is assigned to `a2`
8+
9+
a2[1] = "German"
10+
11+
fmt.Println("a1 = ", a1) // The array `a1` remains unchanged
12+
fmt.Println("a2 = ", a2)
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
a := [2][2]int{
7+
{3, 5},
8+
{7, 9}, // This comma is necessary
9+
}
10+
fmt.Println(a)
11+
12+
// Just like 1D arrays, you don't need to initialize all the elements in a multi-dimensional array.
13+
// Un-initialized array elements will be assigned the zero value of the array type
14+
b := [3][4]float64{
15+
{1, 3},
16+
{4.5, -3, 7.4, 2},
17+
{6, 2, 11},
18+
}
19+
fmt.Println(b)
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /