From e2d8acf8970c32629bdc5ef57a54f72731a2be1d Mon Sep 17 00:00:00 2001 From: Vyacheslav Dmitriev Date: Sat, 12 Aug 2023 00:35:55 +0300 Subject: [PATCH] solve(learngo): 11-if --- .../01-comparison-operators/main.go | 27 --- .../01/main.go | 22 -- .../02/main.go | 22 -- .../03/main.go | 49 ----- .../01-and-operator/01/main.go | 21 -- .../01-and-operator/02/main.go | 35 --- .../02-or-operator/01/main.go | 21 -- .../02-or-operator/02/main.go | 32 --- .../03-not-operator/01/main.go | 19 -- .../03-not-operator/02/main.go | 23 -- .../02-if-statement/01-if-branch/main.go | 19 -- .../02-if-statement/02-else-branch/main.go | 21 -- .../03-else-if-branch/01/main.go | 23 -- .../03-else-if-branch/02/main.go | 25 --- .../04-refactor-feet-to-meters/main.go | 43 ---- .../01-1st-challenge/01-challenge/main.go | 37 ---- .../01-1st-challenge/02-solution/main.go | 33 --- .../03-solution-refactor/main.go | 42 ---- .../02-2nd-challenge/01-challenge/main.go | 73 ------- .../02-2nd-challenge/02-solution/main.go | 44 ---- .../11-if/03-error-handling/01-itoa/main.go | 23 -- .../11-if/03-error-handling/02-atoi/main.go | 25 --- .../03-atoi-error-handling/main.go | 45 ---- .../01-challenge/main.go | 53 ----- .../02-solution/main.go | 43 ---- .../04-short-if/01-without-short-if/main.go | 22 -- .../04-short-if/02-with-short-if/main.go | 24 --- .../11-if/04-short-if/03-scope/main.go | 45 ---- .../04-scope-shadowing/01-shadowing/main.go | 38 ---- .../02-shadowing-solution/main.go | 45 ---- .../11-if/exercises/01-age-seasons/main.go | 16 +- .../exercises/01-age-seasons/solution/main.go | 27 --- .../11-if/exercises/02-simplify-it/main.go | 14 +- .../exercises/02-simplify-it/solution/main.go | 21 -- .../11-if/exercises/03-arg-count/main.go | 20 ++ .../exercises/03-arg-count/solution/main.go | 34 --- .../11-if/exercises/04-vowel-or-cons/main.go | 23 ++ .../04-vowel-or-cons/solution/main.go | 35 --- .../04-vowel-or-cons/solution2/main.go | 40 ---- .../11-if/exercises/05-movie-ratings/main.go | 20 ++ .../05-movie-ratings/solution/main.go | 35 --- .../05-movie-ratings/solution2/main.go | 35 --- .../11-if/exercises/06-odd-even/main.go | 20 ++ .../exercises/06-odd-even/solution/main.go | 36 ---- .../11-if/exercises/07-leap-year/main.go | 40 ---- .../exercises/07-leap-year/solution/main.go | 49 ----- .../exercises/08-simplify-leap-year/main.go | 23 -- .../08-simplify-leap-year/solution/main.go | 37 ---- .../11-if/exercises/09-days-in-month/main.go | 91 -------- .../09-days-in-month/solution/main.go | 59 ----- .../11-if/questions/1-comparison-operators.md | 20 +- .../11-if/questions/2-logical-operators.md | 147 ------------- collections/learngo/11-if/questions/3-if.md | 202 ------------------ .../11-if/questions/4-error-handling.md | 20 +- .../learngo/11-if/questions/5-short-if.md | 6 +- 55 files changed, 122 insertions(+), 1942 deletions(-) delete mode 100644 collections/learngo/11-if/01-boolean-operators/01-comparison-operators/main.go delete mode 100644 collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/01/main.go delete mode 100644 collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/02/main.go delete mode 100644 collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/03/main.go delete mode 100644 collections/learngo/11-if/01-boolean-operators/03-logical-operators/01-and-operator/01/main.go delete mode 100644 collections/learngo/11-if/01-boolean-operators/03-logical-operators/01-and-operator/02/main.go delete mode 100644 collections/learngo/11-if/01-boolean-operators/03-logical-operators/02-or-operator/01/main.go delete mode 100644 collections/learngo/11-if/01-boolean-operators/03-logical-operators/02-or-operator/02/main.go delete mode 100644 collections/learngo/11-if/01-boolean-operators/03-logical-operators/03-not-operator/01/main.go delete mode 100644 collections/learngo/11-if/01-boolean-operators/03-logical-operators/03-not-operator/02/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/01-if-branch/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/02-else-branch/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/03-else-if-branch/01/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/03-else-if-branch/02/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/04-refactor-feet-to-meters/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/01-challenge/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/02-solution/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/03-solution-refactor/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/01-challenge/main.go delete mode 100644 collections/learngo/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/02-solution/main.go delete mode 100644 collections/learngo/11-if/03-error-handling/01-itoa/main.go delete mode 100644 collections/learngo/11-if/03-error-handling/02-atoi/main.go delete mode 100644 collections/learngo/11-if/03-error-handling/03-atoi-error-handling/main.go delete mode 100644 collections/learngo/11-if/03-error-handling/04-challenge-feet-to-meters/01-challenge/main.go delete mode 100644 collections/learngo/11-if/03-error-handling/04-challenge-feet-to-meters/02-solution/main.go delete mode 100644 collections/learngo/11-if/04-short-if/01-without-short-if/main.go delete mode 100644 collections/learngo/11-if/04-short-if/02-with-short-if/main.go delete mode 100644 collections/learngo/11-if/04-short-if/03-scope/main.go delete mode 100644 collections/learngo/11-if/04-short-if/04-scope-shadowing/01-shadowing/main.go delete mode 100644 collections/learngo/11-if/04-short-if/04-scope-shadowing/02-shadowing-solution/main.go delete mode 100644 collections/learngo/11-if/exercises/01-age-seasons/solution/main.go delete mode 100644 collections/learngo/11-if/exercises/02-simplify-it/solution/main.go delete mode 100644 collections/learngo/11-if/exercises/03-arg-count/solution/main.go delete mode 100644 collections/learngo/11-if/exercises/04-vowel-or-cons/solution/main.go delete mode 100644 collections/learngo/11-if/exercises/04-vowel-or-cons/solution2/main.go delete mode 100644 collections/learngo/11-if/exercises/05-movie-ratings/solution/main.go delete mode 100644 collections/learngo/11-if/exercises/05-movie-ratings/solution2/main.go delete mode 100644 collections/learngo/11-if/exercises/06-odd-even/solution/main.go delete mode 100644 collections/learngo/11-if/exercises/07-leap-year/main.go delete mode 100644 collections/learngo/11-if/exercises/07-leap-year/solution/main.go delete mode 100644 collections/learngo/11-if/exercises/08-simplify-leap-year/main.go delete mode 100644 collections/learngo/11-if/exercises/08-simplify-leap-year/solution/main.go delete mode 100644 collections/learngo/11-if/exercises/09-days-in-month/main.go delete mode 100644 collections/learngo/11-if/exercises/09-days-in-month/solution/main.go delete mode 100644 collections/learngo/11-if/questions/2-logical-operators.md delete mode 100644 collections/learngo/11-if/questions/3-if.md diff --git a/collections/learngo/11-if/01-boolean-operators/01-comparison-operators/main.go b/collections/learngo/11-if/01-boolean-operators/01-comparison-operators/main.go deleted file mode 100644 index af9ccc2..0000000 --- a/collections/learngo/11-if/01-boolean-operators/01-comparison-operators/main.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - speed := 100 // #1 - // speed := 10 // #2 - - fast := speed >= 80 - slow := speed < 20 - - fmt.Printf("fast's type is %T\n", fast) - - fmt.Printf("going fast? %t\n", fast) - fmt.Printf("going slow? %t\n", slow) - - fmt.Printf("is it 100 mph? %t\n", speed == 100) - fmt.Printf("is it not 100 mph? %t\n", speed != 100) -} diff --git a/collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/01/main.go b/collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/01/main.go deleted file mode 100644 index fbdfad7..0000000 --- a/collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/01/main.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -func main() { - var speed int - // speed = "100" // NOT OK - - var running bool - // running = 50 // NOT OK - - var force float64 - // speed = force // NOT OK - - _, _, _ = speed, running, force -} diff --git a/collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/02/main.go b/collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/02/main.go deleted file mode 100644 index 17d734c..0000000 --- a/collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/02/main.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -func main() { - var speed int - speed = 50 // OK - - var running bool - running = true // OK - - var force float64 - speed = int(force) - - _, _, _ = speed, running, force -} diff --git a/collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/03/main.go b/collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/03/main.go deleted file mode 100644 index 205898d..0000000 --- a/collections/learngo/11-if/01-boolean-operators/02-comparison-and-assignability/03/main.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "strings" -) - -func main() { - speed := 100 // #1 - // speed := 10 // #2 - - fast := speed >= 80 - slow := speed < 20 - - fmt.Printf("going fast? %t\n", fast) - fmt.Printf("going slow? %t\n", slow) - - fmt.Printf("is it 100 mph? %t\n", speed == 100) - fmt.Printf("is it not 100 mph? %t\n", speed != 100) - - fmt.Println(strings.Repeat("-", 25)) - - // ------------------------- - // #3 - speedB := 150.5 - faster := speedB > 100 // ok: untyped - - fmt.Println("is the other car going faster?", faster) - - // ERROR: Type Mismatch - // faster = speedB > speed - faster = speedB > float64(speed) - fmt.Println("is the other car going faster?", faster) - - // #4 - // ERROR: - // only numeric values are comparable with - // ordering operators: <, <=, >, >= - - // fmt.Println(true > faster) -} diff --git a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/01-and-operator/01/main.go b/collections/learngo/11-if/01-boolean-operators/03-logical-operators/01-and-operator/01/main.go deleted file mode 100644 index 40f1ca9..0000000 --- a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/01-and-operator/01/main.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - // remove the comments and run - // I've commented the lines it's because of the warnings - - // fmt.Println("true && true =", true && true) - fmt.Println("true && false =", true && false) - fmt.Println("false && true =", false && true) - // fmt.Println("false && false =", false && false) -} diff --git a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/01-and-operator/02/main.go b/collections/learngo/11-if/01-boolean-operators/03-logical-operators/01-and-operator/02/main.go deleted file mode 100644 index 5be8ed6..0000000 --- a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/01-and-operator/02/main.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - speed := 100 - fmt.Println("within limits?", - speed >= 40 && speed <= 55, - ) - - speed = 20 - fmt.Println("within limits?", - speed >= 40 && speed <= 55, - // ^- short-circuits in the first expression here - // because, it becomes false - ) - - speed = 50 - fmt.Println("within limits?", - speed >= 40 && speed <= 55, - ) - - // ERROR: invalid - // both operands should be booleans - // 1 && 2 - fmt.Println(1 == 1 && 2 == 2) -} diff --git a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/02-or-operator/01/main.go b/collections/learngo/11-if/01-boolean-operators/03-logical-operators/02-or-operator/01/main.go deleted file mode 100644 index e79dac6..0000000 --- a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/02-or-operator/01/main.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - // remove the comments and run - // I've commented the lines it's because of the warnings - - // fmt.Println("true || true =", true || true) - fmt.Println("true || false =", true || false) - fmt.Println("false || true =", false || true) - // fmt.Println("false || false =", false || false) -} diff --git a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/02-or-operator/02/main.go b/collections/learngo/11-if/01-boolean-operators/03-logical-operators/02-or-operator/02/main.go deleted file mode 100644 index 2b8e383..0000000 --- a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/02-or-operator/02/main.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - color := "red" - - fmt.Println("reddish colors?", - // true || false => true (short-circuits) - color == "red" || color == "dark red", - ) - - color = "dark red" - - fmt.Println("reddish colors?", - // false || true => true - color == "red" || color == "dark red", - ) - - fmt.Println("greenish colors?", - // false || false => false - color == "green" || color == "light green", - ) -} diff --git a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/03-not-operator/01/main.go b/collections/learngo/11-if/01-boolean-operators/03-logical-operators/03-not-operator/01/main.go deleted file mode 100644 index 11b5709..0000000 --- a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/03-not-operator/01/main.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - fmt.Println( - "hi" == "hi" && 3 > 2, // true && true => true - "hi" != "hi" || 3 > 2, // false || true => true - !("hi" != "hi" || 3 > 2), // !(false || true) => false - ) -} diff --git a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/03-not-operator/02/main.go b/collections/learngo/11-if/01-boolean-operators/03-logical-operators/03-not-operator/02/main.go deleted file mode 100644 index 19c02b9..0000000 --- a/collections/learngo/11-if/01-boolean-operators/03-logical-operators/03-not-operator/02/main.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" -) - -func main() { - var on bool - - on = !on - fmt.Println(on) - - on = !!on - fmt.Println(on) -} diff --git a/collections/learngo/11-if/02-if-statement/01-if-branch/main.go b/collections/learngo/11-if/02-if-statement/01-if-branch/main.go deleted file mode 100644 index 57c2124..0000000 --- a/collections/learngo/11-if/02-if-statement/01-if-branch/main.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - score, valid := 5, true - - if score > 3 && valid { - fmt.Println("good") - } -} diff --git a/collections/learngo/11-if/02-if-statement/02-else-branch/main.go b/collections/learngo/11-if/02-if-statement/02-else-branch/main.go deleted file mode 100644 index 31f1137..0000000 --- a/collections/learngo/11-if/02-if-statement/02-else-branch/main.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - score, valid := 3, true - - if score > 3 && valid { - fmt.Println("good") - } else { - fmt.Println("low") - } -} diff --git a/collections/learngo/11-if/02-if-statement/03-else-if-branch/01/main.go b/collections/learngo/11-if/02-if-statement/03-else-if-branch/01/main.go deleted file mode 100644 index 76a7b23..0000000 --- a/collections/learngo/11-if/02-if-statement/03-else-if-branch/01/main.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - score := 3 - - if score > 3 { - fmt.Println("good") - } else if score == 3 { - fmt.Println("on the edge") - } else { - fmt.Println("low") - } -} diff --git a/collections/learngo/11-if/02-if-statement/03-else-if-branch/02/main.go b/collections/learngo/11-if/02-if-statement/03-else-if-branch/02/main.go deleted file mode 100644 index db9d224..0000000 --- a/collections/learngo/11-if/02-if-statement/03-else-if-branch/02/main.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - score := 2 - - if score > 3 { - fmt.Println("good") - } else if score == 3 { - fmt.Println("on the edge") - } else if score == 2 { - fmt.Println("meh...") - } else { - fmt.Println("low") - } -} diff --git a/collections/learngo/11-if/02-if-statement/04-refactor-feet-to-meters/main.go b/collections/learngo/11-if/02-if-statement/04-refactor-feet-to-meters/main.go deleted file mode 100644 index 4ed7d15..0000000 --- a/collections/learngo/11-if/02-if-statement/04-refactor-feet-to-meters/main.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" - "strings" -) - -const usage = ` -Feet to Meters --------------- -This program converts feet into meters. - -Usage: -feet [feetsToConvert]` - -func main() { - if len(os.Args) < 2 { - fmt.Println(strings.TrimSpace(usage)) - - // ALTERNATIVE: - // fmt.Println("Please tell me a value in feet") - - return - } - - arg := os.Args[1] - - feet, _ := strconv.ParseFloat(arg, 64) - - meters := feet * 0.3048 - - fmt.Printf("%g feet is %g meters.\n", feet, meters) -} diff --git a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/01-challenge/main.go b/collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/01-challenge/main.go deleted file mode 100644 index 20ec9b6..0000000 --- a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/01-challenge/main.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -// --------------------------------------------------------- -// CHALLENGE #1 -// Create a user/password protected program. -// -// EXAMPLE USER -// username: jack -// password: 1888 -// -// EXPECTED OUTPUT -// go run main.go -// Usage: [username] [password] -// -// go run main.go albert -// Usage: [username] [password] -// -// go run main.go hacker 42 -// Access denied for "hacker". -// -// go run main.go jack 6475 -// Invalid password for "jack". -// -// go run main.go jack 1888 -// Access granted to "jack". -// --------------------------------------------------------- - -func main() { -} diff --git a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/02-solution/main.go b/collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/02-solution/main.go deleted file mode 100644 index 446a749..0000000 --- a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/02-solution/main.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" -) - -func main() { - args := os.Args - - if len(args) != 3 { - fmt.Println("Usage: [username] [password]") - return - } - - u, p := args[1], args[2] - - if u != "jack" { - fmt.Printf("Access denied for %q.\n", u) - } else if p != "1888" { - fmt.Printf("Invalid password for %q.\n", u) - } else { - fmt.Printf("Access granted to %q.\n", u) - } -} diff --git a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/03-solution-refactor/main.go b/collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/03-solution-refactor/main.go deleted file mode 100644 index 56069c3..0000000 --- a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/03-solution-refactor/main.go +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" -) - -const ( - usage = "Usage: [username] [password]" - errUser = "Access denied for %q.\n" - errPwd = "Invalid password for %q.\n" - accessOK = "Access granted to %q.\n" - user = "jack" - pass = "1888" -) - -func main() { - args := os.Args - - if len(args) != 3 { - fmt.Println(usage) - return - } - - u, p := args[1], args[2] - - if u != user { - fmt.Printf(errUser, u) - } else if p != pass { - fmt.Printf(errPwd, u) - } else { - fmt.Printf(accessOK, u) - } -} diff --git a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/01-challenge/main.go b/collections/learngo/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/01-challenge/main.go deleted file mode 100644 index 5352d33..0000000 --- a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/01-challenge/main.go +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" -) - -// --------------------------------------------------------- -// CHALLENGE #2 -// Add one more user to the PassMe program below. -// -// EXAMPLE USERS -// username: jack -// password: 1888 -// -// username: inanc -// password: 1879 -// -// EXPECTED OUTPUT -// go run main.go -// Usage: [username] [password] -// -// go run main.go hacker 42 -// Access denied for "hacker". -// -// go run main.go jack 1888 -// Access granted to "jack". -// -// go run main.go inanc 1879 -// Access granted to "inanc". -// -// go run main.go jack 1879 -// Invalid password for "jack". -// -// go run main.go inanc 1888 -// Invalid password for "inanc". -// --------------------------------------------------------- - -const ( - usage = "Usage: [username] [password]" - errUser = "Access denied for %q.\n" - errPwd = "Invalid password for %q.\n" - accessOK = "Access granted to %q.\n" - user = "jack" - pass = "1888" -) - -func main() { - args := os.Args - - if len(args) != 3 { - fmt.Println(usage) - return - } - - u, p := args[1], args[2] - - if u != user { - fmt.Printf(errUser, u) - } else if p != pass { - fmt.Printf(errPwd, u) - } else { - fmt.Printf(accessOK, u) - } -} diff --git a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/02-solution/main.go b/collections/learngo/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/02-solution/main.go deleted file mode 100644 index b54d96d..0000000 --- a/collections/learngo/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/02-solution/main.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" -) - -const ( - usage = "Usage: [username] [password]" - errUser = "Access denied for %q.\n" - errPwd = "Invalid password for %q.\n" - accessOK = "Access granted to %q.\n" - user, user2 = "jack", "inanc" - pass, pass2 = "1888", "1879" -) - -func main() { - args := os.Args - - if len(args) != 3 { - fmt.Println(usage) - return - } - - u, p := args[1], args[2] - - if u != user && u != user2 { - fmt.Printf(errUser, u) - } else if u == user && p == pass { - fmt.Printf(accessOK, u) - } else if u == user2 && p == pass2 { - fmt.Printf(accessOK, u) - } else { - fmt.Printf(errPwd, u) - } -} diff --git a/collections/learngo/11-if/03-error-handling/01-itoa/main.go b/collections/learngo/11-if/03-error-handling/01-itoa/main.go deleted file mode 100644 index daf584b..0000000 --- a/collections/learngo/11-if/03-error-handling/01-itoa/main.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "strconv" -) - -func main() { - // Itoa doesn't return any errors - // So, you don't have to handle the errors for it - - s := strconv.Itoa(42) - - fmt.Println(s) -} diff --git a/collections/learngo/11-if/03-error-handling/02-atoi/main.go b/collections/learngo/11-if/03-error-handling/02-atoi/main.go deleted file mode 100644 index 89018ca..0000000 --- a/collections/learngo/11-if/03-error-handling/02-atoi/main.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -func main() { - // Atoi returns an error value - // So, you should always check it - - n, err := strconv.Atoi(os.Args[1]) - - fmt.Println("Converted number :", n) - fmt.Println("Returned error value:", err) -} diff --git a/collections/learngo/11-if/03-error-handling/03-atoi-error-handling/main.go b/collections/learngo/11-if/03-error-handling/03-atoi-error-handling/main.go deleted file mode 100644 index 31abb96..0000000 --- a/collections/learngo/11-if/03-error-handling/03-atoi-error-handling/main.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -func main() { - age := os.Args[1] - - // Atoi returns an int and an error value - // So, you need to handle the errors - - n, err := strconv.Atoi(age) - - // handle the error immediately and quit - // don't do anything else here - - if err != nil { - fmt.Println("ERROR:", err) - - // quits/returns from the main function - // so, the program ends - return - } - - // DO NOT DO THIS: - // else { - // happy path - // } - - // DO THIS: - - // happy path (err is nil) - fmt.Printf("SUCCESS: Converted %q to %d.\n", age, n) -} diff --git a/collections/learngo/11-if/03-error-handling/04-challenge-feet-to-meters/01-challenge/main.go b/collections/learngo/11-if/03-error-handling/04-challenge-feet-to-meters/01-challenge/main.go deleted file mode 100644 index 1319de5..0000000 --- a/collections/learngo/11-if/03-error-handling/04-challenge-feet-to-meters/01-challenge/main.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" - "strings" -) - -// --------------------------------------------------------- -// CHALLENGE -// Add error handling to the feet to meters program. -// -// EXPECTED OUTPUT -// go run main.go hello -// error: 'hello' is not a number. -// -// go run main.go what -// error: 'what' is not a number. -// -// go run main.go 100 -// 100 feet is 30.48 meters. -// --------------------------------------------------------- - -const usage = ` -Feet to Meters --------------- -This program converts feet into meters. - -Usage: -feet [feetsToConvert]` - -func main() { - if len(os.Args) < 2 { - fmt.Println(strings.TrimSpace(usage)) - return - } - - arg := os.Args[1] - - feet, _ := strconv.ParseFloat(arg, 64) - meters := feet * 0.3048 - - fmt.Printf("%g feet is %g meters.\n", feet, meters) -} diff --git a/collections/learngo/11-if/03-error-handling/04-challenge-feet-to-meters/02-solution/main.go b/collections/learngo/11-if/03-error-handling/04-challenge-feet-to-meters/02-solution/main.go deleted file mode 100644 index 5dfaa5f..0000000 --- a/collections/learngo/11-if/03-error-handling/04-challenge-feet-to-meters/02-solution/main.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" - "strings" -) - -const usage = ` -Feet to Meters --------------- -This program converts feet into meters. - -Usage: -feet [feetsToConvert]` - -func main() { - if len(os.Args) < 2 { - fmt.Println(strings.TrimSpace(usage)) - return - } - - arg := os.Args[1] - - feet, err := strconv.ParseFloat(arg, 64) - if err != nil { - fmt.Printf("error: '%s' is not a number.\n", arg) - return - } - - meters := feet * 0.3048 - - fmt.Printf("%g feet is %g meters.\n", feet, meters) -} diff --git a/collections/learngo/11-if/04-short-if/01-without-short-if/main.go b/collections/learngo/11-if/04-short-if/01-without-short-if/main.go deleted file mode 100644 index 74a9241..0000000 --- a/collections/learngo/11-if/04-short-if/01-without-short-if/main.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "strconv" -) - -func main() { - n, err := strconv.Atoi("42") - - if err == nil { - fmt.Println("There was no error, n is", n) - } -} diff --git a/collections/learngo/11-if/04-short-if/02-with-short-if/main.go b/collections/learngo/11-if/04-short-if/02-with-short-if/main.go deleted file mode 100644 index 9fb1657..0000000 --- a/collections/learngo/11-if/04-short-if/02-with-short-if/main.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "strconv" -) - -func main() { - if n, err := strconv.Atoi("42"); err == nil { - // n and err are available here - fmt.Println("There was no error, n is", n) - } - - // n and err are not available here - // fmt.Println(n, err) -} diff --git a/collections/learngo/11-if/04-short-if/03-scope/main.go b/collections/learngo/11-if/04-short-if/03-scope/main.go deleted file mode 100644 index 9bf2b8e..0000000 --- a/collections/learngo/11-if/04-short-if/03-scope/main.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -func main() { - if a := os.Args; len(a) != 2 { - - // only a is available here - fmt.Println("Give me a number.") - - // no need to return on error - // return - - } else if n, err := strconv.Atoi(a[1]); err != nil { - - // a, n and err are available here - fmt.Printf("Cannot convert %q.\n", a[1]) - - // no need to return on error - // return - - } else { - // all of the variables (names) - // are available here - fmt.Printf("%s * 2 is %d\n", a[1], n*2) - } - - // a, n and err are not available here - // they belong to the if statement - - // TRY: - // fmt.Println(a, n, err) -} diff --git a/collections/learngo/11-if/04-short-if/04-scope-shadowing/01-shadowing/main.go b/collections/learngo/11-if/04-short-if/04-scope-shadowing/01-shadowing/main.go deleted file mode 100644 index 3ced9cd..0000000 --- a/collections/learngo/11-if/04-short-if/04-scope-shadowing/01-shadowing/main.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -func main() { - // UNCOMMENT THIS TO SEE IT IN ACTION: - // var n int - - if a := os.Args; len(a) != 2 { - fmt.Println("Give me a number.") - } else if n, err := strconv.Atoi(a[1]); err != nil { - // else if here shadows the main func's n - // variable and it declares it own - // with the same name: n - - fmt.Printf("Cannot convert %q.\n", a[1]) - } else { - fmt.Printf("%s * 2 is %d\n", a[1], n*2) - } - - // n here belongs to the main func - // not to the if statement above - - // UNCOMMENT ALSO LINES BELOW TO SEE IT IN ACTION: - // fmt.Printf("n is %d. 👻 👻 👻 - you've been shadowed ;-)\n", n) -} diff --git a/collections/learngo/11-if/04-short-if/04-scope-shadowing/02-shadowing-solution/main.go b/collections/learngo/11-if/04-short-if/04-scope-shadowing/02-shadowing-solution/main.go deleted file mode 100644 index a905334..0000000 --- a/collections/learngo/11-if/04-short-if/04-scope-shadowing/02-shadowing-solution/main.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -func main() { - // n and err belongs to the main function now - // not only to the if statement below - var ( - n int - err error - ) - - if a := os.Args; len(a) != 2 { - fmt.Println("Give me a number.") - } else if n, err = strconv.Atoi(a[1]); err != nil { - // here else if uses the main func's n and err - // variables instead of its own - - fmt.Printf("Cannot convert %q.\n", a[1]) - } else { - // assigns the result back into main func's - // n variable - n *= 2 - - fmt.Printf("%s * 2 is %d\n", a[1], n) - } - - // if statement has calculated the result using - // the main func's n variable - // - // so, that's why it prints the correct result here - fmt.Println("n is", n) -} diff --git a/collections/learngo/11-if/exercises/01-age-seasons/main.go b/collections/learngo/11-if/exercises/01-age-seasons/main.go index 7f4edcb..e911b57 100644 --- a/collections/learngo/11-if/exercises/01-age-seasons/main.go +++ b/collections/learngo/11-if/exercises/01-age-seasons/main.go @@ -8,6 +8,8 @@ package main +import "fmt" + // --------------------------------------------------------- // EXERCISE: Age Seasons // @@ -29,7 +31,19 @@ package main func main() { // Change this accordingly to produce the expected outputs - // age := 10 + age := 10 + + if age > 60 { + fmt.Println("Getting older") + } else if age > 30 { + fmt.Println("Getting wiser") + } else if age > 20 { + fmt.Println("Adulthood") + } else if age > 10 { + fmt.Println("Young blood") + } else { + fmt.Println("Booting up") + } // Type your if statement here. } diff --git a/collections/learngo/11-if/exercises/01-age-seasons/solution/main.go b/collections/learngo/11-if/exercises/01-age-seasons/solution/main.go deleted file mode 100644 index e302a03..0000000 --- a/collections/learngo/11-if/exercises/01-age-seasons/solution/main.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - age := 10 - - if age > 60 { - fmt.Println("Getting older") - } else if age > 30 { - fmt.Println("Getting wiser") - } else if age > 20 { - fmt.Println("Adulthood") - } else if age > 10 { - fmt.Println("Young blood") - } else { - fmt.Println("Booting up") - } -} diff --git a/collections/learngo/11-if/exercises/02-simplify-it/main.go b/collections/learngo/11-if/exercises/02-simplify-it/main.go index a3da51e..b979d30 100644 --- a/collections/learngo/11-if/exercises/02-simplify-it/main.go +++ b/collections/learngo/11-if/exercises/02-simplify-it/main.go @@ -31,19 +31,7 @@ func main() { // DO NOT TOUCH THIS isSphere, radius := true, 200 - var big bool - - if radius >= 50 { - if radius >= 100 { - if radius >= 200 { - big = true - } - } - } - - if big != true { - fmt.Println("I don't know.") - } else if !(isSphere == false) { + if isSphere && radius >= 200 { fmt.Println("It's a big sphere.") } else { fmt.Println("I don't know.") diff --git a/collections/learngo/11-if/exercises/02-simplify-it/solution/main.go b/collections/learngo/11-if/exercises/02-simplify-it/solution/main.go deleted file mode 100644 index 53ebc81..0000000 --- a/collections/learngo/11-if/exercises/02-simplify-it/solution/main.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import "fmt" - -func main() { - isSphere, radius := true, 200 - - if isSphere && radius >= 200 { - fmt.Println("It's a big sphere.") - } else { - fmt.Println("I don't know.") - } -} diff --git a/collections/learngo/11-if/exercises/03-arg-count/main.go b/collections/learngo/11-if/exercises/03-arg-count/main.go index 3efb505..4de21f0 100644 --- a/collections/learngo/11-if/exercises/03-arg-count/main.go +++ b/collections/learngo/11-if/exercises/03-arg-count/main.go @@ -8,6 +8,11 @@ package main +import ( + "fmt" + "os" +) + // --------------------------------------------------------- // EXERCISE: Arg Count // @@ -30,4 +35,19 @@ package main // --------------------------------------------------------- func main() { + args := os.Args + l := len(args) - 1 + + if l == 0 { + fmt.Println("Give me args") + } else if l == 1 { + fmt.Printf("There is one: %q\n", args[1]) + } else if l == 2 { + fmt.Printf( + `There are two: "%s %s"`+"\n", + args[1], args[2], + ) + } else { + fmt.Printf("There are %d arguments\n", l) + } } diff --git a/collections/learngo/11-if/exercises/03-arg-count/solution/main.go b/collections/learngo/11-if/exercises/03-arg-count/solution/main.go deleted file mode 100644 index 62c5984..0000000 --- a/collections/learngo/11-if/exercises/03-arg-count/solution/main.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" -) - -func main() { - var ( - args = os.Args - l = len(args) - 1 - ) - - if l == 0 { - fmt.Println("Give me args") - } else if l == 1 { - fmt.Printf("There is one: %q\n", args[1]) - } else if l == 2 { - fmt.Printf( - `There are two: "%s %s"`+"\n", - args[1], args[2], - ) - } else { - fmt.Printf("There are %d arguments\n", l) - } -} diff --git a/collections/learngo/11-if/exercises/04-vowel-or-cons/main.go b/collections/learngo/11-if/exercises/04-vowel-or-cons/main.go index 5cc8e1b..a6f3db3 100644 --- a/collections/learngo/11-if/exercises/04-vowel-or-cons/main.go +++ b/collections/learngo/11-if/exercises/04-vowel-or-cons/main.go @@ -8,6 +8,11 @@ package main +import ( + "fmt" + "os" +) + // --------------------------------------------------------- // EXERCISE: Vowel or Consonant // @@ -50,4 +55,22 @@ package main // --------------------------------------------------------- func main() { + args := os.Args + + if len(args) != 2 || len(args[1]) != 1 { + fmt.Println("Give me a letter") + return + } + + // I didn't use a short-if here because, it's already + // hard to read. Do not make it harder. + + s := args[1] + if s == "a" || s == "e" || s == "i" || s == "o" || s == "u" { + fmt.Printf("%q is a vowel.\n", s) + } else if s == "w" || s == "y" { + fmt.Printf("%q is sometimes a vowel, sometimes not.\n", s) + } else { + fmt.Printf("%q is a consonant.\n", s) + } } diff --git a/collections/learngo/11-if/exercises/04-vowel-or-cons/solution/main.go b/collections/learngo/11-if/exercises/04-vowel-or-cons/solution/main.go deleted file mode 100644 index fca5301..0000000 --- a/collections/learngo/11-if/exercises/04-vowel-or-cons/solution/main.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" -) - -func main() { - args := os.Args - - if len(args) != 2 || len(args[1]) != 1 { - fmt.Println("Give me a letter") - return - } - - // I didn't use a short-if here because, it's already - // hard to read. Do not make it harder. - - s := args[1] - if s == "a" || s == "e" || s == "i" || s == "o" || s == "u" { - fmt.Printf("%q is a vowel.\n", s) - } else if s == "w" || s == "y" { - fmt.Printf("%q is sometimes a vowel, sometimes not.\n", s) - } else { - fmt.Printf("%q is a consonant.\n", s) - } -} diff --git a/collections/learngo/11-if/exercises/04-vowel-or-cons/solution2/main.go b/collections/learngo/11-if/exercises/04-vowel-or-cons/solution2/main.go deleted file mode 100644 index 88a59b4..0000000 --- a/collections/learngo/11-if/exercises/04-vowel-or-cons/solution2/main.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strings" -) - -func main() { - args := os.Args - - if len(args) != 2 || len(args[1]) != 1 { - fmt.Println("Give me a letter") - return - } - - s := args[1] - if strings.IndexAny(s, "aeiou") != -1 { - fmt.Printf("%q is a vowel.\n", s) - } else if s == "w" || s == "y" { - fmt.Printf("%q is sometimes a vowel, sometimes not.\n", s) - } else { - fmt.Printf("%q is a consonant.\n", s) - } - - // Notice that: - // - // I didn't use IndexAny for the else if above. - // - // It's because, calling a function is a costly operation. - // And, this way, the code is simpler. -} diff --git a/collections/learngo/11-if/exercises/05-movie-ratings/main.go b/collections/learngo/11-if/exercises/05-movie-ratings/main.go index c3b35ea..f4f8fd8 100644 --- a/collections/learngo/11-if/exercises/05-movie-ratings/main.go +++ b/collections/learngo/11-if/exercises/05-movie-ratings/main.go @@ -8,6 +8,12 @@ package main +import ( + "fmt" + "os" + "strconv" +) + // --------------------------------------------------------- // STORY // @@ -53,4 +59,18 @@ package main // --------------------------------------------------------- func main() { + if len(os.Args) < 2 { + fmt.Println("If age data is wrong or absent let the user know.") + } + + if age, err := strconv.Atoi(os.Args[1]); err != nil { + fmt.Println("If age data is wrong or absent let the user know.") + } else if age > 17 { + fmt.Println("R-Rated") + } else if age >= 13 && age <= 17 { + fmt.Println("PG-13") + } else if age < 13 { + fmt.Println("PG-Rated") + } + } diff --git a/collections/learngo/11-if/exercises/05-movie-ratings/solution/main.go b/collections/learngo/11-if/exercises/05-movie-ratings/solution/main.go deleted file mode 100644 index b0e88ab..0000000 --- a/collections/learngo/11-if/exercises/05-movie-ratings/solution/main.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -func main() { - if len(os.Args) != 2 { - fmt.Println("Requires age") - return - } - - age, err := strconv.Atoi(os.Args[1]) - - if err != nil || age < 0 { - fmt.Printf(`Wrong age: %q`+"\n", os.Args[1]) - return - } else if age > 17 { - fmt.Println("R-Rated") - } else if age >= 13 && age <= 17 { - fmt.Println("PG-13") - } else if age < 13 { - fmt.Println("PG-Rated") - } -} diff --git a/collections/learngo/11-if/exercises/05-movie-ratings/solution2/main.go b/collections/learngo/11-if/exercises/05-movie-ratings/solution2/main.go deleted file mode 100644 index f440938..0000000 --- a/collections/learngo/11-if/exercises/05-movie-ratings/solution2/main.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -// 🛑 DON'T DO THIS: -// It's hard to read. -// It's just an exercise. - -func main() { - if len(os.Args) != 2 { - fmt.Println("Requires age") - return - } else if age, err := strconv.Atoi(os.Args[1]); err != nil || age < 0 { - fmt.Printf(`Wrong age: %q`+"\n", os.Args[1]) - return - } else if age > 17 { - fmt.Println("R-Rated") - } else if age >= 13 && age <= 17 { - fmt.Println("PG-13") - } else if age < 13 { - fmt.Println("PG-Rated") - } -} diff --git a/collections/learngo/11-if/exercises/06-odd-even/main.go b/collections/learngo/11-if/exercises/06-odd-even/main.go index 7801a6d..8da55ac 100644 --- a/collections/learngo/11-if/exercises/06-odd-even/main.go +++ b/collections/learngo/11-if/exercises/06-odd-even/main.go @@ -8,6 +8,12 @@ package main +import ( + "fmt" + "os" + "strconv" +) + // --------------------------------------------------------- // EXERCISE: Odd or Even // @@ -37,4 +43,18 @@ package main // --------------------------------------------------------- func main() { + if len(os.Args) < 2 { + fmt.Println("not provided") + } + + if value, err := strconv.Atoi(os.Args[1]); err != nil { + fmt.Printf("%q is not a number", os.Args[1]) + } else if value%8 == 0 { + fmt.Printf("%d is an even number and it's divisible by 8\n", value) + } else if value%2 == 0 { + fmt.Printf("%d is an even number\n", value) + } else { + fmt.Printf("%d is an odd number\n", value) + } + } diff --git a/collections/learngo/11-if/exercises/06-odd-even/solution/main.go b/collections/learngo/11-if/exercises/06-odd-even/solution/main.go deleted file mode 100644 index 482b678..0000000 --- a/collections/learngo/11-if/exercises/06-odd-even/solution/main.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -func main() { - if len(os.Args) != 2 { - fmt.Println("Pick a number") - return - } - - n, err := strconv.Atoi(os.Args[1]) - if err != nil { - fmt.Printf("%q is not a number\n", os.Args[1]) - return - } - - if n%8 == 0 { - fmt.Printf("%d is an even number and it's divisible by 8\n", n) - } else if n%2 == 0 { - fmt.Printf("%d is an even number\n", n) - } else { - fmt.Printf("%d is an odd number\n", n) - } -} diff --git a/collections/learngo/11-if/exercises/07-leap-year/main.go b/collections/learngo/11-if/exercises/07-leap-year/main.go deleted file mode 100644 index f5ccafa..0000000 --- a/collections/learngo/11-if/exercises/07-leap-year/main.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -// --------------------------------------------------------- -// EXERCISE: Leap Year -// -// Find out whether a given year is a leap year or not. -// -// EXPECTED OUTPUT -// go run main.go -// Give me a year number -// -// go run main.go eighties -// "eighties" is not a valid year. -// -// go run main.go 2018 -// 2018 is not a leap year. -// -// go run main.go 2100 -// 2100 is not a leap year. -// -// go run main.go 2019 -// 2019 is not a leap year. -// -// go run main.go 2020 -// 2020 is a leap year. -// -// go run main.go 2024 -// 2024 is a leap year. -// --------------------------------------------------------- - -func main() { -} diff --git a/collections/learngo/11-if/exercises/07-leap-year/solution/main.go b/collections/learngo/11-if/exercises/07-leap-year/solution/main.go deleted file mode 100644 index e3f79d8..0000000 --- a/collections/learngo/11-if/exercises/07-leap-year/solution/main.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -func main() { - if len(os.Args) != 2 { - fmt.Println("Give me a year number") - return - } - - year, err := strconv.Atoi(os.Args[1]) - if err != nil { - fmt.Printf("%q is not a valid year.\n", os.Args[1]) - return - } - - // Notice that: - // I've intentionally created this solution as verbose - // as I can. - // - // See the next exercise. - - var leap bool - if year%400 == 0 { - leap = true - } else if year%100 == 0 { - leap = false - } else if year%4 == 0 { - leap = true - } - - if leap { - fmt.Printf("%d is a leap year.\n", year) - } else { - fmt.Printf("%d is not a leap year.\n", year) - } -} diff --git a/collections/learngo/11-if/exercises/08-simplify-leap-year/main.go b/collections/learngo/11-if/exercises/08-simplify-leap-year/main.go deleted file mode 100644 index c401912..0000000 --- a/collections/learngo/11-if/exercises/08-simplify-leap-year/main.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -// --------------------------------------------------------- -// EXERCISE: Simplify the Leap Year -// -// 1. Look at the solution of "the previous exercise". -// -// 2. And simplify the code (especially the if statements!). -// -// EXPECTED OUTPUT -// It's the same as the previous exercise. -// --------------------------------------------------------- - -func main() { -} diff --git a/collections/learngo/11-if/exercises/08-simplify-leap-year/solution/main.go b/collections/learngo/11-if/exercises/08-simplify-leap-year/solution/main.go deleted file mode 100644 index 7208da8..0000000 --- a/collections/learngo/11-if/exercises/08-simplify-leap-year/solution/main.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strconv" -) - -func main() { - if len(os.Args) != 2 { - fmt.Println("Give me a year number") - return - } - - year, err := strconv.Atoi(os.Args[1]) - if err != nil { - fmt.Printf("%q is not a valid year.\n", os.Args[1]) - return - } - - if year%4 == 0 && (year%100 != 0 || year%400 == 0) { - fmt.Printf("%d is a leap year.\n", year) - } else { - fmt.Printf("%d is not a leap year.\n", year) - } -} - -// Review the original source code here: -// https://github.com/golang/go/blob/ad644d2e86bab85787879d41c2d2aebbd7c57db8/src/time/time.go#L1289 diff --git a/collections/learngo/11-if/exercises/09-days-in-month/main.go b/collections/learngo/11-if/exercises/09-days-in-month/main.go deleted file mode 100644 index 0d3d6d5..0000000 --- a/collections/learngo/11-if/exercises/09-days-in-month/main.go +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -// --------------------------------------------------------- -// EXERCISE: Days in a Month -// -// Print the number of days in a given month. -// -// RESTRICTIONS -// 1. On a leap year, february should print 29. Otherwise, 28. -// -// Set your computer clock to 2020 to see whether it works. -// -// 2. It should work case-insensitive. See below. -// -// Search on Google: golang pkg strings ToLower -// -// 3. Get the current year using the time.Now() -// -// Search on Google: golang pkg time now year -// -// -// EXPECTED OUTPUT -// -// ----------------------------------------- -// Your solution should not accept invalid months -// ----------------------------------------- -// go run main.go -// Give me a month name -// -// go run main.go sheep -// "sheep" is not a month. -// -// ----------------------------------------- -// Your solution should handle the leap years -// ----------------------------------------- -// go run main.go january -// "january" has 31 days. -// -// go run main.go february -// "february" has 28 days. -// -// go run main.go march -// "march" has 31 days. -// -// go run main.go april -// "april" has 30 days. -// -// go run main.go may -// "may" has 31 days. -// -// go run main.go june -// "june" has 30 days. -// -// go run main.go july -// "july" has 31 days. -// -// go run main.go august -// "august" has 31 days. -// -// go run main.go september -// "september" has 30 days. -// -// go run main.go october -// "october" has 31 days. -// -// go run main.go november -// "november" has 30 days. -// -// go run main.go december -// "december" has 31 days. -// -// ----------------------------------------- -// Your solution should be case insensitive -// ----------------------------------------- -// go run main.go DECEMBER -// "DECEMBER" has 31 days. -// -// go run main.go dEcEmBeR -// "dEcEmBeR" has 31 days. -// --------------------------------------------------------- - -func main() { -} diff --git a/collections/learngo/11-if/exercises/09-days-in-month/solution/main.go b/collections/learngo/11-if/exercises/09-days-in-month/solution/main.go deleted file mode 100644 index 950bece..0000000 --- a/collections/learngo/11-if/exercises/09-days-in-month/solution/main.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// -// For more tutorials : https://learngoprogramming.com -// In-person training : https://www.linkedin.com/in/inancgumus/ -// Follow me on twitter: https://twitter.com/inancgumus - -package main - -import ( - "fmt" - "os" - "strings" - "time" -) - -func main() { - if len(os.Args) != 2 { - fmt.Println("Give me a month name") - return - } - - // get the current year and find out whether it's a leap year - year := time.Now().Year() - leap := year%4 == 0 && (year%100 != 0 || year%400 == 0) - - // setting it to 28, saves me typing it below again - days := 28 - - month := os.Args[1] - - // case insensitive - if m := strings.ToLower(month); m == "april" || - m == "june" || - m == "september" || - m == "november" { - days = 30 - } else if m == "january" || - m == "march" || - m == "may" || - m == "july" || - m == "august" || - m == "october" || - m == "december" { - days = 31 - } else if m == "february" { - // try a "logical and operator" above. - // like: `else if m == "february" && leap` - if leap { - days = 29 - } - } else { - fmt.Printf("%q is not a month.\n", month) - return - } - - fmt.Printf("%q has %d days.\n", month, days) -} diff --git a/collections/learngo/11-if/questions/1-comparison-operators.md b/collections/learngo/11-if/questions/1-comparison-operators.md index 7a329df..242c2cb 100644 --- a/collections/learngo/11-if/questions/1-comparison-operators.md +++ b/collections/learngo/11-if/questions/1-comparison-operators.md @@ -1,7 +1,7 @@ ## Which one below is not one of the equality operators of Go? 1. `==` 2. `!=` -3. `>` +3. `>` `CORRECT` > **3:** That's the greater operator. It checks whether an ordered value is greater than the other or not. @@ -9,7 +9,7 @@ ## Which one below is not one of the ordering operators of Go? 1. `>` 2. `<=` -3. `==` +3. `==` `CORRECT` 4. `<` > **3:** That's the equal operator. In an expression, it checks whether a value (operand) is equal to another value (operand). @@ -18,7 +18,7 @@ ## Which one of these types is returned by the comparison operators? 1. int 2. byte -3. bool +3. bool `CORRECT` 4. float64 > **3:** That's right. All the comparison operators return an untyped bool value (true or false). @@ -28,7 +28,7 @@ 1. int value 2. byte value 3. string value -4. bool value +4. bool value `CORRECT` 5. all of them > **1-2:** This is an ordered value, it can be used. @@ -43,7 +43,7 @@ 2. byte value 3. string value 4. bool value -5. They all can be used +5. They all can be used `CORRECT` > **5:** That's right. Every **comparable value** can be used as an operand to equality operators. @@ -55,7 +55,7 @@ fmt.Println("go" == "go!") ``` 1. true true -2. true false +2. true false `CORRECT` 3. false true 4. false false 5. error @@ -72,7 +72,7 @@ fmt.Println(1 == true) 2. 1 3. false 4. 2 -5. error +5. error `CORRECT` > **5:** That's right. A numeric constant cannot be compared to a bool value. @@ -85,7 +85,7 @@ fmt.Println(2.9 <= 2.9) 1. true true 2. true false -3. false true +3. false true `CORRECT` 4. false false 5. error @@ -100,7 +100,7 @@ fmt.Println(true <= false) 2. true false 3. false true 4. false false -5. error +5. error `CORRECT` > **5:** That's right. Bool values are not ordered values, so they cannot be compared using the comparison operators. @@ -122,7 +122,7 @@ func main() { 2. `weight *= float64(factor)` 3. `weight *= int(factor)` 4. `weight = float64(weight) * factor` -5. `weight = int(float64(weight) * factor)` +5. `weight = int(float64(weight) * factor)` `CORRECT` > **1:** It can be fixed. > diff --git a/collections/learngo/11-if/questions/2-logical-operators.md b/collections/learngo/11-if/questions/2-logical-operators.md deleted file mode 100644 index 130a9bc..0000000 --- a/collections/learngo/11-if/questions/2-logical-operators.md +++ /dev/null @@ -1,147 +0,0 @@ -## Which one below is not one of the logical operators of Go? -1. `||` -2. `!=` -3. `!` -4. `&&` - -> **2:** That's the "not equal" operator. It's a comparison operator, not a logical operator. - - -## Which one of these types is returned by a logical operator? -1. int -2. byte -3. bool -4. float64 - -> **3:** That's right. All the logical operators return an untyped bool value (true or false). - - -## Which one of these can be used as an operand to a logical operator? -1. int -2. byte -3. bool -4. float64 - -> **3:** That's right. All the logical operators expect a bool value (or a bool expression that yields a bool value). - - -## Which expression below equals to the sentence below? -"age is equal or above 15 and hair color is yellow" - -1. `age > 15 || hairColor == "yellow"` -2. `age < 15 || hairColor != "yellow"` -3. `age >= 15 && hairColor == "yellow"` -4. `age > 15 && hairColor == "yellow"` - - -## What does this program print? -```go -package main -import "fmt" - -func main() { - var ( - on = true - off = !on - ) - - fmt.Println(!on && !off) - fmt.Println(!on || !off) -} -``` - -1. true true -2. true false -3. false true -4. false false -5. error - -> **3:** `!on` is false. `!off` is true. So, `!on && !off` is false. And, `!on || !off` is true. - - -## What does this program print? -```go -package main -import "fmt" - -func main() { - on := 1 - fmt.Println(on == true) -} -``` - -1. true -2. false -3. error - -> **3:** `on` is int, while `true` is a bool. So, there's a type mismatch error here. Go is not like other C based languages where `1` equals to `true`. - - -## What does this code print? -```go -// Note: "a" comes before "b" -a := "a" > "b" -b := "b" <= "c" -fmt.Println(a || b) -``` - -1. "a" -2. "b" -3. true -4. false -5. error - -> **1-2:** Logical operators return a bool value only. - -> **3:** Order is like so: "a", "b", "c". So, `"a" > "b"` is false. `"b" <= "c"` is true. So, `a || b` is true. - -> **5:** There isn't an error. Strings are actually numbers, so, they're ordered and can be compared using the ordering operators. - - -## What does the following program print? -```go -// Let's say that there are two functions like this: -// -// isOn() which returns true and prints "on ". -// isOff() which returns false and prints "off ". -// -// Remember: Logical operators short-circuit. - -package main -import "fmt" - -func main() { - _ = isOff() && isOn() - _ = isOn() || isOff() -} - -// Don't mind about these functions. -// Just focus on the problem. -// They are here just for you to understand what's going on better. -func isOn() bool { - fmt.Print("on ") - return true -} - -func isOff() bool { - fmt.Print("off ") - return false -} -``` - -1. "on off on off " -2. "off on " -3. "off on on off " -4. "on off " - -> **1, 3:** Remember: Logical operators short-circuit. - -> **2:** That's right. -> -> In: `isOff() && isOn()`, `isOff()` returns false, so, logical AND operator short-circuits and doesn't call `isOn()`; so it prints: `"off "`. -> -> Then, in: `isOn() || isOff()`, `isOn()` returns true, so, logical OR operator short circuits and doesn't call `isOff()`; so it prints `"on "`. - -> **4:** Think again. - -Example program is [here](https://play.golang.org/p/6z3afaOf7yT). \ No newline at end of file diff --git a/collections/learngo/11-if/questions/3-if.md b/collections/learngo/11-if/questions/3-if.md deleted file mode 100644 index 337e5bc..0000000 --- a/collections/learngo/11-if/questions/3-if.md +++ /dev/null @@ -1,202 +0,0 @@ -## What does "control flow" mean? -1. Changing the top-to-bottom execution of a program -2. Changing the left-to-right execution of a program -3. Deciding which statements are executed - -> **1, 2:** You can't change that. -> -> **3:** That's right. Control flow allows us to decide which parts of a program is to be run depending on condition values such as true or false. - - -## How can you simplify the condition expression of this if statement? -```go -if (mood == "perfect") { - // this code is not important -} -``` - -1. `if {mood == "perfect"}` -2. `if [mood == "perfect"]` -3. `if mood = "perfect"` -4. `if mood == "perfect"` - -> **1, 2:** That's a syntax error. Try again. -> -> **3:** `=` is the assignment operator. It cannot be used as a condition. -> -> **4:** That's right. In Go, you don't need to use the parentheses. - - -## The following code doesn't work. How you can fix it? -```go -package main - -import "fmt" - -func main() { - // this program prints "cool" - // when the mood is "happy" - - mood := "happy" - - if "happy" { - fmt.Println("cool") - } -} -``` -1. Just wrap the "happy" inside parentheses. -2. You need to compare mood with "happy". Like this: `if mood == "happy" { ... }` -3. Just directly use `mood` instead of `happy`. Like this: `if mood { ... }` -4. This should work! This is a tricky question. - -> **1:** That won't change anything. Go adds the parentheses automatically behind the scenes for every if statement. -> -> **2:** Yep. In Go, condition expressions always yield a bool value. Using a comparison operator will yield a bool value. So, it will work. -> -> **4:** No, it's not :) - - -## How can you simplify the following code? You only need to change the condition expression, but how? -```go -package main - -import "fmt" - -func main() { - happy := true - - if happy == true { - fmt.Println("cool!") - } -} -``` -1. `happy != false` -2. `!happy == false` -3. `happy` -4. `!happy == true` - -> **1, 2:** Right! But you can do it better. -> -> **3:** Perfect! You don't need to compare the value to `true`. `happy` is already true, so it'll print "cool!". -> -> **4:** That won't print anything. `!happy` yields false. - - -## How can you simplify the following code? You only need to change the condition expression, but how? -```go -package main - -import "fmt" - -func main() { - happy := false - - if happy == !true { - fmt.Println("why not?") - } -} -``` -1. Easy! Like this: `happy != true` -2. `!happy` -3. `happy == false` -4. `!happy == false` - -> **1, 3:** Right! But you can do it better. -> -> **2:** Perfect! You don't need to compare the value to `false` or to `!true` (which is false). `!happy` already returns true, because it's false at the beginning. -> -> **4:** That won't print anything. `happy` will be true. - - -## This code contains an error. How to fix it? -```go -package main - -import "fmt" - -func main() { - happy := false - - if happy { - fmt.Println("cool!") - } else if !happy { - fmt.Println("why not?") - } else { - fmt.Println("why not?") - } else { - fmt.Println("why not?") - } -} -``` -1. Remove one of the else branches. -2. Move the else if as the last branch. -3. It repeats "why not?" several times. -4. Remove the `else if` branch. - -> **1:** Right. There can be only one else branch. -> -> **2:** If there's an else branch, you can't move else if branch as the last branch. -> -> **3, 4:** So? :) That's not the cause of the problem. -> - - -## What's the problem with the following code? -```go -package main - -import "fmt" - -func main() { - happy := true - energic := happy - - if happy { - fmt.Println("cool!") - } else if !happy { - fmt.Println("why not?") - } else if energic { - fmt.Println("working out?") - } -} -``` -1. It declares the energic variable unnecessarily. -2. You can't use more than one else if branch. -3. It will never run the last else if branch. -4. There's no else branch. - -> **2:** Well, actually you can. -> -> **3:** Right. `happy` can only be either true or false. That means, it will always execute the first two branches, but it will never execute the else if branch. -> -> **4:** It doesn't have to be. Else branch is optional. - - -## How can you simplify the following code? -```go -package main - -import "fmt" - -func main() { - happy := false - - if happy { - fmt.Println("cool!") - } else if happy != true { - fmt.Println("why not?") - } else { - fmt.Println("why not?") - } -} -``` -1. Change `else if`'s condition to: `!happy`. -2. Move the else branch before else if. -3. Remove the else branch. -4. Remove the else if branch. - -> **1, 3:** Close! But, you can do it even better. -> -> **2:** You can't: `else` branch should be the last branch. -> -> **4:** Cool. That's not necessary because `else` branch already handless "unhappy" situation. It's simpler because it doesn't have a condition. \ No newline at end of file diff --git a/collections/learngo/11-if/questions/4-error-handling.md b/collections/learngo/11-if/questions/4-error-handling.md index 229a4f4..51d5560 100644 --- a/collections/learngo/11-if/questions/4-error-handling.md +++ b/collections/learngo/11-if/questions/4-error-handling.md @@ -2,7 +2,7 @@ 1. I don't know. 2. To control the execution flow. 3. To make a program malware safe. -4. Because, things can go wrong. +4. Because, things can go wrong. `CORRECT` > **1:** Then, please watch the lecture again! :) > @@ -13,12 +13,12 @@ ## What's a nil value? 1. The dark matter that rules the universe. -2. It's a zero value for pointers or pointer-based types. It means the value is uninitialized. +2. It's a zero value for pointers or pointer-based types. It means the value is uninitialized. `CORRECT` 3. It's equal to empty string: `"" == nil` is true. ## What's an error value? -1. It stores the error details +1. It stores the error details `CORRECT` 2. A global variable which stores the error status. 3. A global constant which stores the error status. @@ -28,7 +28,7 @@ ## How Go handles error handling? 1. Using a throw/catch statement -2. Using a simple if statement with nil comparison +2. Using a simple if statement with nil comparison `CORRECT` 3. Using a mechanism called tagging > **1:** There isn't a throw/catch statement in Go; unlike Java, C#, and so on... Go is explicit. @@ -37,7 +37,7 @@ ## When you should handle the errors? 1. After the main func ends. 2. Before calling a function. -3. Immediately, after calling a function which returns an error value. +3. Immediately, after calling a function which returns an error value. `CORRECT` ## For which one of the following functions that you might want to handle the errors? @@ -47,7 +47,7 @@ func Write() error func String() string func Reset() ``` -1. Read and Write +1. Read and Write `CORRECT` 2. String and Reset 3. Read, Write and Reset 4. For neither of them @@ -62,12 +62,12 @@ func Reset() ## Let's say a function returns a nil error value. So, what does that mean? 1. The function call failed. -2. The function call is successful. +2. The function call is successful. `CORRECT` 3. The function call is in an indeterministic state. We can't know. ## Let's say a function returns a non-nil error value. So, what does that mean? -1. The function call failed. +1. The function call failed. `CORRECT` 2. The function call is successful. 3. The function call is in an indeterministic state. We can't know. @@ -99,7 +99,7 @@ func main() { ``` 1. Yes. It prints the parsed duration if it's successful. 2. No. It doesn't check for the errors. -3. No. It prints the duration even when there's an error. +3. No. It prints the duration even when there's an error. `CORRECT` > **1:** Yes, it handles the error; however it does so incorrectly. Something is missing here. Look closely. > @@ -133,7 +133,7 @@ func main() { fmt.Println(d) } ``` -1. Yes. It prints the parsed duration if it's successful. +1. Yes. It prints the parsed duration if it's successful. `CORRECT` 2. No. It doesn't check for the errors. 3. No. It prints the duration even when there's an error. diff --git a/collections/learngo/11-if/questions/5-short-if.md b/collections/learngo/11-if/questions/5-short-if.md index 1a871ea..b625f3f 100644 --- a/collections/learngo/11-if/questions/5-short-if.md +++ b/collections/learngo/11-if/questions/5-short-if.md @@ -13,7 +13,7 @@ func main() { } } ``` -1. Swap the simple statement with the `err != nil` check. +1. Swap the simple statement with the `err != nil` check. `CORRECT` 2. Remove the error handling. 3. Remove the semicolon. 4. Change the short declaration to an assignment. @@ -38,7 +38,7 @@ func main() { ``` 1. true and true 2. false and false -3. true and false +3. true and false `CORRECT` 4. false and true > **3:** Yes. It shadows the main()'s done variable, and inside the if statement, it prints "true". Then, after the if statement ends, it prints the main()'s done variable which is "false". @@ -73,7 +73,7 @@ _See the code also in here: https://play.golang.org/p/fDrmcXWGnQB_ 1. Remove the first declaration (main()'s n variable). 2. Remove the declaration in the short-if (if's n). 3. Declare an error variable outside of the main. -4. Declare an error variable along with main's n variable, then change the short-if declaration to an assignment. +4. Declare an error variable along with main's n variable, then change the short-if declaration to an assignment. `CORRECT` > **1:** That will break the program. Because, the last line prints it. >