8000 Added modules to learn struct and data_structures, mainly arrays and … · anarchymonkey/learn_golang@a4a501f · GitHub
[go: up one dir, main page]

Skip to content

Commit a4a501f

Browse files
committed
Added modules to learn struct and data_structures, mainly arrays and slices
1 parent d5e79ef commit a4a501f

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed
Lines changed: 140 additions & 0 deletions
70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func learnArrays() {
9+
// arrays can be defined as the syntax [n]T
10+
// n denotes the size of the array whereas T defines the type
11+
12+
var numbers [5]int
13+
14+
for i := 0; i < 5; i++ {
15+
numbers[i] = i * 2
16+
}
17+
18+
var primeNumbers [5]int = [5]int{1, 3, 5, 7, 11}
19+
20+
fmt.Println(numbers, primeNumbers)
21+
22+
}
23+
24+
func learnDynamicArrays() {
25+
// a basic array turned into a slice
26+
27+
randomMonthsInFeb := []int{
28+
1,
29+
2,
30+
3,
31+
4,
32+
19,
33+
20,
34+
28,
35+
}
36+
37+
var sliceOfMonthsInFeb []int = randomMonthsInFeb[0:5]
38+
39+
fmt.Println("The slice is", sliceOfMonthsInFeb)
40+
41+
// appending values in slices
42+
43+
for i := 0; i < 5; i++ {
44+
// remember append returns the value and thus needs to be stored somewhere
45+
sliceOfMonthsInFeb = append(sliceOfMonthsInFeb, i*4)
46+
}
47+
48+
fmt.Println("appended slice", sliceOfMonthsInFeb)
49+
50+
// A slice does not store any data, rather it just references an array location thus if we modify that location the copies will also be modified
51+
52+
// Let us see with an example
53+
54+
cities := []string{
55+
"Bangalore",
56+
"Hydrabad",
57+
"Kolkata",
58+
"Mumbai",
59+
}
60+
61+
fmt.Println("cities => start", cities)
62+
63+
a := cities[0:2]
64+
b := cities[1:3]
65+
66+
fmt.Println("a & b", a, b)
67+
68+
b[0] = "+++++++"
69+
+
// you will see that if we modify the reference of b[0], the same modification will be done in a[1] as they share the same reference
71+
72+
fmt.Println("a & b after b is moditfied", a, b)
73+
74+
fmt.Println("cities => end", cities)
75+
76+
// slice defaults
77+
78+
// lower bound is defaulted to 0 & the higher bound is defaulted to the length slice[lowerbound:upperbound]
79+
80+
// slice length and capacity
81+
82+
// empty slice is init with nil
83+
84+
dummySlice := []int{
85+
1, 2, 3, 4, 5,
86+
}
87+
88+
printSlice(dummySlice)
89+
90+
dummySlice = dummySlice[0:3]
91+
92+ 6D47
printSlice(dummySlice)
93+
94+
dummySlice = dummySlice[:2]
95+
96+
printSlice(dummySlice)
97+
98+
}
99+
100+
func createSlicesOfSlices() {
101+
// slices of slices in DSA terms is a 2D dynamic array
102+
103+
// lets make a chess board
104+
105+
chessBoard := [][]string{
106+
[]string{"-", "-", "-"},
107+
[]string{"-", "-", "-"},
108+
[]string{"-", "-", "-"},
109+
}
110+
111+
chessBoard[0][0] = "X"
112+
chessBoard[1][1] = "O"
113+
chessBoard[0][1] = "X"
114+
chessBoard[2][2] = "Aniket"
115+
chessBoard[2][1] = "O"
116+
chessBoard[0][2] = "O"
117+
chessBoard[2][0] = "X"
118+
chessBoard[1][0] = "O"
119+
chessBoard[1][2] = "X"
120+
121+
for i := 0; i < len(chessBoard); i++ {
122+
fmt.Printf("%s\n", strings.Join(chessBoard[i], " "))
123+
}
124+
}
125+
126+
func printSlice[T []int](val T) {
127+
fmt.Printf("\n len = %v, cap = %v \n", len(val), cap(val))
128+
}
129+
130+
func main() {
131+
132+
// learn arrays
133+
learnArrays()
134+
135+
// learn dynamic arrays | slices
136+
137+
learnDynamicArrays()
138+
139+
createSlicesOfSlices()
140+
}

go_basics/data_structures/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module anarchymonkey.com/go-basics/data-structures
2+
3+
go 1.20

go_basics/go.work

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ go 1.20
22

33
use (
44
./conditional_statements
5+
./data_structures
56
./datatypes
67
./defer_panic_recover
78
./functions
89
./loops
910
./numeric_constants
1011
./pointers
12+
./structs
1113
./typeConversionsAndInferrence
1214
./variables
1315
)

go_basics/structs/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module anarchymonkey.com/go-basics/structs
2+
3+
go 1.20

go_basics/structs/struct.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// A struct is a collection of fields
6+
7+
type Vertex struct {
8+
X int
9+
Y int
10+
}
11+
12+
func structExample() {
13+
14+
// basic struct example
15+
var vertex1 Vertex = Vertex{
16+
X: 1,
17+
Y: 2,
18+
}
19+
20+
fmt.Printf("X = %d, Y = %d \n", vertex1.X, vertex1.Y)
21+
22+
// pointer to a struct example
23+
24+
var vertex2 Vertex = Vertex{
25+
X: 10,
26+
Y: 20,
27+
}
28+
29+
p := &vertex2
30+
31+
// you can ommit the * operator while assigning values to the pointer
32+
p.X = 12
33+
34+
fmt.Println("X in pointer is ", p.X)
35+
36+
// struct literals
37+
38+
v1 := Vertex{1, 2}
39+
v2 := Vertex{X: 1}
40+
v3 := Vertex{}
41+
ptr := &Vertex{3, 4}
42+
43+
fmt.Println(v1, v2, v3, ptr.X, ptr.Y)
44+
45+
}
46+
47+
func main() {
48+
49+
structExample()
50+
}

0 commit comments

Comments
 (0)
0