From 60fd45cb337fc4e085466cf0026bbdbfa7d4a356 Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Sat, 11 May 2019 16:37:39 +0300 Subject: [PATCH] update: foundations --- .../01-print-args/01-printf/main.go | 36 ++++++++- .../01-for-crunch-the-primes/main.go | 10 ++- .../03-math-table-if-switch-loop/main.go | 68 ++++++++++------- .../04-lucky-number-if-for-switch/main.go | 39 ++++++++-- .../05-path-searcher-for-range/main.go | 6 +- x-tba/foundations/area-of-a-circle/main.go | 29 ++++++++ x-tba/foundations/cels-to-fahr/main.go | 20 +++++ x-tba/foundations/counter/main.go | 74 +++++++++++++++++++ x-tba/foundations/feet-to-meters/main.go | 18 +++++ x-tba/foundations/volume-of-a-sphere/main.go | 25 +++++++ 10 files changed, 282 insertions(+), 43 deletions(-) create mode 100644 x-tba/foundations/area-of-a-circle/main.go create mode 100644 x-tba/foundations/cels-to-fahr/main.go create mode 100644 x-tba/foundations/counter/main.go create mode 100644 x-tba/foundations/feet-to-meters/main.go create mode 100644 x-tba/foundations/volume-of-a-sphere/main.go diff --git a/x-tba/foundations/01-print-args/01-printf/main.go b/x-tba/foundations/01-print-args/01-printf/main.go index 341112c..aa1d946 100644 --- a/x-tba/foundations/01-print-args/01-printf/main.go +++ b/x-tba/foundations/01-print-args/01-printf/main.go @@ -10,13 +10,41 @@ package main import "fmt" func main() { + // zero-values var ( - planet = "venus" - distance = 261 - orbital = 224.701 - hasLife = false + planet string + distance int + orbital float64 + hasLife bool ) + // assignment + planet = "venus" + distance = 261 + orbital = 224.701 + hasLife = false + + // var ( + // planet = "venus" + // distance = 261 + // orbital = 224.701 + // hasLife = false + // ) + + distance += 5 + distance *= 2 + distance++ + distance-- + orbital++ + + // orbital *= 10 + const constFactor = 10 + orbital *= constFactor + + factor := 10 + // orbital *= factor + orbital *= float64(factor) + // swiss army knife %v verb fmt.Printf("Planet: %v\n", planet) fmt.Printf("Distance: %v millions kms\n", distance) diff --git a/x-tba/foundations/03-if-switch-loop/01-for-crunch-the-primes/main.go b/x-tba/foundations/03-if-switch-loop/01-for-crunch-the-primes/main.go index 1c6fbc5..3d6ee56 100644 --- a/x-tba/foundations/03-if-switch-loop/01-for-crunch-the-primes/main.go +++ b/x-tba/foundations/03-if-switch-loop/01-for-crunch-the-primes/main.go @@ -16,10 +16,18 @@ import ( ) func main() { + const usageMsg = `Type a couple of unique numbers. +Separate them with spaces.` + // remember [1:] skips the first argument + args := os.Args[1:] + if len(args) == 0 { + fmt.Println(usageMsg) + return + } main: - for _, arg := range os.Args[1:] { + for _, arg := range args { n, err := strconv.Atoi(arg) if err != nil { // skip non-numerics diff --git a/x-tba/foundations/03-if-switch-loop/03-math-table-if-switch-loop/main.go b/x-tba/foundations/03-if-switch-loop/03-math-table-if-switch-loop/main.go index 3175f8b..1f27c29 100644 --- a/x-tba/foundations/03-if-switch-loop/03-math-table-if-switch-loop/main.go +++ b/x-tba/foundations/03-if-switch-loop/03-math-table-if-switch-loop/main.go @@ -15,31 +15,21 @@ import ( ) const ( - validOps = "%*/+-" - - usageMsg = "Usage: [op=" + validOps + "] [size]" - sizeMissingMsg = "Size is missing" + validOps = "* / + - mul div add sub" + usageMsg = "Usage: [valid ops: " + validOps + "] [size]" + sizeMissingMsg = "Size is missing\n" + usageMsg invalidOpMsg = `Invalid operator. Valid ops one of: ` + validOps - - invalidOp = -1 ) func main() { + // CHECK THE ARGUMENTS args := os.Args[1:] - - switch l := len(args); { - case l == 1: + if l := len(args); l == 1 { fmt.Println(sizeMissingMsg) - fallthrough - case l < 1: - fmt.Println(usageMsg) return - } - - op := args[0] - if strings.IndexAny(op, validOps) == invalidOp { - fmt.Println(invalidOpMsg) + } else if l < 1 { + fmt.Println(usageMsg) return } @@ -49,12 +39,32 @@ func main() { return } + // CHECK THE VALIDITY OF THE OP + op, ops := args[0], strings.Fields(validOps) + + var ok bool + for _, o := range ops { + if strings.ToLower(o) == op { + ok = true + break + } + } + + if !ok { + fmt.Println(invalidOpMsg) + return + } + + // PRINT THE TABLE + + // HEADER fmt.Printf("%5s", op) for i := 0; i <= size; i++ { fmt.Printf("%5d", i) } fmt.Println() + // CELLS for i := 0; i <= size; i++ { fmt.Printf("%5d", i) @@ -62,22 +72,24 @@ func main() { var res int switch op { - case "*": + default: + fallthrough // default is multiplication + case "*", "mul": res = i * j - case "%": - if j != 0 { - res = i % j - } - case "/": - if j != 0 { - res = i / j - } - case "+": + case "+", "add": res = i + j - case "-": + case "-", "sub": res = i - j + case "%", "mod": + if j == 0 { + // continue // will continue the loop + break // breaks the switch + } + res = i % j } + // break // breaks the loop + fmt.Printf("%5d", res) } fmt.Println() diff --git a/x-tba/foundations/03-if-switch-loop/04-lucky-number-if-for-switch/main.go b/x-tba/foundations/03-if-switch-loop/04-lucky-number-if-for-switch/main.go index fdeaecb..d02ff0b 100644 --- a/x-tba/foundations/03-if-switch-loop/04-lucky-number-if-for-switch/main.go +++ b/x-tba/foundations/03-if-switch-loop/04-lucky-number-if-for-switch/main.go @@ -44,7 +44,7 @@ func main() { verbose = true } - guess, err := strconv.Atoi(args[0]) + guess, err := strconv.Atoi(args[len(args)-1]) if err != nil { fmt.Println("Not a number.") return @@ -75,13 +75,36 @@ func main() { } } - msg := "%s Try again?\n" + // msg, n := "%s Try again?\n", rand.Intn(5) + // if msg, n := "%s Try again?\n", rand.Intn(5); n <= 2 { + // fmt.Printf(msg, "☠️ YOU LOST...") + // } else if n < 3 { + // fmt.Printf(msg, "☠️ JUST A BAD LUCK...") + // } else if n == 4 { + // fmt.Printf(msg, "☠️ TRY NEXT TIME...") + // } - switch rand.Intn(2) { - case 0: - fmt.Printf(msg, "☠️ YOU LOST...") - case 1: - fmt.Printf(msg, "☠️ JUST A BAD LUCK...") + // var msg string + // switch rand.Intn(10) { + // // more probability + // case 0, 1, 2, 3, 4, 5: + // msg = "☠️ YOU LOST..." + // case 6, 7, 8: + // msg = "☠️ JUST A BAD LUCK..." + // default: + // msg = "☠️ TRY NEXT TIME..." + // } + // fmt.Printf("%s Try again?\n", msg) + + var msg string + switch n := rand.Intn(10); { + // more probability + case n <= 5: + msg = "☠️ YOU LOST..." + case n <= 8: + msg = "☠️ JUST A BAD LUCK..." + default: + msg = "☠️ TRY NEXT TIME..." } - + fmt.Printf("%s Try again?\n", msg) } diff --git a/x-tba/foundations/03-if-switch-loop/05-path-searcher-for-range/main.go b/x-tba/foundations/03-if-switch-loop/05-path-searcher-for-range/main.go index 10fdaed..d647794 100644 --- a/x-tba/foundations/03-if-switch-loop/05-path-searcher-for-range/main.go +++ b/x-tba/foundations/03-if-switch-loop/05-path-searcher-for-range/main.go @@ -32,9 +32,11 @@ func main() { for i, w := range words { q, w = strings.ToLower(q), strings.ToLower(w) - if strings.Contains(w, q) { - fmt.Printf("#%-2d: %q\n", i+1, w) + if !strings.Contains(w, q) { + continue } + + fmt.Printf("#%-2d: %q\n", i+1, w) } } } diff --git a/x-tba/foundations/area-of-a-circle/main.go b/x-tba/foundations/area-of-a-circle/main.go new file mode 100644 index 0000000..6f5658e --- /dev/null +++ b/x-tba/foundations/area-of-a-circle/main.go @@ -0,0 +1,29 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "fmt" + "math" +) + +func main() { + var ( + radius = 10. + area float64 + ) + + area = math.Pi * radius * radius + + fmt.Printf("radius: %g -> area: %.2f\n", + radius, area) + + // ALTERNATIVE: + // math.Pow calculates the power of a float number + // area = math.Pi * math.Pow(radius, 2) +} diff --git a/x-tba/foundations/cels-to-fahr/main.go b/x-tba/foundations/cels-to-fahr/main.go new file mode 100644 index 0000000..74a4fd2 --- /dev/null +++ b/x-tba/foundations/cels-to-fahr/main.go @@ -0,0 +1,20 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import "fmt" + +func main() { + celsius := 35. + + // Wrong formula : 9*celsius + 160 / 5 + // Correct formula: (9*celsius + 160) / 5 + fahrenheit := (9*celsius + 160) / 5 + + fmt.Printf("%g ºC is %g ºF\n", celsius, fahrenheit) +} diff --git a/x-tba/foundations/counter/main.go b/x-tba/foundations/counter/main.go new file mode 100644 index 0000000..1711418 --- /dev/null +++ b/x-tba/foundations/counter/main.go @@ -0,0 +1,74 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "fmt" + "strconv" +) + +func main() { + // var counter int + // var factor float64 + var ( + counter int + factor float64 + ) + + // counter = counter + 1 + counter++ + fmt.Println(counter) + + // counter = counter - 1 + counter-- + fmt.Println(counter) + + // counter = counter + 5 + counter += 5 + fmt.Println(counter) + + // counter = counter * 10 + counter *= 10 + fmt.Println(counter) + + // counter = counter / 2.0 + counter /= 2.0 + fmt.Println(counter) + + factor += float64(counter) + fmt.Println(counter) + + var bigCounter int64 + counter += int(bigCounter) + fmt.Println(counter) + + fmt.Println( + "hello" + ", " + "how" + " " + "are" + " " + "today?", + ) + + // you can combine raw string and string literals + fmt.Println( + `hello` + `, ` + `how` + ` ` + `are` + ` ` + "today?", + ) + + // ------------------------------------------ + // Converting non-string values into string + // ------------------------------------------ + + eq := "1 + 2 = " + sum := 1 + 2 + + // invalid op + // string concat op can only be used with strings + // fmt.Println(eq + sum) + + // you need to convert it using strconv.Itoa + // Itoa = Integer to ASCII + + fmt.Println(eq + strconv.Itoa(sum)) +} diff --git a/x-tba/foundations/feet-to-meters/main.go b/x-tba/foundations/feet-to-meters/main.go new file mode 100644 index 0000000..0f8cace --- /dev/null +++ b/x-tba/foundations/feet-to-meters/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "os" + "strconv" +) + +func main() { + c, _ := strconv.ParseFloat(os.Args[1], 64) + f := c*1.8 + 32 + + // Like this: + fmt.Printf("%g ºC is %g ºF\n", c, f) + + // Or just like this (both are correct): + fmt.Printf("%g ºF\n", f) +} diff --git a/x-tba/foundations/volume-of-a-sphere/main.go b/x-tba/foundations/volume-of-a-sphere/main.go new file mode 100644 index 0000000..ca27075 --- /dev/null +++ b/x-tba/foundations/volume-of-a-sphere/main.go @@ -0,0 +1,25 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "fmt" + "math" + "os" + "strconv" +) + +func main() { + var radius, vol float64 + + radius, _ = strconv.ParseFloat(os.Args[1], 64) + + vol = (4 * math.Pi * math.Pow(radius, 3)) / 3 + + fmt.Printf("radius: %g -> volume: %.2f\n", radius, vol) +}