-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
449 lines (388 loc) · 10.1 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package main
import (
"fmt"
"strings"
"sort"
"math"
)
/*func sayGreeting(name string) {
fmt.Printf("Good Morning %v\n", name)
}
func sayBye(name string) {
fmt.Printf("Good Bye %v\n", name)
}
func cycleNames(name []string, f func(string)) {
for _, value := range name {
f(value)
}
}
func circleArea(radius float64) float64 {
return math.Pi * radius * radius
}*/
/*func getInitials(name string) (string, string) {
s := strings.ToUpper(name)
names := strings.Split(s, " ")
initials := []string{}
for _, value := range names {
initials = append(initials, value[:1])
}
if len(initials) > 1 {
return initials[0], initials[1]
}
return initials[0], "_"
}*/
// Pass By Value
// Non-Pointer Values
/*func updateName(name string) string {
name = "wedge"
return name
}*/
// Pointer Wrapper Values
/*func updateMenu(menu map[string]float64) {
menu["coffee"] = 2.99
}*/
// Pass By Reference (Pointers)
/*func updateName(name *string) {
*name = "wedge"
}*/
// For structs, Pointers are automatically dereferenced
/*type bill struct {
name string
items map[string]float64
tip float64
}*/
// Make new Bills
/*func newBill(name string) bill {
b := bill{
name: name,
items: map[string]float64{},
tip: 0,
}
return b
}*/
// Format the Bill
/*func (b *bill) format() string {
fs := "Bill Breakdown: \n"
var total float64 = 0
// List Items
for key, value := range b.items {
fs += fmt.Sprintf("%-25v ...$%v \n", key+": ", value)
total += value
}
// Tip
fs += fmt.Sprintf("%-25v ...$%0.2f \n", "Tip: ", b.tip)
// Total
fs += fmt.Sprintf("%-25v ...$%0.2f", "Total: ", total+b.tip)
return fs
}*/
// Update Tip
/*func (b *bill) updateTip(tip float64) {
b.tip = tip
}*/
// Add An Item to Bill
/*func (b *bill) addItem(name string, price float64) {
b.items[name] = price
}*/
// Save Bill
/*func (b *bill) save() {
data := [] byte {b.format()}
err := os.WriteFile("bills/"+b.name+".txt", data, 0644)
if err != nil {
panic(err)
}
fmt.Println("Bill was saved to file")
}*/
// Creating a Bill
/*func createBill() bill {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Create a new bill name: ")
name, _ := getInput("Create a new bill name: ", reader)
b := newBill(name)
fmt.Println("Created the bill - ", b.name)
return b
}*/
// Fetching Input from User
/*func getInput(prompt string, r *bufio.Reader) (string, error) {
fmt.Print(prompt)
input, err := r.ReadString('\n')
return strings.TrimSpace(input), err
}*/
/*func promptOptions(b bill) {
reader := bufio.NewReader(os.Stdin)
opt, _ := getInput("Choose Option (a - add item, s - save bill, t - add tip): ", reader)
switch opt {
case "a":
name, _ := getInput("Item Name: ", reader)
price, _ := getInput("Item Price: ", reader)
p, err := strconv.ParseFloat(price, 64)
if err != nil {
fmt.Println("The price must be a number")
promptOptions(b)
}
b.addItem(name, p)
fmt.Println("Item Added - "name, price)
promptOptions(b)
case "t":
tip, _ := getInput("Tip Amount: ", reader)
t, err := strconv.ParseFloat(tip, 64)
if err != nil {
fmt.Println("The price must be a number")
promptOptions(b)
}
b.updateTip(t)
fmt.Println("Tip Added - "tip)
promptOptions(b)
case "s":
b.save()
fmt.Println("You chose to save the bill", b.name)
default:
fmt.Println("You chose an invalid option!")
}
}*/
/*
// shape interface
type shape interface {
area() float64
circumf() float64
}
type square struct {
length float64
}
type circle struct {
radius float64
}
// square methods
func (s square) area() float64 {
return s.length * s.length
}
func (s square) circumf() float64 {
return s.length * 4
}
// circle methods
func (c circle) area() float64 {
return math.Pi * c.radius * c.radius
}
func (c circle) circumf() float64 {
return 2 * math.Pi * c.radius
}
func printShapeInfo(s shape) {
fmt.Printf("area of %T is: %0.2f \n", s, s.area())
fmt.Printf("circumference of %T is: %0.2f \n", s, s.circumf())
}
*/
func main() {
// Println Function
/*fmt.Println("Hello World")*/
// Print Function
/*fmt.Print("Rengoku ")
fmt.Print("Mitsuri")*/
// Printf Function
/*name := "Mohit"
age := 26
fmt.Printf("My name is %v and my age is %v\n", name, age)
fmt.Printf("My name is %q and my age is %q\n", name, age)
fmt.Printf("Age is of type %T\n", age)
fmt.Printf("You scored %.2f points in the game\n", 92.65)
fmt.Printf("You scored %.1f points in the game\n", 92.66)*/
// Sprintf Function
/*name := "Mohit"
age := 26
var str = fmt.Sprintf("My name is %v and my age is %v\n", name, age)
fmt.Println("Saved String : ", str)*/
// Strings
/*var nameOne string = "Mohit"
var nameTwo = "Kambli"
var nameThree string
fmt.Println("Hello " + nameOne + " " + nameTwo + "!")
nameOne = "Ameya"
nameThree = "Kale"
nameFour := "Pratik"
fmt.Println("Hello " + nameOne + " " + nameThree + "!" + nameFour)*/
// Integers
/*var ageOne int = 25
var ageTwo = 30
var ageThree int
ageFour := 50
fmt.Println(ageOne, ageTwo, ageThree, ageFour)*/
// Floats
/*var scoreOne float32 = 9.5
var scoreTwo = 8.3
var scoreThree float64
scoreFour := 9.9
fmt.Println(scoreOne, scoreTwo, scoreThree, scoreFour)*/
// Arrays
/*//var ages [3]int = [3]int{20, 25, 30}
var ages = [3]int{20, 25, 30}
fmt.Println(ages, len(ages))
names := [4]string{"abc", "pqr", "lmn", "xyz"}
fmt.Println(names, len(names))*/
// Slices
/*scores := []int{10, 20, 30}
scores = append(scores, 40)
fmt.Println(scores, len(scores))*/
// Slice Ranges
/*rangeOne := scores[1:3]
fmt.Println(rangeOne, len(rangeOne))
rangeTwo := scores[2:]
fmt.Println(rangeTwo, len(rangeTwo))
rangeThree := scores[:3]
fmt.Println(rangeThree, len(rangeThree))
rangeThree = append(rangeThree, 40)
fmt.Println(rangeThree, len(rangeThree))*/
// Strings Package
//greeting := "Hello there friends!"
// (Atleast, the following methods of the strings package don't modify the original string)
/*fmt.Println(strings.Contains(greeting, "Hello"))
// Doesn't alter original string
fmt.Println(strings.ReplaceAll(greeting, "Hello", "Hi"))
fmt.Println(strings.ToUpper(greeting))
fmt.Println(strings.Index(greeting, "ll"))
fmt.Println(strings.Split(greeting, " "))
// Original Value Remains Unchanged
fmt.Println("Original String: ", greeting) */
// Sort Package
/*ages := []int{45, 20, 35, 30, 75, 60, 50, 25}
sort.Ints(ages)
// Alters the orignal slice/array
fmt.Println(ages)
// Searches in the sorted slice/array and not in the original array
validIntIndex := sort.SearchInts(ages, 60)
fmt.Println(validIntIndex)
invalidIntIndex := sort.SearchInts(ages, 100)
// Doesn't return -1, but instead returns an index which is out of bounds, specifically n
fmt.Println(invalidIntIndex)
names := []string{"yoshi", "mario", "peach", "bowser", "luigi"}
sort.Strings(names)
fmt.Println(names)
validStringIndex := sort.SearchStrings(names, "mario")
fmt.Println(validStringIndex)*/
// Loops
/*x := 0
for x < 5 {
fmt.Println("Value of X: ", x)
x++
}*/
/*for i := 0; i < 5; i++ {
fmt.Println("Value of i: ", i)
}*/
//names := []string{"mario", "luigi", "yoshi", "peach"}
/*for i := 0; i < len(names); i++ {
fmt.Println("Name: ", names[i])
}*/
/*for index, value := range names {
fmt.Printf("Index: %v, Value: %v\n", index, value)
}*/
// Underscore can be used for the option that is not required
// Option can either be the index or the actual value
// If we don't use a particular option, then we would get an error, hence use underscore
/*for _, value := range names {
fmt.Printf("Value: %v\n", value)
}*/
/*for _, value := range names {
fmt.Printf("Value: %v\n", value)
// Following line doesn't make any changes to the original slice/array
value = "new string"
}
fmt.Println(names)*/
// Booleans & Conditionals
/*age := 45
fmt.Println(age <= 50)
fmt.Println(age >= 50)
fmt.Println(age == 45)
fmt.Println(age != 50)
if age < 50 {
fmt.Println("Age is less than 50")
} else if age > 50 {
fmt.Println("Age is greater than 50")
} else {
fmt.Println("Age is equal to 50")
}*/
/*names := []string{"mario", "luigi", "yoshi", "peach", "bowser"}
for index, value := range names {
if index == 1 {
fmt.Println("Continuing at Index: ", index)
continue
}
if index > 2 {
fmt.Println("Breaking at Index: ", index)
break
}
fmt.Printf("Value at index %v : %v\n", index, value)
}*/
// Functions
/*sayGreeting("mario")
sayBye("luigi")
cycleNames([]string{"cloud", "tifa", "barret"}, sayGreeting)
cycleNames([]string{"cloud", "tifa", "barret"}, sayBye)
a1 := circleArea(10.5)
a2 := circleArea(15)
fmt.Println(a1, ", ", a2)
fmt.Printf("Circle 1 : %0.3f\nCircle 2 : %0.3f", a1, a2)*/
// More on Functions
/*firstName1, secondName1 := getInitials("tifa lockhart")
fmt.Println(firstName1, ", ", secondName1)
firstName2, secondName2 := getInitials("cloud strife")
fmt.Println(firstName2, ", ", secondName2)
firstName3, secondName3 := getInitials("barret")
fmt.Println(firstName3, ", ", secondName3)*/
// Maps
/*menu := map[string]float64{
"soup": 4.99,
"pie": 7.99,
"salad": 6.99,
"toffee pudding": 3.55,
}
fmt.Println(menu)
fmt.Println(menu["pie"])
for key, value := range menu {
fmt.Println(key, " - ", value)
}
phonebook := map[int]string{
12345: "mario",
23456: "luigi",
34567: "peach",
}
fmt.Println(phonebook)
fmt.Println(phonebook[23456])
// Updating value of one existing key
phonebook[34567] = "bowser"
for key, value := range phonebook {
fmt.Println(key, " - ", value)
}*/
// Pass By Value
// Non-Pointer Values
/*name := "tifa"
name = updateName(name)
fmt.Println(name)*/
// Pointer Wrapper Values
/*menu := map[string]float64{
"pie": 5.95,
"ice cream": 3.99,
}
updateMenu(menu)
fmt.Println(menu)*/
// Pointers
/*name := "tifa"
mName := &name
fmt.Println("Memory Address: ", &mName)
fmt.Println("Value at Memory Address: ", *mName)
fmt.Println(name)
updateName(mName)
fmt.Println(name)*/
// Bill Initialization (Struct)
/*myBill := createBill()
promptOptions(myBill)*/
/*
// Interface
shapes := []shape{
square{length: 15.2},
circle{radius: 7.5},
circle{radius: 12.3},
square{length: 4.9},
}
for _, v := range shapes {
printShapeInfo(v)
fmt.Println("---")
}
*/
}