Skip to content

Commit 969a38d

Browse files
committed
Tutorial4 - Arrays, Slices, Maps and Loops
1 parent 51b7386 commit 969a38d

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ go run tutorial1/main.go
4545
- [x] [Basics](tutorial1)
4646
- [x] [Constants Variables and Basic Data Types](tutorial2)
4747
- [x] [Functions and Control Structures](tutorial3)
48+
- [x] [Arrays, Slices, Maps and Loops](tutorial4)

tutorial4/README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Arrays, Slices, and Control Structures in Go
2+
3+
This will guide you through some basic concepts in Go, including arrays, slices, and control structures. The code we'll be discussing demonstrates these concepts in a simple and understandable way.
4+
5+
## Arrays
6+
7+
In Go, an array is a numbered sequence of elements of a specific length. Here are some ways you can declare an array:
8+
9+
1. Declare an array without initializing elements (all elements get the zero value of the element type):
10+
11+
```go
12+
var intArr [3]int32
13+
fmt.Println(intArr[0]) // prints: 0
14+
```
15+
16+
2. Declare an array and initialize some or all elements:
17+
18+
```go
19+
var intArr [3]int32
20+
intArr[1] = 123
21+
fmt.Println(intArr[0]) // prints: 0
22+
fmt.Println(intArr[1]) // prints: 123
23+
```
24+
25+
3. Declare and initialize an array using a literal:
26+
27+
```go
28+
var intArr [3]int32 = [3]int32{1,2,3}
29+
// or
30+
intArr := [3]int32{1,2,3}
31+
// or let Go figure out the length based on the number of elements
32+
intArr := [...]int32{1,2,3}
33+
```
34+
35+
4. Access a range of elements (a slice of the array):
36+
37+
```go
38+
fmt.Println(intArr[1:3]) // prints: [2 3]
39+
```
40+
41+
## Slices
42+
43+
A slice is a segment of an array. Slices are indexable and have a length. Unlike arrays, they can be resized. Here's how you can declare a slice:
44+
45+
```go
46+
var intSlice []int32 = []int32{4,5,6}
47+
```
48+
49+
You can append elements to a slice using the `append` function:
50+
51+
```go
52+
intSlice = append(intSlice, 7)
53+
```
54+
55+
You can also append another slice to a slice:
56+
57+
```go
58+
var intSlice2 []int32 = []int32{8,9}
59+
intSlice = append(intSlice, intSlice2...)
60+
```
61+
62+
The `...` is used to pass the elements of `intSlice2` as separate arguments to the `append` function.
63+
64+
## Maps
65+
66+
A map is an unordered collection of key-value pairs. Here's how you can declare a map:
67+
68+
```go
69+
var myMap map[string]uint8 = make(map[string]uint8)
70+
```
71+
72+
You can add elements to the map like this:
73+
74+
```go
75+
var myMap2 = map[string]uint8{"Shikha":24, "Sarah":23, "Kanha":1}
76+
```
77+
78+
## Control Structures
79+
80+
### For Loop
81+
82+
Go has only one looping construct: the `for` loop. The basic `for` loop has three components separated by semicolons: the init statement, the condition expression, and the post statement.
83+
84+
Here's a `for` loop that prints the numbers from 0 to 9:
85+
86+
```go
87+
for i:=0; i<10; i++ {
88+
fmt.Printf("printing number %v \n",i)
89+
}
90+
```
91+
92+
### While Loop
93+
94+
Go doesn't have a `while` loop, but you can create one using the `for` loop:
95+
96+
```go
97+
var i int = 0
98+
for i<10{
99+
fmt.Println(i)
100+
i=i+1
101+
}
102+
```
103+
104+
This will print the numbers from 0 to 9, just like the previous `for` loop.
105+
106+
107+
### Checkout the code
108+
109+
- [main.go](main.go)

tutorial4/main.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// var intArr [3]int32
7+
// intArr[1] = 123
8+
// fmt.Println((intArr[0]))
9+
// fmt.Println(intArr[1:3])
10+
11+
// fmt.Println(&intArr[0])
12+
// fmt.Println(&intArr[1])
13+
// fmt.Println(&intArr[2])
14+
15+
//var intArr [3]int32 = [3]int32{1,2,3}
16+
//intArr := [3]int32{1,2,3}
17+
intArr := [...]int32{1,2,3}
18+
fmt.Println(intArr)
19+
20+
var intSlice []int32 = []int32{4,5,6}
21+
fmt.Printf("The length is %v with capacity %v and the value is %v", len(intSlice), cap(intSlice), intSlice)
22+
intSlice = append(intSlice, 7)
23+
fmt.Printf("\nThe length is %v with capacity %v and the value is %v", len(intSlice), cap(intSlice), intSlice)
24+
25+
var intSlice2 []int32 = []int32{8,9}
26+
intSlice = append(intSlice, intSlice2...)
27+
fmt.Printf("\n for slice 2 %v", intSlice)
28+
29+
var intSlice3 []int32 = make([]int32, 3, 8)
30+
intSlice2 = append(intSlice2, intSlice3...)
31+
fmt.Printf("\n for slice 3 %v \n", intSlice2)
32+
33+
var myMap map[string]uint8 = make(map[string]uint8)
34+
fmt.Println(myMap)
35+
36+
var myMap2 = map[string]uint8{"Shikha":24, "Sarah":23, "Kanha":1}
37+
fmt.Printf("Age of Shikha %v \n",myMap2["Shikha"])
38+
39+
var age, ok = myMap2["Adam"]
40+
if ok{
41+
fmt.Printf("The age is %v", age)
42+
}else{
43+
fmt.Println("Invalid Name")
44+
}
45+
46+
delete(myMap2, "Sarah")
47+
48+
for name, age:= range myMap2{
49+
fmt.Printf("Name: %v, Age:%v \n", name, age)
50+
}
51+
52+
for i , v := range intArr{
53+
fmt.Printf("Index: %v, Value: %v\n", i, v)
54+
}
55+
56+
//GO doesn't have while loop but it can be achieved by for loop itself
57+
58+
var i int = 0
59+
for i<10{
60+
fmt.Println(i)
61+
i=i+1
62+
}
63+
64+
for i:=0; i<10; i++ {
65+
fmt.Printf("printing number %v \n",i)
66+
}
67+
68+
}
69+
70+
/*
71+
72+
>>>>>>>>>>>>>>>>> go run tutorial4/main.go
73+
74+
O/P ::::::
75+
76+
77+
[1 2 3]
78+
The length is 3 with capacity 3 and the value is [4 5 6]
79+
The length is 4 with capacity 6 and the value is [4 5 6 7]
80+
for slice 2 [4 5 6 7 8 9]
81+
for slice 3 [8 9 0 0 0]
82+
map[]
83+
Age of Shikha 24
84+
Invalid Name
85+
Name: Shikha, Age:24
86+
Name: Kanha, Age:1
87+
Index: 0, Value: 1
88+
Index: 1, Value: 2
89+
Index: 2, Value: 3
90+
0
91+
1
92+
2
93+
3
94+
4
95+
5
96+
6
97+
7
98+
8
99+
9
100+
printing number 0
101+
printing number 1
102+
printing number 2
103+
printing number 3
104+
printing number 4
105+
printing number 5
106+
printing number 6
107+
printing number 7
108+
printing number 8
109+
printing number 9
110+
*/

0 commit comments

Comments
 (0)