-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
255 lines (194 loc) · 5.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"fmt"
_ "goTutorial/compoundInterest"
"goTutorial/conditional"
"goTutorial/loop"
"goTutorial/simpleInterest"
"goTutorial/simpleInterest/finance"
"log"
"math"
"unsafe"
)
// package level variables
var p, r, t = 5000.0, 10.0, 1.0
// error silencers
var _ = finance.ShowFinanceDetails()
func init() {
println("### Main package: initialized ###")
if p < 0{
log.Fatal("Principal is less than zero")
}
if r < 0 {
log.Fatal("Rate of interest is less than zero")
}
if t < 0 {
log.Fatal("Duration is less than zero")
}
}
func main() {
fmt.Println("Hello World")
fmt.Println()
// declaring without initializing
// default zero value will be assigned depending on the type
var num1 int
fmt.Println("Declaration only: ", num1)
// declaring + initializing : with type
var num2 int = 2345
fmt.Println("Assignment: ", num2)
// declaring + initializing : without type
// type will be inferred depending on the value assigned
var num3 = 454
fmt.Println("Assignment without type: ", num3)
// multiple variables (same type) - with type
var height, weight int = 158, 43
fmt.Println("Old height and weight: ", height, weight)
weight = 55
fmt.Println("New weight after dieting for 10 years: ", weight)
// multiple variables (same type) - without type
var id, index = 3244, 23
fmt.Println("id: ", id, " / index: ", index)
// multiple variables (different types)
var (
name = "Shenali"
age = 20
studentId int // if not assigning, should specify the type
)
fmt.Println("Name: ", name, ", age: ", age, ", Student ID: ", studentId)
studentId = 20191152
fmt.Println("Student ID: ", studentId)
//shorthand
number := 34
fmt.Println(number)
stName, stAge := "Shaini", 15
fmt.Println("Student: ", stName, "(", stAge, ")")
minNumber := math.Min(2323.2, 33545.0)
fmt.Println("Minimum: ", minNumber)
fmt.Println()
// bool
isCrazy, isStupid := true, false
isComplicated := isCrazy && isStupid
fmt.Println("Complicated? ", isComplicated)
// int
a := 34
fmt.Printf("a : type is %T and size is %d \n", a, unsafe.Sizeof(a))
//sizeOf returns the size in bytes
// int - depending on the platform 32 or 64
// float
b := 34.2
fmt.Printf("b : type is %T and size is %d \n", b, unsafe.Sizeof(b))
// complex
c1 := complex(3, 2)
c2 := 5 + 2i
cSum := c1 + c2
fmt.Println(cSum)
//type conversion - explicit
var n1 int = 21
var f1 float64 = float64(n1)
fmt.Println(f1)
fmt.Println()
// constants
// untyped
const const1 = "Only constant in life is change"
fmt.Println(const1)
// typed
const const2 string = "Shenali"
fmt.Println(const2)
// group of constants
const (
constNum = 232
constName = "CHANGE"
)
fmt.Println(constName)
// custom types and constants - string
const name1 = "Rachel"
type customString string
var name2 customString = "Monica"
fmt.Println(name2)
name2 = customString(name1)
fmt.Println(name2)
// custom types and constants - bool
const bool1 = false
type customBool bool
var bool2 customBool = true
fmt.Println(bool1, bool2)
//bool1 = bool2 // type casting not allowed
// custom types and constants - numeric
const mysteryNumber = 12
var mysteryNumberInt32 int32 = mysteryNumber
var mysteryNumberInt64 int64 = mysteryNumber
var mysteryNumberFloat64 float64 = mysteryNumber
var mysteryNumberComplex64 complex64 = mysteryNumber
fmt.Printf("mysteryNumber - %T, mysteryNumberInt32 %T, mysteryNumberInt64 %T, mysteryNumberFloat64 %T, mysteryNumberComplex64 %T \n", mysteryNumber, mysteryNumberInt32, mysteryNumberInt64, mysteryNumberFloat64, mysteryNumberComplex64)
fmt.Println()
// functions
// 1.
x, y := 290, 7823
sum := addTwoNumbers(x, y)
fmt.Println("Sum of x and y is ", sum)
// 2.
difference := subtractTwoNumbers(3422, 233)
fmt.Println("Difference between two number is ", difference)
// 3.
length, width := 2441.43, 89.2
area, perimeter := findAreaAndPerimeter(length, width)
fmt.Println("Area: ", area, ", perimeter: ", perimeter)
// 4
radius := 32.2
areaCircle, perimeterCircle := findAreaPerimeterCircle(radius)
fmt.Println("Area and perimeter of the circle is ", areaCircle, perimeterCircle)
// 4.1 blank identifier
radius2 := 84.42
_, perimeterCircle2 := findAreaPerimeterCircle(radius2)
fmt.Println(perimeterCircle2)
fmt.Println()
// custom packages
fmt.Println("Simple Interest Calculator")
si := simpleInterest.Calculate(p, r, t)
fmt.Println("Simple Interest is, ", si)
fmt.Println()
//conditionals
//normal if
result := conditional.CheckNumber(-31)
fmt.Println(result)
//if with shorthand assignment
result2 := conditional.CheckLessOrGreater(342.4)
fmt.Println(result2)
//ideal way
conditional.CheckSign(342)
fmt.Println()
//loops
loop.PrintNumbers()
fmt.Println()
loop.ShowNumbers()
fmt.Println()
loop.PrintEvenNumbers()
fmt.Println()
loop.PrintSequence()
fmt.Println()
// switch
conditional.CheckDay()
conditional.CheckVowels("q")
conditional.CheckValue()
conditional.CheckEven()
}
// 1. A basic function
func addTwoNumbers(num1 int, num2 int ) int {
return num1 + num2
}
// 2. When parameters are of same type
func subtractTwoNumbers(num1, num2 int) int {
return num1 - num2
}
// 3. Multiple returns :)
func findAreaAndPerimeter(length, width float64) (float64, float64) {
var perimeter = (length + width) * 2
var area = length * width
return area, perimeter
}
// 4. Named return values
func findAreaPerimeterCircle(radius float64) (areaCircle float64, perimeterCircle float64) {
areaCircle = math.Pi * radius * radius
perimeterCircle = 2 * math.Pi * radius
return
}