From cde4e6632cada64677a106da231064417e8d15ce Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Sat, 13 Oct 2018 23:30:21 +0300 Subject: [PATCH] Initial commit --- .gitignore | 16 ++ .../exercises/01/main.go | 27 ++ .../exercises/01/solution/main.go | 20 ++ .../exercises/02/exercise.md | 7 + .../exercises/02/solution/solution.md | 3 + .../exercises/all-exercises.md | 39 +++ 02-write-your-first-program/main.go | 61 +++++ .../questions/01-gopath-questions.md | 19 ++ .../02-code-your-first-program-questions.md | 151 ++++++++++++ .../03-run-your-first-program-questions.md | 43 ++++ 03-packages-and-scopes/01-packages/bye.go | 14 ++ 03-packages-and-scopes/01-packages/hey.go | 14 ++ 03-packages-and-scopes/01-packages/main.go | 25 ++ .../02-scopes/01-scopes/main.go | 24 ++ .../02-scopes/02-block-scope/main.go | 26 ++ .../02-scopes/03-nested-scope/main.go | 39 +++ .../02-scopes/04-package-scope/bye.go | 14 ++ .../02-scopes/04-package-scope/hey.go | 14 ++ .../02-scopes/04-package-scope/main.go | 38 +++ .../03-importing/01-file-scope/bye.go | 18 ++ .../03-importing/01-file-scope/main.go | 14 ++ .../03-importing/02-renaming/main.go | 16 ++ .../exercises/01-packages/main.go | 30 +++ .../exercises/01-packages/solution/bye.go | 14 ++ .../exercises/01-packages/solution/greet.go | 14 ++ .../exercises/01-packages/solution/main.go | 13 + .../exercises/02-scopes/main.go | 33 +++ .../exercises/02-scopes/solution/main.go | 38 +++ .../exercises/02-scopes/solution/printer.go | 19 ++ .../exercises/03-importing/main.go | 30 +++ .../exercises/03-importing/solution/main.go | 18 ++ .../questions/01-packages-A.md | 44 ++++ .../questions/01-packages-B.md | 40 +++ 03-packages-and-scopes/questions/02-scopes.md | 117 +++++++++ .../01-statements/01-execution-flow/main.go | 22 ++ .../01-statements/02-semicolons/main.go | 16 ++ .../02-expressions/01-operator/main.go | 16 ++ .../02-expressions/02-call-expression/main.go | 18 ++ .../03-comments/main.go | 21 ++ .../exercises/01/main.go | 19 ++ .../exercises/01/solution/main.go | 21 ++ .../exercises/02/main.go | 19 ++ .../exercises/02/solution/main.go | 20 ++ .../exercises/03/main.go | 21 ++ .../exercises/03/solution/main.go | 16 ++ .../exercises/04/main.go | 25 ++ .../exercises/04/solution/main.go | 17 ++ .../exercises/05/main.go | 25 ++ .../exercises/05/solution/main.go | 17 ++ .../exercises/06/exercise.md | 10 + .../exercises/06/solution/solution.md | 7 + .../questions/01-statements.md | 93 +++++++ .../questions/02-expressions.md | 1 + .../questions/03-comments.md | 68 +++++ .../printer-exercise/exercise.md | 18 ++ .../printer-exercise/questions.md | 90 +++++++ .../solution/golang/cmd/main.go | 18 ++ .../printer-exercise/solution/golang/go.go | 17 ++ .../printer/cmd/main.go | 15 ++ .../printer/printer.go | 15 ++ .../01-basic-data-types/exercises/01/main.go | 21 ++ .../exercises/01/solution/main.go | 18 ++ .../01-basic-data-types/exercises/02/main.go | 61 +++++ .../exercises/02/solution/main.go | 19 ++ 06-variables/01-basic-data-types/main.go | 37 +++ .../questions/questions.md | 33 +++ .../01-declaration-syntax/01-syntax/main.go | 16 ++ .../02-naming-rules/main.go | 25 ++ .../03-order-of-declaration/main.go | 13 + .../02-example-declarations/01-int/main.go | 22 ++ .../02-float64/main.go | 22 ++ .../02-example-declarations/03-bool/main.go | 22 ++ .../02-example-declarations/04-string/main.go | 22 ++ .../02-declarations/03-zero-values/main.go | 27 ++ .../01-unused-variable/main.go | 19 ++ .../02-blank-identifier/main.go | 16 ++ .../01-multiple/main.go | 25 ++ .../02-parallel/main.go | 28 +++ .../02-declarations/06-examples/main.go | 41 +++ .../02-declarations/exercises/01/main.go | 22 ++ .../exercises/01/solution/main.go | 18 ++ .../02-declarations/exercises/02/main.go | 22 ++ .../exercises/02/solution/main.go | 17 ++ .../02-declarations/exercises/03/main.go | 22 ++ .../exercises/03/solution/main.go | 18 ++ .../02-declarations/exercises/04/main.go | 28 +++ .../exercises/04/solution/main.go | 15 ++ .../02-declarations/exercises/05/main.go | 33 +++ .../exercises/05/solution/main.go | 17 ++ .../02-declarations/exercises/06/main.go | 41 +++ .../exercises/06/solution/main.go | 40 +++ .../02-declarations/exercises/07/main.go | 32 +++ .../exercises/07/solution/main.go | 18 ++ .../02-declarations/exercises/08/main.go | 34 +++ .../exercises/08/solution/main.go | 15 ++ .../02-declarations/exercises/09/main.go | 21 ++ .../exercises/09/solution/main.go | 13 + .../02-declarations/exercises/10/main.go | 19 ++ .../exercises/10/solution/main.go | 13 + .../02-declarations/exercises/11/main.go | 24 ++ .../exercises/11/solution/main.go | 15 ++ .../02-declarations/questions/01-what.md | 22 ++ .../questions/02-declaration.md | 21 ++ .../questions/03-unused-variables.md | 14 ++ .../questions/04-zero-values.md | 23 ++ .../01-initialization/main.go | 26 ++ .../02-short-declaration/main.go | 34 +++ .../03-coding-example/main.go | 39 +++ .../02-package-scope/main.go | 27 ++ .../01-declaration/main.go | 21 ++ .../02-coding-example/main.go | 32 +++ .../03-redeclaration/01/main.go | 35 +++ .../02-coding-example/main.go | 41 +++ .../04-short-vs-normal/01-declaration/main.go | 41 +++ .../02-short-declaration/main.go | 34 +++ .../03-short-declaration/exercises/01/main.go | 31 +++ .../exercises/01/solution/main.go | 26 ++ .../03-short-declaration/exercises/02/main.go | 25 ++ .../exercises/02/solution/main.go | 18 ++ .../03-short-declaration/exercises/03/main.go | 28 +++ .../exercises/03/solution/main.go | 18 ++ .../03-short-declaration/exercises/04/main.go | 25 ++ .../exercises/04/solution/main.go | 18 ++ .../03-short-declaration/exercises/05/main.go | 34 +++ .../exercises/05/solution/main.go | 20 ++ .../03-short-declaration/exercises/06/main.go | 33 +++ .../exercises/06/solution/main.go | 19 ++ .../questions/questions.md | 101 ++++++++ .../01-assignment/01-assignment/main.go | 21 ++ .../01-assignment/02-strongly-typed/main.go | 34 +++ .../01-assignment/03-examples/main.go | 50 ++++ .../04-assignment/01-overview/main.go | 26 ++ .../05-multiple-assignment/main.go | 29 +++ .../04-assignment/06-swapping/main.go | 21 ++ .../04-assignment/07-path-project/main.go | 22 ++ .../08-path-project-discarding/main.go | 21 ++ .../09-path-project-shortdecl/main.go | 22 ++ .../04-assignment/exercises/01/main.go | 28 +++ .../exercises/01/solution/main.go | 18 ++ .../04-assignment/exercises/02/main.go | 47 ++++ .../exercises/02/solution/main.go | 20 ++ .../04-assignment/exercises/03/main.go | 38 +++ .../exercises/03/solution/main.go | 18 ++ .../04-assignment/exercises/04/main.go | 38 +++ .../exercises/04/solution/main.go | 26 ++ .../04-assignment/exercises/05/main.go | 35 +++ .../exercises/05/solution/main.go | 21 ++ .../04-assignment/exercises/06/main.go | 36 +++ .../exercises/06/solution/main.go | 24 ++ .../04-assignment/exercises/07/main.go | 40 +++ .../exercises/07/solution/main.go | 22 ++ .../04-assignment/exercises/08/main.go | 27 ++ .../exercises/08/solution/main.go | 18 ++ .../04-assignment/exercises/09/main.go | 23 ++ .../exercises/09/solution/main.go | 18 ++ .../04-assignment/exercises/10/main.go | 25 ++ .../exercises/10/solution/main.go | 19 ++ .../04-assignment/questions/questions.md | 66 +++++ .../05-type-conversion/01-destructive/main.go | 25 ++ .../05-type-conversion/02-correct/main.go | 31 +++ .../03-numeric-conversion/main.go | 44 ++++ .../05-type-conversion/exercises/01/main.go | 21 ++ .../exercises/01/solution/main.go | 15 ++ .../05-type-conversion/exercises/02/main.go | 22 ++ .../exercises/02/solution/main.go | 16 ++ .../05-type-conversion/exercises/03/main.go | 20 ++ .../exercises/03/solution/main.go | 14 ++ .../05-type-conversion/exercises/04/main.go | 21 ++ .../exercises/04/solution/main.go | 15 ++ .../05-type-conversion/exercises/05/main.go | 31 +++ .../exercises/05/solution/main.go | 30 +++ .../05-type-conversion/questions/questions.md | 63 +++++ .../01-demonstration/main.go | 32 +++ .../06-project-greeter/02-version1/main.go | 35 +++ .../06-project-greeter/03-version2/main.go | 33 +++ .../06-project-greeter/exercises/01/main.go | 27 ++ .../exercises/01/solution/main.go | 19 ++ .../06-project-greeter/exercises/02/main.go | 24 ++ .../exercises/02/solution/main.go | 28 +++ .../06-project-greeter/exercises/03/main.go | 34 +++ .../exercises/03/solution/main.go | 21 ++ .../06-project-greeter/exercises/04/main.go | 34 +++ .../exercises/04/solution/main.go | 28 +++ .../06-project-greeter/exercises/05/main.go | 34 +++ .../exercises/05/solution/main.go | 23 ++ .../solution-to-the-lecture-exercise/main.go | 41 +++ .../06-project-greeter/questions/questions.md | 54 ++++ .../01-intro/01-println-vs-printf/main.go | 23 ++ 07-printf/01-intro/02/main.go | 20 ++ 07-printf/02-escape-sequences/main.go | 25 ++ 07-printf/03-printing-types/main.go | 25 ++ 07-printf/04-coding/main.go | 51 ++++ 07-printf/exercises/01/main.go | 23 ++ 07-printf/exercises/01/solution/main.go | 14 ++ 07-printf/exercises/02/main.go | 27 ++ 07-printf/exercises/02/solution/main.go | 18 ++ 07-printf/exercises/03/main.go | 25 ++ 07-printf/exercises/03/solution/main.go | 15 ++ 07-printf/exercises/04/main.go | 24 ++ 07-printf/exercises/04/solution/main.go | 14 ++ 07-printf/exercises/05/main.go | 24 ++ 07-printf/exercises/05/solution/main.go | 14 ++ 07-printf/exercises/06/main.go | 20 ++ 07-printf/exercises/06/solution/main.go | 14 ++ 07-printf/exercises/07/main.go | 20 ++ 07-printf/exercises/07/solution/main.go | 14 ++ 07-printf/exercises/08/main.go | 20 ++ 07-printf/exercises/08/solution/main.go | 14 ++ 07-printf/exercises/09/main.go | 20 ++ 07-printf/exercises/09/solution/main.go | 14 ++ 07-printf/exercises/10/main.go | 24 ++ 07-printf/exercises/10/solution/main.go | 23 ++ 07-printf/printf cheatsheet.pdf | Bin 0 -> 40040 bytes 07-printf/questions/questions.md | 90 +++++++ .../01-arithmetic-operators/01/main.go | 33 +++ .../01-arithmetic-operators/02/main.go | 22 ++ .../03-float-inaccuracy/01/main.go | 25 ++ .../03-float-inaccuracy/02/main.go | 25 ++ .../03-float-inaccuracy/03/main.go | 27 ++ .../01/main.go | 30 +++ .../02/main.go | 33 +++ .../01-numbers/03-precedence/01/main.go | 27 ++ .../01-numbers/03-precedence/02/main.go | 20 ++ .../01-numbers/03-precedence/03/main.go | 30 +++ .../01-numbers/03-precedence/04/main.go | 20 ++ .../01-numbers/04-incdec-statement/01/main.go | 23 ++ .../01-numbers/04-incdec-statement/02/main.go | 23 ++ .../01-numbers/04-incdec-statement/03/main.go | 30 +++ .../05-assignment-operations/main.go | 41 +++ .../06-project-feet-to-meters/main.go | 29 +++ .../01-numbers/exercises/01/main.go | 34 +++ .../01-numbers/exercises/01/solution/main.go | 19 ++ .../01-numbers/exercises/02/main.go | 23 ++ .../01-numbers/exercises/02/solution/main.go | 20 ++ .../01-numbers/exercises/03/main.go | 38 +++ .../01-numbers/exercises/03/solution/main.go | 30 +++ .../01-numbers/exercises/04/main.go | 32 +++ .../01-numbers/exercises/04/solution/main.go | 25 ++ .../01-numbers/exercises/05/main.go | 41 +++ .../01-numbers/exercises/05/solution/main.go | 23 ++ .../01-numbers/exercises/06/main.go | 35 +++ .../01-numbers/exercises/06/solution/main.go | 24 ++ .../01-numbers/exercises/07-capstone1/main.go | 45 ++++ .../exercises/07-capstone1/solution/main.go | 29 +++ .../01-numbers/exercises/08-capstone2/main.go | 41 +++ .../exercises/08-capstone2/solution/main.go | 26 ++ .../01-numbers/exercises/09-capstone3/main.go | 38 +++ .../exercises/09-capstone3/solution/main.go | 25 ++ .../questions/01-arithmetic-operators.md | 95 +++++++ .../01-numbers/questions/02-precedence.md | 44 ++++ .../questions/03-assignment-operations.md | 98 ++++++++ .../02-strings/01-raw-string-literal/main.go | 37 +++ .../02-strings/02-concatenation/01/main.go | 16 ++ .../02-assignment-operation/main.go | 22 ++ .../03-concat-non-strings/main.go | 51 ++++ .../03-string-length/01-len/main.go | 18 ++ .../03-string-length/02-unicode-len/main.go | 38 +++ .../exercise-solution/main.go | 33 +++ .../02-strings/04-project-banger/main.go | 26 ++ .../02-strings/exercises/01/main.go | 34 +++ .../02-strings/exercises/01/solution/main.go | 22 ++ .../02-strings/exercises/02/main.go | 42 ++++ .../02-strings/exercises/02/solution/main.go | 28 +++ .../02-strings/exercises/03/main.go | 44 ++++ .../02-strings/exercises/03/solution/main.go | 22 ++ .../02-strings/exercises/04/main.go | 36 +++ .../02-strings/exercises/04/solution/main.go | 19 ++ .../02-strings/exercises/05/main.go | 34 +++ .../02-strings/exercises/05/solution/main.go | 24 ++ .../02-strings/exercises/06/main.go | 25 ++ .../02-strings/exercises/06/solution/main.go | 18 ++ .../02-strings/exercises/07/main.go | 37 +++ .../02-strings/exercises/07/solution/main.go | 26 ++ .../02-strings/exercises/08/main.go | 35 +++ .../02-strings/exercises/08/solution/main.go | 23 ++ .../02-strings/questions/questions.md | 194 +++++++++++++++ 09-go-type-system/01-bits/main.go | 46 ++++ 09-go-type-system/02-bytes/main.go | 25 ++ .../03-predeclared-types/main.go | 59 +++++ .../04-overflow/01-problem/main.go | 25 ++ .../04-overflow/02-explain/main.go | 45 ++++ .../04-overflow/03-destructive/main.go | 27 ++ .../01-duration-example/main.go | 35 +++ .../main.go | 54 ++++ .../03-underlying-types/main.go | 72 ++++++ .../03-underlying-types/weights/weights.go | 19 ++ 09-go-type-system/06-aliased-types/main.go | 39 +++ 09-go-type-system/exercises/01/main.go | 49 ++++ .../exercises/01/solution/main.go | 41 +++ 09-go-type-system/exercises/02/main.go | 39 +++ .../exercises/02/solution/main.go | 23 ++ 09-go-type-system/exercises/03/main.go | 78 ++++++ .../exercises/03/solution/main.go | 44 ++++ 09-go-type-system/exercises/04/main.go | 51 ++++ .../exercises/04/solution/main.go | 32 +++ 09-go-type-system/exercises/05/main.go | 47 ++++ .../exercises/05/solution/main.go | 41 +++ 09-go-type-system/exercises/06/main.go | 51 ++++ .../exercises/06/solution/main.go | 31 +++ .../01-questions-predeclared-types.md | 119 +++++++++ .../questions/02-questions-defined-types.md | 116 +++++++++ .../01-syntax/01-magic-numbers/main.go | 22 ++ .../01-syntax/02-constants/main.go | 25 ++ .../01-syntax/03-safety/main.go | 26 ++ .../04-rules/01-immutability/main.go | 16 ++ .../04-rules/02-runtime-func/main.go | 25 ++ .../01-syntax/04-rules/03-runtime-var/main.go | 17 ++ .../01-syntax/04-rules/04-len/main.go | 21 ++ .../01/main.go | 20 ++ .../02/main.go | 20 ++ .../03/main.go | 20 ++ .../03-multiple-declaration/01/main.go | 26 ++ .../03-multiple-declaration/02/main.go | 23 ++ .../03-multiple-declaration/03/main.go | 23 ++ .../01-typeless-constants/main.go | 18 ++ .../02-typed-vs-typeless/01/main.go | 19 ++ .../02-typed-vs-typeless/02/main.go | 21 ++ .../02-typed-vs-typeless/03/main.go | 20 ++ .../02-typed-vs-typeless/04/main.go | 31 +++ .../03-default-type/01/main.go | 20 ++ .../03-default-type/02/main.go | 20 ++ .../03-default-type/03/main.go | 26 ++ .../03-default-type/04/main.go | 19 ++ .../03-default-type/05/main.go | 15 ++ .../02-typeless-constants/04-demo/01/main.go | 45 ++++ .../02-typeless-constants/04-demo/02/main.go | 41 +++ .../03-refactor-feet-to-meters/main.go | 31 +++ .../solution/main.go | 52 ++++ .../solution/without-comments/main.go | 35 +++ 10-constants/04-iota/01-manually/main.go | 25 ++ 10-constants/04-iota/02-with-iota/main.go | 25 ++ 10-constants/04-iota/03-expressions/main.go | 25 ++ .../04-iota/04-blank-identifier/01/main.go | 20 ++ .../04-iota/04-blank-identifier/02/main.go | 20 ++ .../04-iota/04-blank-identifier/03/main.go | 21 ++ 10-constants/exercises/01/main.go | 29 +++ 10-constants/exercises/01/solution/main.go | 20 ++ 10-constants/exercises/02/main.go | 30 +++ 10-constants/exercises/02/solution/main.go | 21 ++ 10-constants/exercises/03/main.go | 34 +++ 10-constants/exercises/03/solution/main.go | 20 ++ 10-constants/exercises/04/main.go | 26 ++ 10-constants/exercises/04/solution/main.go | 19 ++ 10-constants/exercises/05/main.go | 28 +++ 10-constants/exercises/05/solution/main.go | 20 ++ 10-constants/exercises/06/main.go | 25 ++ 10-constants/exercises/06/solution/main.go | 28 +++ 10-constants/exercises/07/main.go | 34 +++ 10-constants/exercises/07/solution/main.go | 20 ++ 10-constants/exercises/08/main.go | 38 +++ 10-constants/exercises/08/solution/main.go | 21 ++ 10-constants/exercises/09/main.go | 34 +++ 10-constants/exercises/09/solution/main.go | 21 ++ 10-constants/questions/questions.md | 104 ++++++++ .../01-comparison-operators/main.go | 26 ++ .../01/main.go | 21 ++ .../02/main.go | 21 ++ .../03/main.go | 48 ++++ .../01-and-operator/01/main.go | 20 ++ .../01-and-operator/02/main.go | 34 +++ .../02-or-operator/01/main.go | 20 ++ .../02-or-operator/02/main.go | 31 +++ .../03-not-operator/01/main.go | 18 ++ .../03-not-operator/02/main.go | 22 ++ 11-if/02-if-statement/01-if-branch/main.go | 18 ++ 11-if/02-if-statement/02-else-branch/main.go | 20 ++ .../03-else-if-branch/01/main.go | 22 ++ .../03-else-if-branch/02/main.go | 24 ++ .../04-refactor-feet-to-meters/main.go | 42 ++++ .../01-1st-challenge/01-challenge/main.go | 36 +++ .../01-1st-challenge/02-solution/main.go | 32 +++ .../03-solution-refactor/main.go | 41 +++ .../02-2nd-challenge/01-challenge/main.go | 72 ++++++ .../02-2nd-challenge/02-solution/main.go | 43 ++++ 11-if/03-error-handling/01-itoa/main.go | 22 ++ 11-if/03-error-handling/02-atoi/main.go | 24 ++ .../03-atoi-error-handling/main.go | 44 ++++ .../01-challenge/main.go | 52 ++++ .../02-solution/main.go | 42 ++++ 11-if/04-short-if/01-without-short-if/main.go | 21 ++ 11-if/04-short-if/02-with-short-if/main.go | 23 ++ 11-if/04-short-if/03-scope/main.go | 44 ++++ .../04-scope-shadowing/01-shadowing/main.go | 37 +++ .../02-shadowing-solution/main.go | 44 ++++ 11-if/exercises/00/main.go | 22 ++ 11-if/exercises/00/solution/main.go | 11 + 11-if/questions/questions.md | 3 + 12-switch/01-one-case/main.go | 31 +++ 12-switch/02-multiple-cases/main.go | 36 +++ 12-switch/03-default-clause/main.go | 37 +++ 12-switch/04-multiple-conditions/main.go | 30 +++ 12-switch/05-bool-expressions/main.go | 23 ++ 12-switch/06-fallthrough/01-without/main.go | 25 ++ 12-switch/06-fallthrough/02-with/main.go | 27 ++ 12-switch/07-short-switch/main.go | 26 ++ 12-switch/08-parts-of-the-day/main.go | 36 +++ 12-switch/09-when-to-use/main.go | 47 ++++ 12-switch/exercises/00/main.go | 22 ++ 12-switch/exercises/00/solution/main.go | 11 + .../exercises/_challenge/01-challenge/main.go | 75 ++++++ .../exercises/_challenge/02-solution/main.go | 44 ++++ 12-switch/questions/questions.md | 3 + 13-loops/01-loops/01-basics/main.go | 28 +++ 13-loops/01-loops/02-break/main.go | 33 +++ .../01-loops/03-continue/01-a-before/main.go | 39 +++ .../01-loops/03-continue/01-b-after/main.go | 39 +++ 13-loops/01-loops/04-nested-loops/main.go | 37 +++ .../05-for-range/01-loop-over-slices/main.go | 54 ++++ .../05-for-range/02-loop-over-words/main.go | 51 ++++ .../01-randomization/main.go | 34 +++ .../02-game/main.go | 63 +++++ 13-loops/03-project-word-finder/main.go | 38 +++ .../01-word-finder-break/main.go | 40 +++ .../02-word-finder-continue/main.go | 33 +++ .../03-word-finder-switch/main.go | 48 ++++ .../04-labeled-statements/04-goto/main.go | 29 +++ .../01-loops/01-sum-the-numbers/main.go | 21 ++ .../01-sum-the-numbers/solution/main.go | 20 ++ .../02-sum-the-numbers-verbose/main.go | 28 +++ .../solution/main.go | 27 ++ .../exercises/01-loops/03-sum-up-to-n/main.go | 38 +++ .../01-loops/03-sum-up-to-n/solution/main.go | 39 +++ .../exercises/01-loops/04-only-evens/main.go | 30 +++ .../01-loops/04-only-evens/solution/main.go | 42 ++++ .../exercises/01-loops/05-break-up/main.go | 30 +++ .../01-loops/05-break-up/solution/main.go | 50 ++++ .../01-loops/06-infinite-kill/main.go | 42 ++++ .../06-infinite-kill/solution/main.go | 31 +++ .../01-loops/07-crunch-the-primes/main.go | 33 +++ .../07-crunch-the-primes/solution/main.go | 54 ++++ .../01-first-turn-winner/main.go | 28 +++ .../01-first-turn-winner/solution/main.go | 66 +++++ .../02-random-messages/main.go | 37 +++ .../02-random-messages/solution/main.go | 77 ++++++ .../03-double-guesses/main.go | 25 ++ .../03-double-guesses/solution/main.go | 76 ++++++ .../04-verbose-mode/main.go | 29 +++ .../04-verbose-mode/solution/main.go | 78 ++++++ .../05-enough-picks/main.go | 48 ++++ .../05-enough-picks/solution/main.go | 68 +++++ .../06-dynamic-difficulty/main.go | 51 ++++ .../06-dynamic-difficulty/solution/main.go | 62 +++++ .../01-case-insensitive/main.go | 27 ++ .../01-case-insensitive/solution/main.go | 40 +++ .../02-path-searcher/main.go | 70 ++++++ .../02-path-searcher/solution/main.go | 36 +++ 13-loops/exercises/more-exercises.md | 2 + 13-loops/questions/questions.md | 3 + 14-arrays/01-what/01-without-arrays/main.go | 28 +++ 14-arrays/01-what/02-with-arrays/main.go | 27 ++ 14-arrays/02-array-literals/01/main.go | 18 ++ .../02-array-literals/02-fixed-size/main.go | 18 ++ .../03-constant-expressions/main.go | 39 +++ .../02-array-literals/04-element-type/main.go | 51 ++++ .../02-array-literals/05-ellipsis/main.go | 20 ++ 14-arrays/03-array-type/main.go | 22 ++ .../01-uninitialized-array/main.go | 16 ++ .../02-partially-initialized-array/main.go | 16 ++ 14-arrays/04-zero-values/03-ellipsis/main.go | 18 ++ .../04-without-ellipsis/main.go | 18 ++ .../04-zero-values/05-more-examples/main.go | 21 ++ .../01-getting-length/main.go | 35 +++ .../02-getting-elements/main.go | 32 +++ .../03-getting-elements/main.go | 49 ++++ .../01-equal-arrays/main.go | 30 +++ .../02-not-equal-arrays/main.go | 39 +++ .../03-comparability/main.go | 24 ++ .../06-comparing-arrays/04-example/main.go | 26 ++ 14-arrays/07-unnamed-vs-named-types/main.go | 38 +++ .../01-assigning-arrays/main.go | 24 ++ .../02-arrays-are-value-types/main.go | 27 ++ .../03-assigning-arrays2/main.go | 23 ++ .../04-cannot-assign-arrays/main.go | 23 ++ .../05-passing-to-funcs/main.go | 31 +++ 14-arrays/09-multi-dimensional-arrays/main.go | 49 ++++ .../10-keyed-elements/01-unkeyed/main.go | 20 ++ 14-arrays/10-keyed-elements/02-keyed/main.go | 28 +++ .../10-keyed-elements/03-keyed-order/main.go | 28 +++ .../04-keyed-auto-initialize/main.go | 28 +++ .../05-keyed-auto-initialize-ellipsis/main.go | 36 +++ .../06-keyed-and-unkeyed/main.go | 34 +++ .../11-project-xratio/01-without-keys/main.go | 21 ++ .../11-project-xratio/02-with-keys/main.go | 30 +++ .../12-random-message/01-1st-version/main.go | 38 +++ .../12-random-message/02-2nd-version/main.go | 39 +++ .../13-refactor-lucky-number-game/main.go | 72 ++++++ 14-arrays/exercises/00/main.go | 22 ++ 14-arrays/exercises/00/solution/main.go | 11 + .../xxx-lucky-number-unique-picker/main.go | 82 ++++++ 14-arrays/questions/questions.md | 3 + 15-slices/01-intro/01-theory/main.go | 82 ++++++ 15-slices/01-intro/02-examples/main.go | 61 +++++ 15-slices/02-append/01-theory/main.go | 46 ++++ 15-slices/02-append/02-examples/main.go | 26 ++ .../01-with-arrays/main.go | 40 +++ .../02-with-slices/main.go | 43 ++++ 15-slices/04-copy/01-usage/main.go | 28 +++ 15-slices/04-copy/02-hacker-incident/main.go | 44 ++++ .../01-theory/main.go | 78 ++++++ .../02-example/main.go | 36 +++ .../06-slice-expressions/01-theory/main.go | 18 ++ .../06-slice-expressions/02-examples/main.go | 56 +++++ 15-slices/07-full-slice-expressions/main.go | 23 ++ 15-slices/08-make/main.go | 31 +++ .../01-challenge/main.go | 181 ++++++++++++++ .../02-solution-draw-the-board/main.go | 47 ++++ .../03-solution-drawing-loop/main.go | 64 +++++ .../04-solution-final/main.go | 87 +++++++ 15-slices/README-WARNING.md | 8 + 16-strings-revisited/01-byte/main.go | 45 ++++ .../02-string-byte-slice/main.go | 28 +++ .../03-string-indexing-slicing/main.go | 57 +++++ .../04-masker-challenge/main.go | 77 ++++++ .../04-masker-challenge/spam.txt | 7 + .../05-masker-solution/main.go | 88 +++++++ .../05-masker-solution/spam.txt | 7 + 16-strings-revisited/06-encoding/main.go | 144 +++++++++++ .../07-encoding-examples/main.go | 60 +++++ .../08-wrapper-example/main.go | 66 +++++ 16-strings-revisited/09-internals/main.go | 45 ++++ 16-strings-revisited/charset-table/main.go | 83 +++++++ 16-strings-revisited/notes.md | 233 ++++++++++++++++++ 17-maps/README.md | 1 + 18-structs/README.md | 1 + 19-functions/README.md | 1 + 20-pointers/README.md | 1 + 21-closures/README.md | 1 + 22-deferred-funcs/README.md | 1 + 23-variadic-funcs/README.md | 1 + 24-methods/README.md | 1 + 25-method-values/README.md | 1 + 26-interfaces/README.md | 1 + 27-embedding/README.md | 1 + 28-concurrency/README.md | 1 + 29-tba/README.md | 1 + README.md | 25 ++ Warming Up Exercises.md | 39 +++ first/README.md | 19 ++ first/explain/comments/main.go | 21 ++ first/explain/expressions/01-operator/main.go | 16 ++ .../expressions/02-call-expression/main.go | 18 ++ first/explain/expressions/README | 3 + first/explain/importing/01-file-scope/main.go | 14 ++ first/explain/importing/02-renaming/main.go | 16 ++ first/explain/packages/scopes/bye.go | 14 ++ first/explain/packages/scopes/hey.go | 14 ++ first/explain/packages/scopes/main.go | 38 +++ first/explain/packages/what/bye.go | 14 ++ first/explain/packages/what/hey.go | 14 ++ first/explain/packages/what/main.go | 25 ++ first/explain/scopes/01-scopes/main.go | 24 ++ first/explain/scopes/02-block-scope/main.go | 26 ++ first/explain/scopes/03-nested-scope/main.go | 39 +++ .../statements/01-execution-flow/main.go | 22 ++ .../explain/statements/02-semicolons/main.go | 16 ++ first/first/exercises/01/main.go | 27 ++ first/first/exercises/01/solution/main.go | 20 ++ first/first/exercises/02/exercise.md | 7 + first/first/exercises/02/solution/solution.md | 3 + first/first/main.go | 59 +++++ first/first/questions/01-gopath-questions.md | 19 ++ .../02-code-your-first-program-questions.md | 151 ++++++++++++ .../03-run-your-first-program-questions.md | 43 ++++ .../solution/golang/cmd/main.go | 20 ++ first/printer-exercise/solution/golang/go.go | 17 ++ first/printer/cmd/main.go | 15 ++ first/printer/printer.go | 15 ++ 567 files changed, 17896 insertions(+) create mode 100644 .gitignore create mode 100644 02-write-your-first-program/exercises/01/main.go create mode 100644 02-write-your-first-program/exercises/01/solution/main.go create mode 100644 02-write-your-first-program/exercises/02/exercise.md create mode 100644 02-write-your-first-program/exercises/02/solution/solution.md create mode 100644 02-write-your-first-program/exercises/all-exercises.md create mode 100644 02-write-your-first-program/main.go create mode 100644 02-write-your-first-program/questions/01-gopath-questions.md create mode 100644 02-write-your-first-program/questions/02-code-your-first-program-questions.md create mode 100644 02-write-your-first-program/questions/03-run-your-first-program-questions.md create mode 100644 03-packages-and-scopes/01-packages/bye.go create mode 100644 03-packages-and-scopes/01-packages/hey.go create mode 100644 03-packages-and-scopes/01-packages/main.go create mode 100644 03-packages-and-scopes/02-scopes/01-scopes/main.go create mode 100644 03-packages-and-scopes/02-scopes/02-block-scope/main.go create mode 100644 03-packages-and-scopes/02-scopes/03-nested-scope/main.go create mode 100644 03-packages-and-scopes/02-scopes/04-package-scope/bye.go create mode 100644 03-packages-and-scopes/02-scopes/04-package-scope/hey.go create mode 100644 03-packages-and-scopes/02-scopes/04-package-scope/main.go create mode 100644 03-packages-and-scopes/03-importing/01-file-scope/bye.go create mode 100644 03-packages-and-scopes/03-importing/01-file-scope/main.go create mode 100644 03-packages-and-scopes/03-importing/02-renaming/main.go create mode 100644 03-packages-and-scopes/exercises/01-packages/main.go create mode 100644 03-packages-and-scopes/exercises/01-packages/solution/bye.go create mode 100644 03-packages-and-scopes/exercises/01-packages/solution/greet.go create mode 100644 03-packages-and-scopes/exercises/01-packages/solution/main.go create mode 100644 03-packages-and-scopes/exercises/02-scopes/main.go create mode 100644 03-packages-and-scopes/exercises/02-scopes/solution/main.go create mode 100644 03-packages-and-scopes/exercises/02-scopes/solution/printer.go create mode 100644 03-packages-and-scopes/exercises/03-importing/main.go create mode 100644 03-packages-and-scopes/exercises/03-importing/solution/main.go create mode 100644 03-packages-and-scopes/questions/01-packages-A.md create mode 100644 03-packages-and-scopes/questions/01-packages-B.md create mode 100644 03-packages-and-scopes/questions/02-scopes.md create mode 100644 04-statements-expressions-comments/01-statements/01-execution-flow/main.go create mode 100644 04-statements-expressions-comments/01-statements/02-semicolons/main.go create mode 100644 04-statements-expressions-comments/02-expressions/01-operator/main.go create mode 100644 04-statements-expressions-comments/02-expressions/02-call-expression/main.go create mode 100644 04-statements-expressions-comments/03-comments/main.go create mode 100644 04-statements-expressions-comments/exercises/01/main.go create mode 100644 04-statements-expressions-comments/exercises/01/solution/main.go create mode 100644 04-statements-expressions-comments/exercises/02/main.go create mode 100644 04-statements-expressions-comments/exercises/02/solution/main.go create mode 100644 04-statements-expressions-comments/exercises/03/main.go create mode 100644 04-statements-expressions-comments/exercises/03/solution/main.go create mode 100644 04-statements-expressions-comments/exercises/04/main.go create mode 100644 04-statements-expressions-comments/exercises/04/solution/main.go create mode 100644 04-statements-expressions-comments/exercises/05/main.go create mode 100644 04-statements-expressions-comments/exercises/05/solution/main.go create mode 100644 04-statements-expressions-comments/exercises/06/exercise.md create mode 100644 04-statements-expressions-comments/exercises/06/solution/solution.md create mode 100644 04-statements-expressions-comments/questions/01-statements.md create mode 100644 04-statements-expressions-comments/questions/02-expressions.md create mode 100644 04-statements-expressions-comments/questions/03-comments.md create mode 100644 05-write-your-first-library-package/printer-exercise/exercise.md create mode 100644 05-write-your-first-library-package/printer-exercise/questions.md create mode 100644 05-write-your-first-library-package/printer-exercise/solution/golang/cmd/main.go create mode 100644 05-write-your-first-library-package/printer-exercise/solution/golang/go.go create mode 100644 05-write-your-first-library-package/printer/cmd/main.go create mode 100644 05-write-your-first-library-package/printer/printer.go create mode 100644 06-variables/01-basic-data-types/exercises/01/main.go create mode 100644 06-variables/01-basic-data-types/exercises/01/solution/main.go create mode 100644 06-variables/01-basic-data-types/exercises/02/main.go create mode 100644 06-variables/01-basic-data-types/exercises/02/solution/main.go create mode 100644 06-variables/01-basic-data-types/main.go create mode 100644 06-variables/01-basic-data-types/questions/questions.md create mode 100644 06-variables/02-declarations/01-declaration-syntax/01-syntax/main.go create mode 100644 06-variables/02-declarations/01-declaration-syntax/02-naming-rules/main.go create mode 100644 06-variables/02-declarations/01-declaration-syntax/03-order-of-declaration/main.go create mode 100644 06-variables/02-declarations/02-example-declarations/01-int/main.go create mode 100644 06-variables/02-declarations/02-example-declarations/02-float64/main.go create mode 100644 06-variables/02-declarations/02-example-declarations/03-bool/main.go create mode 100644 06-variables/02-declarations/02-example-declarations/04-string/main.go create mode 100644 06-variables/02-declarations/03-zero-values/main.go create mode 100644 06-variables/02-declarations/04-unused-variables-and-blank-identifier/01-unused-variable/main.go create mode 100644 06-variables/02-declarations/04-unused-variables-and-blank-identifier/02-blank-identifier/main.go create mode 100644 06-variables/02-declarations/05-multiple-declarations/01-multiple/main.go create mode 100644 06-variables/02-declarations/05-multiple-declarations/02-parallel/main.go create mode 100644 06-variables/02-declarations/06-examples/main.go create mode 100644 06-variables/02-declarations/exercises/01/main.go create mode 100644 06-variables/02-declarations/exercises/01/solution/main.go create mode 100644 06-variables/02-declarations/exercises/02/main.go create mode 100644 06-variables/02-declarations/exercises/02/solution/main.go create mode 100644 06-variables/02-declarations/exercises/03/main.go create mode 100644 06-variables/02-declarations/exercises/03/solution/main.go create mode 100644 06-variables/02-declarations/exercises/04/main.go create mode 100644 06-variables/02-declarations/exercises/04/solution/main.go create mode 100644 06-variables/02-declarations/exercises/05/main.go create mode 100644 06-variables/02-declarations/exercises/05/solution/main.go create mode 100644 06-variables/02-declarations/exercises/06/main.go create mode 100644 06-variables/02-declarations/exercises/06/solution/main.go create mode 100644 06-variables/02-declarations/exercises/07/main.go create mode 100644 06-variables/02-declarations/exercises/07/solution/main.go create mode 100644 06-variables/02-declarations/exercises/08/main.go create mode 100644 06-variables/02-declarations/exercises/08/solution/main.go create mode 100644 06-variables/02-declarations/exercises/09/main.go create mode 100644 06-variables/02-declarations/exercises/09/solution/main.go create mode 100644 06-variables/02-declarations/exercises/10/main.go create mode 100644 06-variables/02-declarations/exercises/10/solution/main.go create mode 100644 06-variables/02-declarations/exercises/11/main.go create mode 100644 06-variables/02-declarations/exercises/11/solution/main.go create mode 100644 06-variables/02-declarations/questions/01-what.md create mode 100644 06-variables/02-declarations/questions/02-declaration.md create mode 100644 06-variables/02-declarations/questions/03-unused-variables.md create mode 100644 06-variables/02-declarations/questions/04-zero-values.md create mode 100644 06-variables/03-short-declaration/01-initialization-and-short-declaration/01-initialization/main.go create mode 100644 06-variables/03-short-declaration/01-initialization-and-short-declaration/02-short-declaration/main.go create mode 100644 06-variables/03-short-declaration/01-initialization-and-short-declaration/03-coding-example/main.go create mode 100644 06-variables/03-short-declaration/02-package-scope/main.go create mode 100644 06-variables/03-short-declaration/03-multiple-short-declaration/01-declaration/main.go create mode 100644 06-variables/03-short-declaration/03-multiple-short-declaration/02-coding-example/main.go create mode 100644 06-variables/03-short-declaration/03-multiple-short-declaration/03-redeclaration/01/main.go create mode 100644 06-variables/03-short-declaration/03-multiple-short-declaration/03-redeclaration/02-coding-example/main.go create mode 100644 06-variables/03-short-declaration/04-short-vs-normal/01-declaration/main.go create mode 100644 06-variables/03-short-declaration/04-short-vs-normal/02-short-declaration/main.go create mode 100644 06-variables/03-short-declaration/exercises/01/main.go create mode 100644 06-variables/03-short-declaration/exercises/01/solution/main.go create mode 100644 06-variables/03-short-declaration/exercises/02/main.go create mode 100644 06-variables/03-short-declaration/exercises/02/solution/main.go create mode 100644 06-variables/03-short-declaration/exercises/03/main.go create mode 100644 06-variables/03-short-declaration/exercises/03/solution/main.go create mode 100644 06-variables/03-short-declaration/exercises/04/main.go create mode 100644 06-variables/03-short-declaration/exercises/04/solution/main.go create mode 100644 06-variables/03-short-declaration/exercises/05/main.go create mode 100644 06-variables/03-short-declaration/exercises/05/solution/main.go create mode 100644 06-variables/03-short-declaration/exercises/06/main.go create mode 100644 06-variables/03-short-declaration/exercises/06/solution/main.go create mode 100644 06-variables/03-short-declaration/questions/questions.md create mode 100644 06-variables/04-assignment/01-assignment/01-assignment/main.go create mode 100644 06-variables/04-assignment/01-assignment/02-strongly-typed/main.go create mode 100644 06-variables/04-assignment/01-assignment/03-examples/main.go create mode 100644 06-variables/04-assignment/01-overview/main.go create mode 100644 06-variables/04-assignment/05-multiple-assignment/main.go create mode 100644 06-variables/04-assignment/06-swapping/main.go create mode 100644 06-variables/04-assignment/07-path-project/main.go create mode 100644 06-variables/04-assignment/08-path-project-discarding/main.go create mode 100644 06-variables/04-assignment/09-path-project-shortdecl/main.go create mode 100644 06-variables/04-assignment/exercises/01/main.go create mode 100644 06-variables/04-assignment/exercises/01/solution/main.go create mode 100644 06-variables/04-assignment/exercises/02/main.go create mode 100644 06-variables/04-assignment/exercises/02/solution/main.go create mode 100644 06-variables/04-assignment/exercises/03/main.go create mode 100644 06-variables/04-assignment/exercises/03/solution/main.go create mode 100644 06-variables/04-assignment/exercises/04/main.go create mode 100644 06-variables/04-assignment/exercises/04/solution/main.go create mode 100644 06-variables/04-assignment/exercises/05/main.go create mode 100644 06-variables/04-assignment/exercises/05/solution/main.go create mode 100644 06-variables/04-assignment/exercises/06/main.go create mode 100644 06-variables/04-assignment/exercises/06/solution/main.go create mode 100644 06-variables/04-assignment/exercises/07/main.go create mode 100644 06-variables/04-assignment/exercises/07/solution/main.go create mode 100644 06-variables/04-assignment/exercises/08/main.go create mode 100644 06-variables/04-assignment/exercises/08/solution/main.go create mode 100644 06-variables/04-assignment/exercises/09/main.go create mode 100644 06-variables/04-assignment/exercises/09/solution/main.go create mode 100644 06-variables/04-assignment/exercises/10/main.go create mode 100644 06-variables/04-assignment/exercises/10/solution/main.go create mode 100644 06-variables/04-assignment/questions/questions.md create mode 100644 06-variables/05-type-conversion/01-destructive/main.go create mode 100644 06-variables/05-type-conversion/02-correct/main.go create mode 100644 06-variables/05-type-conversion/03-numeric-conversion/main.go create mode 100644 06-variables/05-type-conversion/exercises/01/main.go create mode 100644 06-variables/05-type-conversion/exercises/01/solution/main.go create mode 100644 06-variables/05-type-conversion/exercises/02/main.go create mode 100644 06-variables/05-type-conversion/exercises/02/solution/main.go create mode 100644 06-variables/05-type-conversion/exercises/03/main.go create mode 100644 06-variables/05-type-conversion/exercises/03/solution/main.go create mode 100644 06-variables/05-type-conversion/exercises/04/main.go create mode 100644 06-variables/05-type-conversion/exercises/04/solution/main.go create mode 100644 06-variables/05-type-conversion/exercises/05/main.go create mode 100644 06-variables/05-type-conversion/exercises/05/solution/main.go create mode 100644 06-variables/05-type-conversion/questions/questions.md create mode 100644 06-variables/06-project-greeter/01-demonstration/main.go create mode 100644 06-variables/06-project-greeter/02-version1/main.go create mode 100644 06-variables/06-project-greeter/03-version2/main.go create mode 100644 06-variables/06-project-greeter/exercises/01/main.go create mode 100644 06-variables/06-project-greeter/exercises/01/solution/main.go create mode 100644 06-variables/06-project-greeter/exercises/02/main.go create mode 100644 06-variables/06-project-greeter/exercises/02/solution/main.go create mode 100644 06-variables/06-project-greeter/exercises/03/main.go create mode 100644 06-variables/06-project-greeter/exercises/03/solution/main.go create mode 100644 06-variables/06-project-greeter/exercises/04/main.go create mode 100644 06-variables/06-project-greeter/exercises/04/solution/main.go create mode 100644 06-variables/06-project-greeter/exercises/05/main.go create mode 100644 06-variables/06-project-greeter/exercises/05/solution/main.go create mode 100644 06-variables/06-project-greeter/exercises/solution-to-the-lecture-exercise/main.go create mode 100644 06-variables/06-project-greeter/questions/questions.md create mode 100644 07-printf/01-intro/01-println-vs-printf/main.go create mode 100644 07-printf/01-intro/02/main.go create mode 100644 07-printf/02-escape-sequences/main.go create mode 100644 07-printf/03-printing-types/main.go create mode 100644 07-printf/04-coding/main.go create mode 100644 07-printf/exercises/01/main.go create mode 100644 07-printf/exercises/01/solution/main.go create mode 100644 07-printf/exercises/02/main.go create mode 100644 07-printf/exercises/02/solution/main.go create mode 100644 07-printf/exercises/03/main.go create mode 100644 07-printf/exercises/03/solution/main.go create mode 100644 07-printf/exercises/04/main.go create mode 100644 07-printf/exercises/04/solution/main.go create mode 100644 07-printf/exercises/05/main.go create mode 100644 07-printf/exercises/05/solution/main.go create mode 100644 07-printf/exercises/06/main.go create mode 100644 07-printf/exercises/06/solution/main.go create mode 100644 07-printf/exercises/07/main.go create mode 100644 07-printf/exercises/07/solution/main.go create mode 100644 07-printf/exercises/08/main.go create mode 100644 07-printf/exercises/08/solution/main.go create mode 100644 07-printf/exercises/09/main.go create mode 100644 07-printf/exercises/09/solution/main.go create mode 100644 07-printf/exercises/10/main.go create mode 100644 07-printf/exercises/10/solution/main.go create mode 100644 07-printf/printf cheatsheet.pdf create mode 100644 07-printf/questions/questions.md create mode 100644 08-numbers-and-strings/01-numbers/01-arithmetic-operators/01/main.go create mode 100644 08-numbers-and-strings/01-numbers/01-arithmetic-operators/02/main.go create mode 100644 08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/01/main.go create mode 100644 08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/02/main.go create mode 100644 08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/03/main.go create mode 100644 08-numbers-and-strings/01-numbers/02-arithmetic-operators-examples/01/main.go create mode 100644 08-numbers-and-strings/01-numbers/02-arithmetic-operators-examples/02/main.go create mode 100644 08-numbers-and-strings/01-numbers/03-precedence/01/main.go create mode 100644 08-numbers-and-strings/01-numbers/03-precedence/02/main.go create mode 100644 08-numbers-and-strings/01-numbers/03-precedence/03/main.go create mode 100644 08-numbers-and-strings/01-numbers/03-precedence/04/main.go create mode 100644 08-numbers-and-strings/01-numbers/04-incdec-statement/01/main.go create mode 100644 08-numbers-and-strings/01-numbers/04-incdec-statement/02/main.go create mode 100644 08-numbers-and-strings/01-numbers/04-incdec-statement/03/main.go create mode 100644 08-numbers-and-strings/01-numbers/05-assignment-operations/main.go create mode 100644 08-numbers-and-strings/01-numbers/06-project-feet-to-meters/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/01/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/01/solution/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/02/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/02/solution/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/03/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/03/solution/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/04/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/04/solution/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/05/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/05/solution/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/06/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/06/solution/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/07-capstone1/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/07-capstone1/solution/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/08-capstone2/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/08-capstone2/solution/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/09-capstone3/main.go create mode 100644 08-numbers-and-strings/01-numbers/exercises/09-capstone3/solution/main.go create mode 100644 08-numbers-and-strings/01-numbers/questions/01-arithmetic-operators.md create mode 100644 08-numbers-and-strings/01-numbers/questions/02-precedence.md create mode 100644 08-numbers-and-strings/01-numbers/questions/03-assignment-operations.md create mode 100644 08-numbers-and-strings/02-strings/01-raw-string-literal/main.go create mode 100644 08-numbers-and-strings/02-strings/02-concatenation/01/main.go create mode 100644 08-numbers-and-strings/02-strings/02-concatenation/02-assignment-operation/main.go create mode 100644 08-numbers-and-strings/02-strings/02-concatenation/03-concat-non-strings/main.go create mode 100644 08-numbers-and-strings/02-strings/03-string-length/01-len/main.go create mode 100644 08-numbers-and-strings/02-strings/03-string-length/02-unicode-len/main.go create mode 100644 08-numbers-and-strings/02-strings/04-project-banger/exercise-solution/main.go create mode 100644 08-numbers-and-strings/02-strings/04-project-banger/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/01/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/01/solution/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/02/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/02/solution/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/03/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/03/solution/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/04/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/04/solution/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/05/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/05/solution/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/06/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/06/solution/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/07/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/07/solution/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/08/main.go create mode 100644 08-numbers-and-strings/02-strings/exercises/08/solution/main.go create mode 100644 08-numbers-and-strings/02-strings/questions/questions.md create mode 100644 09-go-type-system/01-bits/main.go create mode 100644 09-go-type-system/02-bytes/main.go create mode 100644 09-go-type-system/03-predeclared-types/main.go create mode 100644 09-go-type-system/04-overflow/01-problem/main.go create mode 100644 09-go-type-system/04-overflow/02-explain/main.go create mode 100644 09-go-type-system/04-overflow/03-destructive/main.go create mode 100644 09-go-type-system/05-defined-types/01-duration-example/main.go create mode 100644 09-go-type-system/05-defined-types/02-type-definition-create-your-own-type/main.go create mode 100644 09-go-type-system/05-defined-types/03-underlying-types/main.go create mode 100644 09-go-type-system/05-defined-types/03-underlying-types/weights/weights.go create mode 100644 09-go-type-system/06-aliased-types/main.go create mode 100644 09-go-type-system/exercises/01/main.go create mode 100644 09-go-type-system/exercises/01/solution/main.go create mode 100644 09-go-type-system/exercises/02/main.go create mode 100644 09-go-type-system/exercises/02/solution/main.go create mode 100644 09-go-type-system/exercises/03/main.go create mode 100644 09-go-type-system/exercises/03/solution/main.go create mode 100644 09-go-type-system/exercises/04/main.go create mode 100644 09-go-type-system/exercises/04/solution/main.go create mode 100644 09-go-type-system/exercises/05/main.go create mode 100644 09-go-type-system/exercises/05/solution/main.go create mode 100644 09-go-type-system/exercises/06/main.go create mode 100644 09-go-type-system/exercises/06/solution/main.go create mode 100644 09-go-type-system/questions/01-questions-predeclared-types.md create mode 100644 09-go-type-system/questions/02-questions-defined-types.md create mode 100644 10-constants/01-declarations/01-syntax/01-magic-numbers/main.go create mode 100644 10-constants/01-declarations/01-syntax/02-constants/main.go create mode 100644 10-constants/01-declarations/01-syntax/03-safety/main.go create mode 100644 10-constants/01-declarations/01-syntax/04-rules/01-immutability/main.go create mode 100644 10-constants/01-declarations/01-syntax/04-rules/02-runtime-func/main.go create mode 100644 10-constants/01-declarations/01-syntax/04-rules/03-runtime-var/main.go create mode 100644 10-constants/01-declarations/01-syntax/04-rules/04-len/main.go create mode 100644 10-constants/01-declarations/02-constant-types-and-expressions/01/main.go create mode 100644 10-constants/01-declarations/02-constant-types-and-expressions/02/main.go create mode 100644 10-constants/01-declarations/02-constant-types-and-expressions/03/main.go create mode 100644 10-constants/01-declarations/03-multiple-declaration/01/main.go create mode 100644 10-constants/01-declarations/03-multiple-declaration/02/main.go create mode 100644 10-constants/01-declarations/03-multiple-declaration/03/main.go create mode 100644 10-constants/02-typeless-constants/01-typeless-constants/main.go create mode 100644 10-constants/02-typeless-constants/02-typed-vs-typeless/01/main.go create mode 100644 10-constants/02-typeless-constants/02-typed-vs-typeless/02/main.go create mode 100644 10-constants/02-typeless-constants/02-typed-vs-typeless/03/main.go create mode 100644 10-constants/02-typeless-constants/02-typed-vs-typeless/04/main.go create mode 100644 10-constants/02-typeless-constants/03-default-type/01/main.go create mode 100644 10-constants/02-typeless-constants/03-default-type/02/main.go create mode 100644 10-constants/02-typeless-constants/03-default-type/03/main.go create mode 100644 10-constants/02-typeless-constants/03-default-type/04/main.go create mode 100644 10-constants/02-typeless-constants/03-default-type/05/main.go create mode 100644 10-constants/02-typeless-constants/04-demo/01/main.go create mode 100644 10-constants/02-typeless-constants/04-demo/02/main.go create mode 100644 10-constants/03-refactor-feet-to-meters/main.go create mode 100644 10-constants/03-refactor-feet-to-meters/solution/main.go create mode 100644 10-constants/03-refactor-feet-to-meters/solution/without-comments/main.go create mode 100644 10-constants/04-iota/01-manually/main.go create mode 100644 10-constants/04-iota/02-with-iota/main.go create mode 100644 10-constants/04-iota/03-expressions/main.go create mode 100644 10-constants/04-iota/04-blank-identifier/01/main.go create mode 100644 10-constants/04-iota/04-blank-identifier/02/main.go create mode 100644 10-constants/04-iota/04-blank-identifier/03/main.go create mode 100644 10-constants/exercises/01/main.go create mode 100644 10-constants/exercises/01/solution/main.go create mode 100644 10-constants/exercises/02/main.go create mode 100644 10-constants/exercises/02/solution/main.go create mode 100644 10-constants/exercises/03/main.go create mode 100644 10-constants/exercises/03/solution/main.go create mode 100644 10-constants/exercises/04/main.go create mode 100644 10-constants/exercises/04/solution/main.go create mode 100644 10-constants/exercises/05/main.go create mode 100644 10-constants/exercises/05/solution/main.go create mode 100644 10-constants/exercises/06/main.go create mode 100644 10-constants/exercises/06/solution/main.go create mode 100644 10-constants/exercises/07/main.go create mode 100644 10-constants/exercises/07/solution/main.go create mode 100644 10-constants/exercises/08/main.go create mode 100644 10-constants/exercises/08/solution/main.go create mode 100644 10-constants/exercises/09/main.go create mode 100644 10-constants/exercises/09/solution/main.go create mode 100644 10-constants/questions/questions.md create mode 100644 11-if/01-boolean-operators/01-comparison-operators/main.go create mode 100644 11-if/01-boolean-operators/02-comparison-and-assignability/01/main.go create mode 100644 11-if/01-boolean-operators/02-comparison-and-assignability/02/main.go create mode 100644 11-if/01-boolean-operators/02-comparison-and-assignability/03/main.go create mode 100644 11-if/01-boolean-operators/03-logical-operators/01-and-operator/01/main.go create mode 100644 11-if/01-boolean-operators/03-logical-operators/01-and-operator/02/main.go create mode 100644 11-if/01-boolean-operators/03-logical-operators/02-or-operator/01/main.go create mode 100644 11-if/01-boolean-operators/03-logical-operators/02-or-operator/02/main.go create mode 100644 11-if/01-boolean-operators/03-logical-operators/03-not-operator/01/main.go create mode 100644 11-if/01-boolean-operators/03-logical-operators/03-not-operator/02/main.go create mode 100644 11-if/02-if-statement/01-if-branch/main.go create mode 100644 11-if/02-if-statement/02-else-branch/main.go create mode 100644 11-if/02-if-statement/03-else-if-branch/01/main.go create mode 100644 11-if/02-if-statement/03-else-if-branch/02/main.go create mode 100644 11-if/02-if-statement/04-refactor-feet-to-meters/main.go create mode 100644 11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/01-challenge/main.go create mode 100644 11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/02-solution/main.go create mode 100644 11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/03-solution-refactor/main.go create mode 100644 11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/01-challenge/main.go create mode 100644 11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/02-solution/main.go create mode 100644 11-if/03-error-handling/01-itoa/main.go create mode 100644 11-if/03-error-handling/02-atoi/main.go create mode 100644 11-if/03-error-handling/03-atoi-error-handling/main.go create mode 100644 11-if/03-error-handling/04-challenge-feet-to-meters/01-challenge/main.go create mode 100644 11-if/03-error-handling/04-challenge-feet-to-meters/02-solution/main.go create mode 100644 11-if/04-short-if/01-without-short-if/main.go create mode 100644 11-if/04-short-if/02-with-short-if/main.go create mode 100644 11-if/04-short-if/03-scope/main.go create mode 100644 11-if/04-short-if/04-scope-shadowing/01-shadowing/main.go create mode 100644 11-if/04-short-if/04-scope-shadowing/02-shadowing-solution/main.go create mode 100644 11-if/exercises/00/main.go create mode 100644 11-if/exercises/00/solution/main.go create mode 100644 11-if/questions/questions.md create mode 100644 12-switch/01-one-case/main.go create mode 100644 12-switch/02-multiple-cases/main.go create mode 100644 12-switch/03-default-clause/main.go create mode 100644 12-switch/04-multiple-conditions/main.go create mode 100644 12-switch/05-bool-expressions/main.go create mode 100644 12-switch/06-fallthrough/01-without/main.go create mode 100644 12-switch/06-fallthrough/02-with/main.go create mode 100644 12-switch/07-short-switch/main.go create mode 100644 12-switch/08-parts-of-the-day/main.go create mode 100644 12-switch/09-when-to-use/main.go create mode 100644 12-switch/exercises/00/main.go create mode 100644 12-switch/exercises/00/solution/main.go create mode 100644 12-switch/exercises/_challenge/01-challenge/main.go create mode 100644 12-switch/exercises/_challenge/02-solution/main.go create mode 100644 12-switch/questions/questions.md create mode 100644 13-loops/01-loops/01-basics/main.go create mode 100644 13-loops/01-loops/02-break/main.go create mode 100644 13-loops/01-loops/03-continue/01-a-before/main.go create mode 100644 13-loops/01-loops/03-continue/01-b-after/main.go create mode 100644 13-loops/01-loops/04-nested-loops/main.go create mode 100644 13-loops/01-loops/05-for-range/01-loop-over-slices/main.go create mode 100644 13-loops/01-loops/05-for-range/02-loop-over-words/main.go create mode 100644 13-loops/02-project-lucky-number-game/01-randomization/main.go create mode 100644 13-loops/02-project-lucky-number-game/02-game/main.go create mode 100644 13-loops/03-project-word-finder/main.go create mode 100644 13-loops/04-labeled-statements/01-word-finder-break/main.go create mode 100644 13-loops/04-labeled-statements/02-word-finder-continue/main.go create mode 100644 13-loops/04-labeled-statements/03-word-finder-switch/main.go create mode 100644 13-loops/04-labeled-statements/04-goto/main.go create mode 100644 13-loops/exercises/01-loops/01-sum-the-numbers/main.go create mode 100644 13-loops/exercises/01-loops/01-sum-the-numbers/solution/main.go create mode 100644 13-loops/exercises/01-loops/02-sum-the-numbers-verbose/main.go create mode 100644 13-loops/exercises/01-loops/02-sum-the-numbers-verbose/solution/main.go create mode 100644 13-loops/exercises/01-loops/03-sum-up-to-n/main.go create mode 100644 13-loops/exercises/01-loops/03-sum-up-to-n/solution/main.go create mode 100644 13-loops/exercises/01-loops/04-only-evens/main.go create mode 100644 13-loops/exercises/01-loops/04-only-evens/solution/main.go create mode 100644 13-loops/exercises/01-loops/05-break-up/main.go create mode 100644 13-loops/exercises/01-loops/05-break-up/solution/main.go create mode 100644 13-loops/exercises/01-loops/06-infinite-kill/main.go create mode 100644 13-loops/exercises/01-loops/06-infinite-kill/solution/main.go create mode 100644 13-loops/exercises/01-loops/07-crunch-the-primes/main.go create mode 100644 13-loops/exercises/01-loops/07-crunch-the-primes/solution/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/solution/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/02-random-messages/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/02-random-messages/solution/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/03-double-guesses/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/03-double-guesses/solution/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/solution/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/05-enough-picks/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/05-enough-picks/solution/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/main.go create mode 100644 13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/solution/main.go create mode 100644 13-loops/exercises/03-word-finder-exercises/01-case-insensitive/main.go create mode 100644 13-loops/exercises/03-word-finder-exercises/01-case-insensitive/solution/main.go create mode 100644 13-loops/exercises/03-word-finder-exercises/02-path-searcher/main.go create mode 100644 13-loops/exercises/03-word-finder-exercises/02-path-searcher/solution/main.go create mode 100644 13-loops/exercises/more-exercises.md create mode 100644 13-loops/questions/questions.md create mode 100644 14-arrays/01-what/01-without-arrays/main.go create mode 100644 14-arrays/01-what/02-with-arrays/main.go create mode 100644 14-arrays/02-array-literals/01/main.go create mode 100644 14-arrays/02-array-literals/02-fixed-size/main.go create mode 100644 14-arrays/02-array-literals/03-constant-expressions/main.go create mode 100644 14-arrays/02-array-literals/04-element-type/main.go create mode 100644 14-arrays/02-array-literals/05-ellipsis/main.go create mode 100644 14-arrays/03-array-type/main.go create mode 100644 14-arrays/04-zero-values/01-uninitialized-array/main.go create mode 100644 14-arrays/04-zero-values/02-partially-initialized-array/main.go create mode 100644 14-arrays/04-zero-values/03-ellipsis/main.go create mode 100644 14-arrays/04-zero-values/04-without-ellipsis/main.go create mode 100644 14-arrays/04-zero-values/05-more-examples/main.go create mode 100644 14-arrays/05-array-operations/01-getting-length/main.go create mode 100644 14-arrays/05-array-operations/02-getting-elements/main.go create mode 100644 14-arrays/05-array-operations/03-getting-elements/main.go create mode 100644 14-arrays/06-comparing-arrays/01-equal-arrays/main.go create mode 100644 14-arrays/06-comparing-arrays/02-not-equal-arrays/main.go create mode 100644 14-arrays/06-comparing-arrays/03-comparability/main.go create mode 100644 14-arrays/06-comparing-arrays/04-example/main.go create mode 100644 14-arrays/07-unnamed-vs-named-types/main.go create mode 100644 14-arrays/08-assigning-arrays/01-assigning-arrays/main.go create mode 100644 14-arrays/08-assigning-arrays/02-arrays-are-value-types/main.go create mode 100644 14-arrays/08-assigning-arrays/03-assigning-arrays2/main.go create mode 100644 14-arrays/08-assigning-arrays/04-cannot-assign-arrays/main.go create mode 100644 14-arrays/08-assigning-arrays/05-passing-to-funcs/main.go create mode 100644 14-arrays/09-multi-dimensional-arrays/main.go create mode 100644 14-arrays/10-keyed-elements/01-unkeyed/main.go create mode 100644 14-arrays/10-keyed-elements/02-keyed/main.go create mode 100644 14-arrays/10-keyed-elements/03-keyed-order/main.go create mode 100644 14-arrays/10-keyed-elements/04-keyed-auto-initialize/main.go create mode 100644 14-arrays/10-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go create mode 100644 14-arrays/10-keyed-elements/06-keyed-and-unkeyed/main.go create mode 100644 14-arrays/11-project-xratio/01-without-keys/main.go create mode 100644 14-arrays/11-project-xratio/02-with-keys/main.go create mode 100644 14-arrays/12-random-message/01-1st-version/main.go create mode 100644 14-arrays/12-random-message/02-2nd-version/main.go create mode 100644 14-arrays/13-refactor-lucky-number-game/main.go create mode 100644 14-arrays/exercises/00/main.go create mode 100644 14-arrays/exercises/00/solution/main.go create mode 100644 14-arrays/exercises/xxx-lucky-number-unique-picker/main.go create mode 100644 14-arrays/questions/questions.md create mode 100644 15-slices/01-intro/01-theory/main.go create mode 100644 15-slices/01-intro/02-examples/main.go create mode 100644 15-slices/02-append/01-theory/main.go create mode 100644 15-slices/02-append/02-examples/main.go create mode 100644 15-slices/03-slices-vs-arrays/01-with-arrays/main.go create mode 100644 15-slices/03-slices-vs-arrays/02-with-slices/main.go create mode 100644 15-slices/04-copy/01-usage/main.go create mode 100644 15-slices/04-copy/02-hacker-incident/main.go create mode 100644 15-slices/05-whats-a-slice-in-real/01-theory/main.go create mode 100644 15-slices/05-whats-a-slice-in-real/02-example/main.go create mode 100644 15-slices/06-slice-expressions/01-theory/main.go create mode 100644 15-slices/06-slice-expressions/02-examples/main.go create mode 100644 15-slices/07-full-slice-expressions/main.go create mode 100644 15-slices/08-make/main.go create mode 100644 15-slices/09-bouncing-ball-challenge/01-challenge/main.go create mode 100644 15-slices/09-bouncing-ball-challenge/02-solution-draw-the-board/main.go create mode 100644 15-slices/09-bouncing-ball-challenge/03-solution-drawing-loop/main.go create mode 100644 15-slices/09-bouncing-ball-challenge/04-solution-final/main.go create mode 100644 15-slices/README-WARNING.md create mode 100644 16-strings-revisited/01-byte/main.go create mode 100644 16-strings-revisited/02-string-byte-slice/main.go create mode 100644 16-strings-revisited/03-string-indexing-slicing/main.go create mode 100644 16-strings-revisited/04-masker-challenge/main.go create mode 100644 16-strings-revisited/04-masker-challenge/spam.txt create mode 100644 16-strings-revisited/05-masker-solution/main.go create mode 100644 16-strings-revisited/05-masker-solution/spam.txt create mode 100644 16-strings-revisited/06-encoding/main.go create mode 100644 16-strings-revisited/07-encoding-examples/main.go create mode 100644 16-strings-revisited/08-wrapper-example/main.go create mode 100644 16-strings-revisited/09-internals/main.go create mode 100644 16-strings-revisited/charset-table/main.go create mode 100644 16-strings-revisited/notes.md create mode 100644 17-maps/README.md create mode 100644 18-structs/README.md create mode 100644 19-functions/README.md create mode 100644 20-pointers/README.md create mode 100644 21-closures/README.md create mode 100644 22-deferred-funcs/README.md create mode 100644 23-variadic-funcs/README.md create mode 100644 24-methods/README.md create mode 100644 25-method-values/README.md create mode 100644 26-interfaces/README.md create mode 100644 27-embedding/README.md create mode 100644 28-concurrency/README.md create mode 100644 29-tba/README.md create mode 100644 README.md create mode 100644 Warming Up Exercises.md create mode 100644 first/README.md create mode 100644 first/explain/comments/main.go create mode 100644 first/explain/expressions/01-operator/main.go create mode 100644 first/explain/expressions/02-call-expression/main.go create mode 100644 first/explain/expressions/README create mode 100644 first/explain/importing/01-file-scope/main.go create mode 100644 first/explain/importing/02-renaming/main.go create mode 100644 first/explain/packages/scopes/bye.go create mode 100644 first/explain/packages/scopes/hey.go create mode 100644 first/explain/packages/scopes/main.go create mode 100644 first/explain/packages/what/bye.go create mode 100644 first/explain/packages/what/hey.go create mode 100644 first/explain/packages/what/main.go create mode 100644 first/explain/scopes/01-scopes/main.go create mode 100644 first/explain/scopes/02-block-scope/main.go create mode 100644 first/explain/scopes/03-nested-scope/main.go create mode 100644 first/explain/statements/01-execution-flow/main.go create mode 100644 first/explain/statements/02-semicolons/main.go create mode 100644 first/first/exercises/01/main.go create mode 100644 first/first/exercises/01/solution/main.go create mode 100644 first/first/exercises/02/exercise.md create mode 100644 first/first/exercises/02/solution/solution.md create mode 100644 first/first/main.go create mode 100644 first/first/questions/01-gopath-questions.md create mode 100644 first/first/questions/02-code-your-first-program-questions.md create mode 100644 first/first/questions/03-run-your-first-program-questions.md create mode 100644 first/printer-exercise/solution/golang/cmd/main.go create mode 100644 first/printer-exercise/solution/golang/go.go create mode 100644 first/printer/cmd/main.go create mode 100644 first/printer/printer.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..817930f --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +*.DS_Store + +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +.vscode/* diff --git a/02-write-your-first-program/exercises/01/main.go b/02-write-your-first-program/exercises/01/main.go new file mode 100644 index 0000000..1820160 --- /dev/null +++ b/02-write-your-first-program/exercises/01/main.go @@ -0,0 +1,27 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print your name and your best friend's name using +// Println twice +// +// EXPECTED OUTPUT +// YourName +// YourBestFriendName +// +// BONUS +// Use `go run` first. +// And after that use `go build` and run your program. +// --------------------------------------------------------- + +func main() { + // ? + // ? +} diff --git a/02-write-your-first-program/exercises/01/solution/main.go b/02-write-your-first-program/exercises/01/solution/main.go new file mode 100644 index 0000000..b1aadb3 --- /dev/null +++ b/02-write-your-first-program/exercises/01/solution/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" + +// go run main.go + +// go build +// ./solution + +func main() { + fmt.Println("Nikola") + fmt.Println("Thomas") +} diff --git a/02-write-your-first-program/exercises/02/exercise.md b/02-write-your-first-program/exercises/02/exercise.md new file mode 100644 index 0000000..cb2de95 --- /dev/null +++ b/02-write-your-first-program/exercises/02/exercise.md @@ -0,0 +1,7 @@ +// --------------------------------------------------------- +// EXERCISE +// Print your GOPATH using `go env` tool +// +// EXPECTED OUTPUT +// The physical folder path that is referenced by $GOPATH +// --------------------------------------------------------- \ No newline at end of file diff --git a/02-write-your-first-program/exercises/02/solution/solution.md b/02-write-your-first-program/exercises/02/solution/solution.md new file mode 100644 index 0000000..4176bbd --- /dev/null +++ b/02-write-your-first-program/exercises/02/solution/solution.md @@ -0,0 +1,3 @@ +You should type this: + +go env GOPATH \ No newline at end of file diff --git a/02-write-your-first-program/exercises/all-exercises.md b/02-write-your-first-program/exercises/all-exercises.md new file mode 100644 index 0000000..88cb1ae --- /dev/null +++ b/02-write-your-first-program/exercises/all-exercises.md @@ -0,0 +1,39 @@ +1. **Run your own program? Say hello to yourself.** + + 1. Build your program using `go build` + + 2. And, send it to your friend + (s/he should use be using the same operating system) + (if you're using windows, then hers/his should be + windows too) + + 3. And then send your program to a friend with a different + operating system. + + (So, you should compile your program for her operating system). + + **For OSX, type:** + GOOS=darwin GOARCH=386 go build + + **For Windows:** + GOOS=windows GOARCH=386 go build + + **For Linux:** + GOOS=linux GOARCH=arm GOARM=7 go build + + **You can find the full list in here:** + https://golang.org/doc/install/source#environment + +2. **Call Print instead of Println** to see what happens. + +3. **Call Println or Print with multiple values** by separating them using commas. + +4. **Remove double quotes from string literals** and see what happens. + +5. **Move the package and import statement** to the bottom of the file and see what happens. + +6. **Read Go online documentation**. Take a quick look at the packages and read what they do. Look at their source-code by clicking on their titles. + +You don't have to understand anything, just do it. This will warm you up for the upcoming lectures. https://golang.org/pkg + +7. Also, **take a tour**: https://tour.golang.org/ See the language features. We're going to talk all about them soon. \ No newline at end of file diff --git a/02-write-your-first-program/main.go b/02-write-your-first-program/main.go new file mode 100644 index 0000000..f91956f --- /dev/null +++ b/02-write-your-first-program/main.go @@ -0,0 +1,61 @@ +// 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 is a special package +// it allows Go to create an executable file +package main + +/* +This is a multi-line comment. + +import keyword makes another package available + for this .go "file". + +import "fmt" lets you access fmt package's functionality + here in this file. +*/ +import "fmt" + +// "func main" is special. +// +// Go has to know where to start +// +// func main creates a starting point for Go +// +// After compiling the code, +// Go runtime will first run this function +func main() { + // after: import "fmt" + // Println function of "fmt" package becomes available + + // Look at what it looks like by typing in the console: + // godoc -src fmt Println + + // Println is just an exported function from + // "fmt" package + + // Exported = First Letter is uppercase + fmt.Println("Hello Gopher!") + + // Go cannot call Println function by itself. + // That's why you need to call it here. + // It only calls `func main` automatically. + + // ----- + + // Go supports Unicode characters in string literals + // And also in source-code: KÖSTEBEK! + // + // Because: Literal ~= Source Code + + // EXERCISE: Remove the comments from below --> // + // fmt.Println("Merhaba Köstebek!") + + // Unnecessary note: + // "Merhaba Köstebek" means "Hello Gopher" + // in Turkish language +} diff --git a/02-write-your-first-program/questions/01-gopath-questions.md b/02-write-your-first-program/questions/01-gopath-questions.md new file mode 100644 index 0000000..7904067 --- /dev/null +++ b/02-write-your-first-program/questions/01-gopath-questions.md @@ -0,0 +1,19 @@ +## Where you should put your Go source code into? +* Anywhere on my computer +* Under $GOPATH +* Under $GOPATH/src *CORRECT* + +## What $GOPATH means? +* It's a file for Go runtime +* Stores Go source code files and compiled packages +* It's a path for gophers to follow + +## Do you need to set your $GOPATH? +* Yes +* No: It's stored on my desktop +* No: It's stored under my user path *CORRECT* + +## How can you print your $GOPATH? +* Using `ls` command +* Using `go env GOPATH` command *CORRECT* +* Using `go environment` command \ No newline at end of file diff --git a/02-write-your-first-program/questions/02-code-your-first-program-questions.md b/02-write-your-first-program/questions/02-code-your-first-program-questions.md new file mode 100644 index 0000000..0179102 --- /dev/null +++ b/02-write-your-first-program/questions/02-code-your-first-program-questions.md @@ -0,0 +1,151 @@ +## What does the package keyword do in the following program? +```go +package main + +func main() { +} +``` +1. func +2. package *CORRECT* +3. fmt.Println +4. import + +> 1. This keyword is used to declare a new function. +> 2. That's right! package keyword allows you to define which package a Go file belongs to. +> 3. This is not a keyword. It's the Println function of the fmt package. +> 4. This keyword is used to import a package. + + +## Which keyword is used to declare a new function? +* func *CORRECT* +* package +* Println +* import + + +## What is a function? +1. It's like a mini-program. It's a reusable and executable block of code. *CORRECT* +2. It allows Go to execute a program. +3. It allows Go to import a package called function. +4. It prints a message to the console. + +> 2. Go looks for package main and func main to do that. A function doesn't do that on its own. +> 3. `import` keyword does that. +> 4. For example: `fmt.Println` does that. + + +## Do you have to call the main function yourself? +1. Yes, so that, I can execute my program. +2. No, Go calls the main function automatically. *CORRECT* + +> 1. No, you don't need to call the main function. Go automatically executes it. + + +## Do you have to call the other functions yourself? +1. Yes, so that, I can execute that function. *CORRECT* +2. Yes, so that, Go can execute my program. +3. No, Go calls the functions automatically. + +> 1. That's right. You need to call a function yourself. Go won't execute it automatically. Go only calls the main function automatically (and some other functions which you didn't learn about yet). + +> 2. That's only the job of the `func main`. There's only one `func main`. + +> 3. Go doesn't call any function automatically except the main func (and some other functions which you didn't learn about yet). So, except the main func, you need to call the functions yourself. + + +## What does `package main` do? +```go +package main + +func main() { +} +``` +* It controls everything +* It allows you to properly exit from a program +* It allows you to create an executable Go program *CORRECT* + + +## What does `func main` do? +```go +package main + +func main() { +} +``` +1. It contains a package called main +2. Go starts executing your program by using the code inside func main *CORRECT* +3. It prints a message to the console + +> 1. main function doesn't contain a package. +> 2. That's right. Go automatically calls the main function to execute your program. +> 3. It doesn't print anything at least directly. + + +## What does `import "fmt"` do? +```go +package main +import "fmt" + +func main() { + fmt.Println("Hi!") +} +``` +1. It prints "fmt" to the console +2. It defines a new package called "fmt" +3. It imports the `fmt` package; so you can use its functionalities *CORRECT* + +> 1. `fmt.Println` prints a message not the `import "fmt"`. +> 2. `package` keyword does that, not the `import` keyword. +> 3. Yes. For example, after you import the fmt package you can call its Println function to print a message to the console. + + +## What this program does? +```go +package main + +func main() { +} +``` +1. It prints a message to the console +2. It's a correct program but it doesn't print anything *CORRECT* +3. It's an incorrect program + +> 1. It doesn't print a message. To do that you can use fmt.Println function. + +> 2. Yes, it's a correct program but since it doesn't contain fmt.Println it doesn't print anything. + +> 3. It's a correct program. It uses the package keyword and it has a main function. So, this is a valid and an executable Go program. + + +## What does this program print? +```go +package main + +func main() { + fmt.Println(Hi! I want to be a Gopher!) +} +``` +* Hi! I want to be a Gopher! +* It doesn't print anything +* This program is incorrect *CORRECT* + +> 1. It doesn't pass the message to Println wrapped between double-quotes. It should be like: fmt.Println("Hi! I want to be a Gopher") + +> 3. It doesn't import "fmt" package. Also see #1. + + +## What does this program print? +```go +package main +import "fmt" + +func main() { + fmt.Println("Hi there!") +} +``` +* Hi there! *CORRECT* +* fmt +* This program is incorrect; it imports the wrong package or there isn't a function called `Println` + +> 2. import "fmt" imports the `fmt` package; so you can use its functionalities. +> 3. Actually, this program is correct. \ No newline at end of file diff --git a/02-write-your-first-program/questions/03-run-your-first-program-questions.md b/02-write-your-first-program/questions/03-run-your-first-program-questions.md new file mode 100644 index 0000000..f0959ad --- /dev/null +++ b/02-write-your-first-program/questions/03-run-your-first-program-questions.md @@ -0,0 +1,43 @@ +## What's the difference between `go build` and `go run`? +1. `go run` just compiles a program; whereas `go build` both compiles and runs it. +2. `go run` both compiles and runs a program; whereas `go build` just compiles it. *CORRECT* + +> 1. It's opposite actually. +> 2. `go run` compiles your program and puts it in a temporary directory. Then it runs the compiled program in there. + + +## Which directory `go build` puts the compiled code into? +1. The same directory where you call `go build` *CORRECT* +2. $GOPATH/src directory +3. $GOPATH/pkg directory +4. Into a temporary directory. + +> 2. There only lives Go source-code files +> 3. Go only puts your code there when you call `go install`. + + +## Which directory `go run` puts the compiled code into? +1. The same directory where you call `go run` +2. $GOPATH/src directory +3. $GOPATH/pkg directory +4. Into a temporary directory. *CORRECT* + + +## Which one below is true for runtime? +1. It happens when your program starts running in a computer *CORRECT* +2. It happens while your program is being compiled + + +## Which one below is true for the compile-time? +1. It happens when your program starts running in a computer +2. It happens while your program is being compiled *CORRECT* + + +## In which stage your program can print a message to the console? +1. While it's being compiled. +2. While it runs (after compile-time). *CORRECT* +3. While it runs (inside the compile-time). + +> 1. In the compilation step your program cannot print a message. In that stage, it's literally dead. +> 2. That's right. That's the only time which your program can interact with a computer and instruct it to print a message to the console. +> 3. Running can only happen after the compile-time \ No newline at end of file diff --git a/03-packages-and-scopes/01-packages/bye.go b/03-packages-and-scopes/01-packages/bye.go new file mode 100644 index 0000000..4705383 --- /dev/null +++ b/03-packages-and-scopes/01-packages/bye.go @@ -0,0 +1,14 @@ +// 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 bye() { + fmt.Println("Bye!") +} diff --git a/03-packages-and-scopes/01-packages/hey.go b/03-packages-and-scopes/01-packages/hey.go new file mode 100644 index 0000000..9f2b3d9 --- /dev/null +++ b/03-packages-and-scopes/01-packages/hey.go @@ -0,0 +1,14 @@ +// 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 hey() { + fmt.Println("Hey!") +} diff --git a/03-packages-and-scopes/01-packages/main.go b/03-packages-and-scopes/01-packages/main.go new file mode 100644 index 0000000..b2f5ac1 --- /dev/null +++ b/03-packages-and-scopes/01-packages/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" + +func main() { + fmt.Println("Hello!") + + // You can access functions from other files + // which are in the same package + + // Here, `main()` can access `bye()` and `hey()` + + // It's because: bye.go, hey.go and main.go + // are in the main package. + + bye() + hey() +} diff --git a/03-packages-and-scopes/02-scopes/01-scopes/main.go b/03-packages-and-scopes/02-scopes/01-scopes/main.go new file mode 100644 index 0000000..9b8fb9b --- /dev/null +++ b/03-packages-and-scopes/02-scopes/01-scopes/main.go @@ -0,0 +1,24 @@ +// 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 + +// file scope +import "fmt" + +// package scope +const ok = true + +// package scope +func main() { // block scope starts + + var hello = "Hello" + + // hello and ok are visible here + fmt.Println(hello, ok) + +} // block scope ends diff --git a/03-packages-and-scopes/02-scopes/02-block-scope/main.go b/03-packages-and-scopes/02-scopes/02-block-scope/main.go new file mode 100644 index 0000000..155ba37 --- /dev/null +++ b/03-packages-and-scopes/02-scopes/02-block-scope/main.go @@ -0,0 +1,26 @@ +// 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 + +func nope() { // block scope starts + + // hello and ok are only visible here + const ok = true + var hello = "Hello" + + _ = hello +} // block scope ends + +func main() { // block scope starts + + // hello and ok are not visible here + + // ERROR: + // fmt.Println(hello, ok) + +} // block scope ends diff --git a/03-packages-and-scopes/02-scopes/03-nested-scope/main.go b/03-packages-and-scopes/02-scopes/03-nested-scope/main.go new file mode 100644 index 0000000..1a3f6ed --- /dev/null +++ b/03-packages-and-scopes/02-scopes/03-nested-scope/main.go @@ -0,0 +1,39 @@ +// 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" + +// I didn't talk about this in a lecture +// As a side-note, I wanted to put it here +// Please review it. + +var declareMeAgain = 10 + +func nested() { // block scope starts + + // declares the same variable + // they both can exist together + // this one only belongs to this scope + // package's variable is still intact + var declareMeAgain = 5 + fmt.Println("inside nope:", declareMeAgain) + +} // block scope ends + +func main() { // block scope starts + + fmt.Println("inside main:", declareMeAgain) + + nested() + + // package-level declareMeAgain isn't effected + // from the change inside the nested func + fmt.Println("inside main:", declareMeAgain) + +} // block scope ends diff --git a/03-packages-and-scopes/02-scopes/04-package-scope/bye.go b/03-packages-and-scopes/02-scopes/04-package-scope/bye.go new file mode 100644 index 0000000..4705383 --- /dev/null +++ b/03-packages-and-scopes/02-scopes/04-package-scope/bye.go @@ -0,0 +1,14 @@ +// 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 bye() { + fmt.Println("Bye!") +} diff --git a/03-packages-and-scopes/02-scopes/04-package-scope/hey.go b/03-packages-and-scopes/02-scopes/04-package-scope/hey.go new file mode 100644 index 0000000..9f2b3d9 --- /dev/null +++ b/03-packages-and-scopes/02-scopes/04-package-scope/hey.go @@ -0,0 +1,14 @@ +// 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 hey() { + fmt.Println("Hey!") +} diff --git a/03-packages-and-scopes/02-scopes/04-package-scope/main.go b/03-packages-and-scopes/02-scopes/04-package-scope/main.go new file mode 100644 index 0000000..fc8ede8 --- /dev/null +++ b/03-packages-and-scopes/02-scopes/04-package-scope/main.go @@ -0,0 +1,38 @@ +// 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() { + fmt.Println("Hello!") + + // two files belong to the same package + // calling `bye()` of bye.go here + bye() +} + +// EXERCISE: Remove the comments from the below function +// And analyze the error message + +// func bye() { +// fmt.Println("Bye!") +// } + +// ***** EXPLANATION ***** +// +// ERROR: "bye" function "redeclared" +// in "this block" +// +// "this block" means = "main package" +// +// "redeclared" means = you're using the same name +// in the same scope again +// +// main package's scope is: +// all source-code files which are in the same main package diff --git a/03-packages-and-scopes/03-importing/01-file-scope/bye.go b/03-packages-and-scopes/03-importing/01-file-scope/bye.go new file mode 100644 index 0000000..ac8a89f --- /dev/null +++ b/03-packages-and-scopes/03-importing/01-file-scope/bye.go @@ -0,0 +1,18 @@ +// 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 + +// Uncomment below code to see the error +// (Just remove the // characters for all 3 lines below) + +// This file cannot see main.go's imported names ("fmt"). +// Because, the imported names belong to file scope. + +// func bye() { +// fmt.Println("Bye!") +// } diff --git a/03-packages-and-scopes/03-importing/01-file-scope/main.go b/03-packages-and-scopes/03-importing/01-file-scope/main.go new file mode 100644 index 0000000..a480589 --- /dev/null +++ b/03-packages-and-scopes/03-importing/01-file-scope/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Println("Hello!") +} diff --git a/03-packages-and-scopes/03-importing/02-renaming/main.go b/03-packages-and-scopes/03-importing/02-renaming/main.go new file mode 100644 index 0000000..d5af349 --- /dev/null +++ b/03-packages-and-scopes/03-importing/02-renaming/main.go @@ -0,0 +1,16 @@ +// 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" +import f "fmt" + +func main() { + fmt.Println("Hello!") + f.Println("There!") +} diff --git a/03-packages-and-scopes/exercises/01-packages/main.go b/03-packages-and-scopes/exercises/01-packages/main.go new file mode 100644 index 0000000..4d10ef8 --- /dev/null +++ b/03-packages-and-scopes/exercises/01-packages/main.go @@ -0,0 +1,30 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Create a few Go files and call their functions from +// the main function. +// +// 1- Create main.go, greet.go and bye.go files +// 2- In main.go: Call greet and bye functions. +// 3- Run `main.go` +// +// HINT +// greet function should be in greet.go +// bye function should be in bye.go +// +// EXPECTED OUTPUT +// hi there +// goodbye +// --------------------------------------------------------- + +func main() { + // call functions of the other files here +} diff --git a/03-packages-and-scopes/exercises/01-packages/solution/bye.go b/03-packages-and-scopes/exercises/01-packages/solution/bye.go new file mode 100644 index 0000000..6b4ab5e --- /dev/null +++ b/03-packages-and-scopes/exercises/01-packages/solution/bye.go @@ -0,0 +1,14 @@ +// 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 bye() { + fmt.Println("goodbye") +} diff --git a/03-packages-and-scopes/exercises/01-packages/solution/greet.go b/03-packages-and-scopes/exercises/01-packages/solution/greet.go new file mode 100644 index 0000000..0083ee1 --- /dev/null +++ b/03-packages-and-scopes/exercises/01-packages/solution/greet.go @@ -0,0 +1,14 @@ +// 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 greet() { + fmt.Println("hi there") +} diff --git a/03-packages-and-scopes/exercises/01-packages/solution/main.go b/03-packages-and-scopes/exercises/01-packages/solution/main.go new file mode 100644 index 0000000..0cbafc0 --- /dev/null +++ b/03-packages-and-scopes/exercises/01-packages/solution/main.go @@ -0,0 +1,13 @@ +// 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 + +func main() { + greet() + bye() +} diff --git a/03-packages-and-scopes/exercises/02-scopes/main.go b/03-packages-and-scopes/exercises/02-scopes/main.go new file mode 100644 index 0000000..f6419da --- /dev/null +++ b/03-packages-and-scopes/exercises/02-scopes/main.go @@ -0,0 +1,33 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Create two files: main.go and printer.go +// +// 2. In printer.go: +// 1. Create a function named hello +// 2. Inside the hello function, print your name +// +// 3. In main.go: +// 1. Create the usual func main +// 2. Call your function just by using its name: hello +// 3. Create a function named bye +// 4. Inside the bye function, print "bye bye" +// +// 4. In printer.go: +// 1. Call the bye function from +// inside the hello function +// +// 5. In main.go: +// 1. +// --------------------------------------------------------- + +func main() { +} diff --git a/03-packages-and-scopes/exercises/02-scopes/solution/main.go b/03-packages-and-scopes/exercises/02-scopes/solution/main.go new file mode 100644 index 0000000..fea1139 --- /dev/null +++ b/03-packages-and-scopes/exercises/02-scopes/solution/main.go @@ -0,0 +1,38 @@ +// 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() { + // as you can see, I don't need to import a package + // and I can call `hello` function here. + // + // this is because, package-scoped names + // are shared in the same package + hello() + + // but here, i can't access the fmt package without + // importing it. + // + // this is because, it's in the printer.go's file scope. + // it imports it. + + // this main func can also call bye function here + // bye() +} + +// printer.go can call this function +// +// this is because, bye function is in the package-scope +// of the main package now. +// +// main func can also call this. +func bye() { + fmt.Println("bye bye") +} diff --git a/03-packages-and-scopes/exercises/02-scopes/solution/printer.go b/03-packages-and-scopes/exercises/02-scopes/solution/printer.go new file mode 100644 index 0000000..99b7b0e --- /dev/null +++ b/03-packages-and-scopes/exercises/02-scopes/solution/printer.go @@ -0,0 +1,19 @@ +// 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 hello() { + // only this file can access the imported fmt package + // when others also do so, they can also access + // their own `fmt` "name" + + fmt.Println("hi! this is inanc!") + bye() +} diff --git a/03-packages-and-scopes/exercises/03-importing/main.go b/03-packages-and-scopes/exercises/03-importing/main.go new file mode 100644 index 0000000..171756d --- /dev/null +++ b/03-packages-and-scopes/exercises/03-importing/main.go @@ -0,0 +1,30 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Import fmt package three times with different names +// +// 2- Print a few messages using those imports +// +// EXPECTED OUTPUT +// hello +// hey +// hi +// --------------------------------------------------------- + +// ? +// ? +// ? + +func main() { + // ? + // ? + // ? +} diff --git a/03-packages-and-scopes/exercises/03-importing/solution/main.go b/03-packages-and-scopes/exercises/03-importing/solution/main.go new file mode 100644 index 0000000..901738e --- /dev/null +++ b/03-packages-and-scopes/exercises/03-importing/solution/main.go @@ -0,0 +1,18 @@ +// 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" +import f "fmt" +import fm "fmt" + +func main() { + fmt.Println("hello") + f.Println("hey") + fm.Println("hi") +} diff --git a/03-packages-and-scopes/questions/01-packages-A.md b/03-packages-and-scopes/questions/01-packages-A.md new file mode 100644 index 0000000..eaf0e99 --- /dev/null +++ b/03-packages-and-scopes/questions/01-packages-A.md @@ -0,0 +1,44 @@ +## Where to store the source code files that belong to a package? +1. Each file should go into a different directory +2. In a single directory *CORRECT* + + +## What's a `package clause` in a Go source-code file? +1. It's used for importing a package +2. It's used for letting Go know that the file belong to a package *CORRECT* +3. It's used for declaring a new function + +> 1. `import` statement does that. +> 3. `func` statement does that. + + +## Where you should put the `package clause` in a Go source-code file? +1. As the first code in a Go source code file *CORRECT* +2. As the last code in a Go source code file +3. You can put it anywhere + + +## How many times you can use `package clause` for a single source code file? +1. Once *CORRECT* +2. None +3. Multiple times + + +## Which one is a correct usage of `package clause`? +1. `my package` +2. `package main` +3. `pkg main` + + +## Which one is correct? +1. All files belong to the same package cannot call each others' functions +2. All files belong to the same package can call each others' functions *CORRECT* + + +## How to run multiple Go files? +1. go run *.*go +2. go build *go +3. go run go +4. go run *.go *CORRECT* + +> 4. You can also call it like (assuming there are file1.go file2.go and file3.go in the same directory): go run file1.go file2.go file3.go \ No newline at end of file diff --git a/03-packages-and-scopes/questions/01-packages-B.md b/03-packages-and-scopes/questions/01-packages-B.md new file mode 100644 index 0000000..3bacd02 --- /dev/null +++ b/03-packages-and-scopes/questions/01-packages-B.md @@ -0,0 +1,40 @@ +## Which one below is a correct package type in Go? +* Empty package +* Executable package *CORRECT* +* Transferrable package +* Librarian package + +## Which package type `go run` can execute? +* Empty package +* Executable package *CORRECT* +* Transferrable package +* Library package + +## Which package type that `go build` can compile? +* Empty package +* Temporary package +* Both of executable and library packages *CORRECT* +* Transferrable package + +## Which one is an executable package? +* `package main` with `func main` *CORRECT* +* `package Main` with `func Main` +* `package exec` with `func exec` + +## Which one is a library package? +* `main package` +* `package lib` *CORRECT* +* `func package` +* `package main` with `func main` + +## Which package is used for an executable Go program? +* Empty package +* Executable package *CORRECT* +* Transferrable package +* Library package + +## Which package is used for reusability and can be imported? +* Empty package +* Executable package +* Transferrable package +* Library package *CORRECT* diff --git a/03-packages-and-scopes/questions/02-scopes.md b/03-packages-and-scopes/questions/02-scopes.md new file mode 100644 index 0000000..a29d787 --- /dev/null +++ b/03-packages-and-scopes/questions/02-scopes.md @@ -0,0 +1,117 @@ +## What's a scope? +* Executable block of code +* The visibility of the declared names **CORRECT** +* Determines what to run + +```go +package awesome + +import "fmt" + +var enabled bool + +func block() { + var counter int + fmt.Println(counter) +} +``` + +## Which name below is package scoped? +1. awesome +2. fmt +3. enabled **CORRECT** +4. counter + +> 3. That's right. `enabled` is out of any functions, so it's a package scoped name. `block()` function is also package scoped; it's out of any function too. + + +## Which name below is file scoped? +1. awesome +2. fmt **CORRECT** +3. enabled +4. block() +5. counter + +> 2. That's right. Imported package names are file scoped. And they can only be used within the same file. + + +## Which name below is in the scope of the block() func? +1. awesome +2. fmt +3. enabled +4. block() +5. counter **CORRECT** + +> 5. That's right. `counter` is declared within the `block()` func, so it's in the scope of the block func. Out of the `block()` func, other code can't see it. + + +## Can `block()` see `enabled` name? +1. Yes: It's in the package scope **CORRECT** +2. No: It's in the file scope +3. No: It's in the block scope of block() + +> 1. All code inside the same package can see all the other package level declared names. + + +## Can other files in `awesome` package see `counter` name? +1. Yes +2. No: It's in the package scope +3. No: It's in the file scope +4. No: It's in the block scope of block() **CORRECT** + +> 4. That's right. None of the other code can see the names inside the `block()` function. Only the code inside the `block()` function can see them (Only to some extend. For example: Inside the block, the code can only see the variables declared before it.) + + +## Can other files in `awesome` package see `fmt` name? +1. Yes +2. No: It's in the package scope +3. No: It's in the file scope **CORRECT** +4. No: It's in the block scope of block() + +> 3. Only the same file can see the imported packages, not the other files whether they're in the same package or not. + + +## What happens if you declare the same name in the same scope like this: +```go +package awesome + +import "fmt" + +// declared twice in the package scope +var enabled bool +var enabled bool + +func block() { + var counter int + fmt.Println(counter) +} +``` +1. The newly declared name will override the previous one. +2. I can't do that. It's already been declared at the package scope. *CORRECT* +3. I can't do that. It's already been declared at the file scope. + +> 2. That's right. You can't declare the same name in the same scope. If you could do so, then how would you access it again? Or to which one? + + +## What happens if you declare the same name in another scope like this: +```go +package awesome + +import "fmt" + +// declared at the package scope +var enabled bool + +func block() { + // also declared in the block scope + var enabled bool + + var counter int + fmt.Println(counter) +} +``` +1. The newly declared name will override the previous one. *CORRECT* +2. I can't do that. It's already been declared at the package scope. +3. I can't do that. It's already been declared at the file scope. + +> 1. Actually, you can declare the same name in the inner scopes like this. `block()`'s scope is inside its package. This means that it can access to its package's scope (but not vice versa). So, `block()`'s scope is under its package's scope. This means that you can declare the same name again. It will override the parent scope's name. They both can be exist together. Check out the example in the course repository to find out. \ No newline at end of file diff --git a/04-statements-expressions-comments/01-statements/01-execution-flow/main.go b/04-statements-expressions-comments/01-statements/01-execution-flow/main.go new file mode 100644 index 0000000..efa800c --- /dev/null +++ b/04-statements-expressions-comments/01-statements/01-execution-flow/main.go @@ -0,0 +1,22 @@ +// 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() { + fmt.Println("Hello!") + + // Statements change the execution flow + // Especially the control flow statements like `if` + if 5 > 1 { + fmt.Println("bigger") + } +} diff --git a/04-statements-expressions-comments/01-statements/02-semicolons/main.go b/04-statements-expressions-comments/01-statements/02-semicolons/main.go new file mode 100644 index 0000000..605765b --- /dev/null +++ b/04-statements-expressions-comments/01-statements/02-semicolons/main.go @@ -0,0 +1,16 @@ +// 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() { + fmt.Println("Hello"); fmt.Println("World!") +} diff --git a/04-statements-expressions-comments/02-expressions/01-operator/main.go b/04-statements-expressions-comments/02-expressions/01-operator/main.go new file mode 100644 index 0000000..891740b --- /dev/null +++ b/04-statements-expressions-comments/02-expressions/01-operator/main.go @@ -0,0 +1,16 @@ +// 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() { + fmt.Println("Hello!" + "!") +} diff --git a/04-statements-expressions-comments/02-expressions/02-call-expression/main.go b/04-statements-expressions-comments/02-expressions/02-call-expression/main.go new file mode 100644 index 0000000..98b454d --- /dev/null +++ b/04-statements-expressions-comments/02-expressions/02-call-expression/main.go @@ -0,0 +1,18 @@ +// 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" + "runtime" +) + +func main() { + // runtime.NumCPU() is a call expression + fmt.Println(runtime.NumCPU() + 1) +} diff --git a/04-statements-expressions-comments/03-comments/main.go b/04-statements-expressions-comments/03-comments/main.go new file mode 100644 index 0000000..6758349 --- /dev/null +++ b/04-statements-expressions-comments/03-comments/main.go @@ -0,0 +1,21 @@ +// 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 makes this package an executable program +package main + +import "fmt" + +/* +main function +Go executes this program using this function. +There should be only one main file in a main package. +Executable programs are also called as "commands". +*/ +func main() { + fmt.Println("Hello Gopher!") +} diff --git a/04-statements-expressions-comments/exercises/01/main.go b/04-statements-expressions-comments/exercises/01/main.go new file mode 100644 index 0000000..e96ea8a --- /dev/null +++ b/04-statements-expressions-comments/exercises/01/main.go @@ -0,0 +1,19 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Greet a few people +// 2. Try to type your statements separating them using +// semicolons +// 3. Observe how Go fixes them for you +// --------------------------------------------------------- + +func main() { +} diff --git a/04-statements-expressions-comments/exercises/01/solution/main.go b/04-statements-expressions-comments/exercises/01/solution/main.go new file mode 100644 index 0000000..f357e5b --- /dev/null +++ b/04-statements-expressions-comments/exercises/01/solution/main.go @@ -0,0 +1,21 @@ +// 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 + +func main() { + // uncomment the below line of code; then save the file + // + // you will see that Go will fix it automatically + // + // this is because, for Go, it doesn't matter whether + // you use semicolons between statements or not. + // + // it adds them automatically after all. + + // fmt.Println("inanc"); fmt.Println("lina"); fmt.Println("ebru"); +} diff --git a/04-statements-expressions-comments/exercises/02/main.go b/04-statements-expressions-comments/exercises/02/main.go new file mode 100644 index 0000000..57a1cf1 --- /dev/null +++ b/04-statements-expressions-comments/exercises/02/main.go @@ -0,0 +1,19 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Try to type just "Hello" on a line. +// Do not use Println +// Observe the error +// --------------------------------------------------------- + +func main() { + // ? +} diff --git a/04-statements-expressions-comments/exercises/02/solution/main.go b/04-statements-expressions-comments/exercises/02/solution/main.go new file mode 100644 index 0000000..3b3ed44 --- /dev/null +++ b/04-statements-expressions-comments/exercises/02/solution/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 + +func main() { + // uncomment to see the error + + // "Hello" + + // It says "evaluted but not used" because + // the "Hello" expression returned a value + // and no statement has used it + + // You can't use expressions without statements +} diff --git a/04-statements-expressions-comments/exercises/03/main.go b/04-statements-expressions-comments/exercises/03/main.go new file mode 100644 index 0000000..745645a --- /dev/null +++ b/04-statements-expressions-comments/exercises/03/main.go @@ -0,0 +1,21 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print the expected output using operators +// +// EXPECTED OUTPUT +// "Hello!!!?" +// --------------------------------------------------------- + +func main() { + // use + operator below multiple times + // fmt.Println("Hello!" + ?) +} diff --git a/04-statements-expressions-comments/exercises/03/solution/main.go b/04-statements-expressions-comments/exercises/03/solution/main.go new file mode 100644 index 0000000..5de119e --- /dev/null +++ b/04-statements-expressions-comments/exercises/03/solution/main.go @@ -0,0 +1,16 @@ +// 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() { + // Operators bind multiple expressions together + // as if there's a single expression + fmt.Println("Hello!" + "!" + "!" + "?") +} diff --git a/04-statements-expressions-comments/exercises/04/main.go b/04-statements-expressions-comments/exercises/04/main.go new file mode 100644 index 0000000..2b9a614 --- /dev/null +++ b/04-statements-expressions-comments/exercises/04/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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Look at runtime package documentation +// 2- Find the func that returns the Go version +// 3- Print the Go version by calling that func +// +// HINT +// It's here: https://golang.org/pkg/runtime +// +// EXPECTED OUTPUT +// "go1.10" +// --------------------------------------------------------- + +func main() { + // ? +} diff --git a/04-statements-expressions-comments/exercises/04/solution/main.go b/04-statements-expressions-comments/exercises/04/solution/main.go new file mode 100644 index 0000000..899a90f --- /dev/null +++ b/04-statements-expressions-comments/exercises/04/solution/main.go @@ -0,0 +1,17 @@ +// 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" + "runtime" +) + +func main() { + fmt.Println(runtime.Version()) +} diff --git a/04-statements-expressions-comments/exercises/05/main.go b/04-statements-expressions-comments/exercises/05/main.go new file mode 100644 index 0000000..9282961 --- /dev/null +++ b/04-statements-expressions-comments/exercises/05/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" + +// --------------------------------------------------------- +// EXERCISE +// Use single and multiline comments to comment Printlns +// +// EXPECTED OUTPUT +// None +// --------------------------------------------------------- + +func main() { + fmt.Println("hello") + fmt.Println("how") + fmt.Println("are") + fmt.Println("you") +} diff --git a/04-statements-expressions-comments/exercises/05/solution/main.go b/04-statements-expressions-comments/exercises/05/solution/main.go new file mode 100644 index 0000000..5b76ed5 --- /dev/null +++ b/04-statements-expressions-comments/exercises/05/solution/main.go @@ -0,0 +1,17 @@ +// 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 + +func main() { + // fmt.Println("hello") + /* + fmt.Println("how") + fmt.Println("are") + fmt.Println("you") + */ +} diff --git a/04-statements-expressions-comments/exercises/06/exercise.md b/04-statements-expressions-comments/exercises/06/exercise.md new file mode 100644 index 0000000..78b6eb2 --- /dev/null +++ b/04-statements-expressions-comments/exercises/06/exercise.md @@ -0,0 +1,10 @@ +// --------------------------------------------------------- +// EXERCISE +// 1- Print the documentation of runtime.NumCPU function +// in the command line +// +// 2- Print also its source code using in the command line +// +// HINT +// You should use correct go doc tools +// --------------------------------------------------------- diff --git a/04-statements-expressions-comments/exercises/06/solution/solution.md b/04-statements-expressions-comments/exercises/06/solution/solution.md new file mode 100644 index 0000000..bd2fb64 --- /dev/null +++ b/04-statements-expressions-comments/exercises/06/solution/solution.md @@ -0,0 +1,7 @@ +## DOCUMENTATION: + + go doc runtime NumCPU + +## SOURCE CODE: + + godoc -src runtime NumCPU diff --git a/04-statements-expressions-comments/questions/01-statements.md b/04-statements-expressions-comments/questions/01-statements.md new file mode 100644 index 0000000..0c42e73 --- /dev/null +++ b/04-statements-expressions-comments/questions/01-statements.md @@ -0,0 +1,93 @@ +## Which one is the correct description for a statement? +1. A statement instructs Go to do something *CORRECT* +2. A statement produces a value +3. A statement can't change the execution flow + +> 2. A statement can't produce a value. However, it can indirectly help producing a value. +> 3. It surely can. + + +## What's the direction of execution in a Go code? +1. From left to right +2. From top to bottom *CORRECT* +3. From right to left +4. From bottom to top + +> 2. That's right. Go executes the code from top-to-bottom, one statement at a time. + + +## Which one is the correct description for an expression? +1. An expression instructs Go to do something +2. An expression produces a value *CORRECT* +3. An expression can change the execution flow + +> 1. It can't. Only a statement can do that. +> 3. It can't. Only a statement can do that. + + +## Which one is the correct description for an operator? +1. An operator instructs Go to do something +2. An operator can change the execution flow +3. An operator can combine expressions *CORRECT* + +> 1. It can't. Only a statement can do that. +> 2. It can't. Only a statement can do that. + + +## Why the following code doesn't work? +```go +package main +import "fmt" + +func main() { + "Hello" +} +``` + +1. "Hello" is an expression and it can't be on its own on a single line of code without a statement. *CORRECT* +2. By removing the double-quotes surrounding the "Hello". Like this: Hello +3. By moving "Hello" out of the func main. + + +## Does the following code works? And why? +```go +package main +import ( + "fmt" + "runtime" +) + +func main() { + fmt.Println(runtime.NumCPU()); fmt.Println("cpus"); fmt.Println("the machine") +} +``` + +1. It works: Expressions can be typed by separating them using semicolons +2. It doesn't work: Statements should be on their own on a single line of code +3. It works: Go adds semicolons behind the scenes for every statement already *CORRECT* + +> 1. It works but that's not the reason. And, expressions can't be typed like that. +> 2. Are you sure? +> 3. That's right. Whether there's a semicolon or not; Go adds them automatically. Those statements are still assumed as they're on a different code line of their own. + + +## Why this code works? +```go +package main +import ( + "fmt" + "runtime" +) + +func main() { + fmt.Println(runtime.NumCPU() + 10) +} +``` + +1. Operators can combine expressions *CORRECT* +2. Statements can be used with operators +3. Expressions can return multiple values + +> 1. That's right. + operator combines `runtime.NumCPU()` and `10` expressions. +> 2. No, they can't be. For example, you can't do this: `import "fmt" + 3`. Some statement can allow expressions. However, this doesn't mean that they can be combined using expressions. +> 3. That's right however it's irrelevant to why this code works. \ No newline at end of file diff --git a/04-statements-expressions-comments/questions/02-expressions.md b/04-statements-expressions-comments/questions/02-expressions.md new file mode 100644 index 0000000..7be2986 --- /dev/null +++ b/04-statements-expressions-comments/questions/02-expressions.md @@ -0,0 +1 @@ +## Please check out the questions inside the statements directory. \ No newline at end of file diff --git a/04-statements-expressions-comments/questions/03-comments.md b/04-statements-expressions-comments/questions/03-comments.md new file mode 100644 index 0000000..aa67d06 --- /dev/null +++ b/04-statements-expressions-comments/questions/03-comments.md @@ -0,0 +1,68 @@ +## Why do you need to use comments sometimes? +1. To combine different expressions together +2. To provide explanations or generating automatic documentation for your code *CORRECT* +3. To make the code look nice and beautiful + + +## Which of the following code is correct? +1. +```go +package main + +/ main function is an entry point / +func main() { + fmt.Println("Hi") +} +``` + +2. *CORRECT* +```go +package main + +// main function is an entry point /* +func main() { + fmt.Println(/* this will print Hi! */ "Hi") +} +``` + +3. +```go +package main + +/* +main function is an entry point + +It allows Go to find where to start executing an executable program. +*/ +func main() { + fmt.Println(// "this will print Hi!") +} +``` + +> 1. `/` is not a comment. It should be `//`. +> 2. Multiline comments can be put almost anywhere. However, when a comment starts with `/*`, it also needs to end with `*/`. Here, Go doesn't interpret `/* ... */`, it just skips it. And, when Go sees `//` as the first two characters in a code, it skips the whole line. +> 3. `//` prevents Go to interpret the rest of the code line. That's why this code doesn't work. Go can't interpret this part because of the comment: `"this will print Hi!")` + +## How should you name your code so that Go can generate documentation from your code automatically? +1. By commenting the each line of the code; then it will generate the documentation from whatever it sees +2. By starting the comments using the name of the declared names *CORRECT* +3. By using multi-line comments + +> 1. This won't help. Sorry. +> 3. It doesn't matter whether you type your comments using single-line comments or multi-line comments. + + +## Which tool do you need to use from the command-line to print the documentation? +1. go build +2. go run +3. go doctor +4. go doc + + +## What's the difference between `godoc` and `go doc`? +1. `go doc` is the real tool behind `godoc` +2. `godoc` is the real tool behind `go doc` *CORRECT* +3. `go` tool is the real tool behind `go doc` +4. `go` tool is the real tool behind `godoc` + +> 2. That's right. go doc tool uses godoc tool behind the scenes. go doc is just a simplified version of the godoc tool. \ No newline at end of file diff --git a/05-write-your-first-library-package/printer-exercise/exercise.md b/05-write-your-first-library-package/printer-exercise/exercise.md new file mode 100644 index 0000000..4e41fec --- /dev/null +++ b/05-write-your-first-library-package/printer-exercise/exercise.md @@ -0,0 +1,18 @@ +# EXERCISE +1. Create a new library +2. In it, create a function that returns Go version +3. Create a command and import your library +4. Call your function that returns Go version +5. Run your program + +## HINTS +**Create your package function like this:** + +```go +func Version() string { + return runtime.Version() +} +``` + +## EXPECTED OUTPUT +It should print the current Go version on your system. \ No newline at end of file diff --git a/05-write-your-first-library-package/printer-exercise/questions.md b/05-write-your-first-library-package/printer-exercise/questions.md new file mode 100644 index 0000000..92135dc --- /dev/null +++ b/05-write-your-first-library-package/printer-exercise/questions.md @@ -0,0 +1,90 @@ +## Which one below is correct? +**NOTE** _There are explanations inside the answers. Even if you know the answer please try to select all of them one by one, so you can read the explanations._ + +1. You can run a library package. +2. In a library package there should be a function named main (func main). +3. You can compile a library package. *CORRECT* +4. You have to compile a library package. + +> 1. You can't, but you can import it from other packages. +> 2. In a library package, you don't have to include a main function. Because, it isn't an executable package. Only in executable packages you need a main func. +> 4. You don't have to compile it. When you import it, it will automatically be built by the other program or library when it gets compiled or ran. + + +## What do you need to export a name? +1. You need to type it in all capital letters +2. You need to type its first letter as a capital letter *CORRECT* +3. You need to put it inside a function scope +4. You need to create a new file for that name + +> 1. When you do so, it will be exported, that's true, but don't do that; so I assume that this answer is not correct :) +> 2. That's right. Then the other packages can access it. +> 3. It should be in a package scope, not function scope. +> 4. You don't have to do that. + + +## How can you use a function from your library from an executable program? +1. You need to export your library package first; then you can access to its imported names +2. You need to import your library package first; then you can access to its exported names *CORRECT* +3. You can access your library package as if it's in your executable program +4. You can import it just by using its name + +> 1. You can't export packages. All packages are already exported. Unless you put them in a directory called: "internal". But, that's an advanced topic for now. +> 2. That's right. +> 3. You can't access to a package from another package without importing it. +> 4. No, you can't. You need to import it using its full directory path after GOPATH. BTW, in the near future this may change with the Go modules support. + + +## In the following program, which names are exported? +```go +package wizard + +import "fmt" + +func doMagic() { + fmt.Println("enchanted!") +} + +func Fireball() { + fmt.Println("fireball!!!") +} +``` + +1. fmt +2. doMagic +3. Fireball *CORRECT* +4. Println + +> 1. That's just an imported package name. +> 2. It starts with a lowercase letter; so, it's not exported. +> 3. That's right. It starts with a capital letter. +> 4. This isn't your function. It's already been exported in the fmt package. But, it isn't exported here. + + +## In the following program, which names are exported? +```go +package wizard +import "fmt" + +var one string +var Two string +var greenTrees string + +func doMagic() { + fmt.Println("enchanted!") +} + +func Fireball() { + fmt.Println("fireball!!!") +} +``` + +1. doMagic and Fireball +2. Fireball and Two *CORRECT* +3. Fireball, greenTrees and Two +4. Fireball, greenTrees, one and Two + +> 1. doMagic starts with a lowercase letter; so, it's not exported. +> 2. That's right. Fireball and Two, they both start with a capital letter. +> 3. greenTrees starts with a lowercase letter; so, it's not exported. +> 4. one and greenTrees do not start with capital letters; so, they're not exported. \ No newline at end of file diff --git a/05-write-your-first-library-package/printer-exercise/solution/golang/cmd/main.go b/05-write-your-first-library-package/printer-exercise/solution/golang/cmd/main.go new file mode 100644 index 0000000..3679ee8 --- /dev/null +++ b/05-write-your-first-library-package/printer-exercise/solution/golang/cmd/main.go @@ -0,0 +1,18 @@ +// 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" // You should replace this with your username + + "github.com/inancgumus/learngo/05-write-your-first-library-package/printer-exercise/solution/golang" +) + +func main() { + fmt.Println(golang.Version()) +} diff --git a/05-write-your-first-library-package/printer-exercise/solution/golang/go.go b/05-write-your-first-library-package/printer-exercise/solution/golang/go.go new file mode 100644 index 0000000..360ffbb --- /dev/null +++ b/05-write-your-first-library-package/printer-exercise/solution/golang/go.go @@ -0,0 +1,17 @@ +// 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 golang + +import ( + "runtime" +) + +// Version returns the current Go version +func Version() string { + return runtime.Version() +} diff --git a/05-write-your-first-library-package/printer/cmd/main.go b/05-write-your-first-library-package/printer/cmd/main.go new file mode 100644 index 0000000..bfa61ad --- /dev/null +++ b/05-write-your-first-library-package/printer/cmd/main.go @@ -0,0 +1,15 @@ +// 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 + +// Automatically imports!... AWESOME! +import "github.com/inancgumus/learngo/05-write-your-first-library-package/printer" + +func main() { + printer.Hello() +} diff --git a/05-write-your-first-library-package/printer/printer.go b/05-write-your-first-library-package/printer/printer.go new file mode 100644 index 0000000..229213d --- /dev/null +++ b/05-write-your-first-library-package/printer/printer.go @@ -0,0 +1,15 @@ +// 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 printer + +import "fmt" + +// Hello is an exported function +func Hello() { + fmt.Println("exported hello") +} diff --git a/06-variables/01-basic-data-types/exercises/01/main.go b/06-variables/01-basic-data-types/exercises/01/main.go new file mode 100644 index 0000000..21963cf --- /dev/null +++ b/06-variables/01-basic-data-types/exercises/01/main.go @@ -0,0 +1,21 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Print a few integer literals +// 2- Print a few float literals +// 3- Print true and false bool literals +// 4- Print your name using a string literal +// 5- Print a non-english sentence using a string literal +// --------------------------------------------------------- + +func main() { + // Use fmt.Println() +} diff --git a/06-variables/01-basic-data-types/exercises/01/solution/main.go b/06-variables/01-basic-data-types/exercises/01/solution/main.go new file mode 100644 index 0000000..782e2a8 --- /dev/null +++ b/06-variables/01-basic-data-types/exercises/01/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + fmt.Println(42, 8500, 344433, -2323) + fmt.Println(3.14, 6.28, -42.) + fmt.Println(true, false) + fmt.Println("Hi! I'm Inanc!") + fmt.Println("Merhaba, adım İnanç!") +} diff --git a/06-variables/01-basic-data-types/exercises/02/main.go b/06-variables/01-basic-data-types/exercises/02/main.go new file mode 100644 index 0000000..387f8ca --- /dev/null +++ b/06-variables/01-basic-data-types/exercises/02/main.go @@ -0,0 +1,61 @@ +// 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" + +// THIS EXERCISE IS OPTIONAL + +// --------------------------------------------------------- +// EXERCISE +// 1- Print 0 to 9 in hexadecimal +// 2- Print 10 to 15 in hexadecimal +// 3- Print 17 in hexadecimal +// 4- Print 25 in hexadecimal +// 5- Print 50 in hexadecimal +// 6- Print 100 in hexadecimal +// +// NOTES +// https://stackoverflow.com/questions/910309/how-to-turn-hexadecimal-into-decimal-using-brain +// +// https://simple.wikipedia.org/wiki/Hexadecimal_numeral_system +// +// EXPECTED OUTPUT +// 0 1 2 3 4 5 6 7 8 9 +// 10 11 12 13 14 15 +// 17 +// 25 +// 50 +// 100 +// --------------------------------------------------------- + +func main() { + // EXAMPLES: + + // I'm going to print 10 in hexadecimal + fmt.Println(0xa) + + // I'm going to print 16 in hexadecimal + // 0x10 + // ^^----- 1 * 0 = 0 + // | + // +------ 16 * 1 = 16 + // = 16 + fmt.Println(0x10) + + // I'm going to print 150 in hexadecimal + // 0x96 + // ^^----- 1 * 6 = 6 + // | + // +------ 16 * 9 = 144 + // = 150 + fmt.Println(0x96) + + // COMMENT-OUT ALL THE CODE ABOVE, THEN, + // ADD YOUR OWN SOLUTIONS BELOW +} diff --git a/06-variables/01-basic-data-types/exercises/02/solution/main.go b/06-variables/01-basic-data-types/exercises/02/solution/main.go new file mode 100644 index 0000000..8fa17e8 --- /dev/null +++ b/06-variables/01-basic-data-types/exercises/02/solution/main.go @@ -0,0 +1,19 @@ +// 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() { + fmt.Println(0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9) + fmt.Println(0xa, 0xb, 0xc, 0xd, 0xe, 0xf) + fmt.Println(0x11) // 17 + fmt.Println(0x19) // 25 + fmt.Println(0x32) // 50 + fmt.Println(0x64) // 100 +} diff --git a/06-variables/01-basic-data-types/main.go b/06-variables/01-basic-data-types/main.go new file mode 100644 index 0000000..81cae06 --- /dev/null +++ b/06-variables/01-basic-data-types/main.go @@ -0,0 +1,37 @@ +// 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() { + // integer literal + fmt.Println( + -200, -100, 0, 50, 100, 100, + ) + + // float literal + fmt.Println( + -50.5, -20.5, -0., 1., 100.56, // ... + ) + + // bool constants + fmt.Println( + true, false, + ) + + // string literal - utf-8 + fmt.Println( + "", // empty - prints just a space + "hi", + + // unicode + "nasılsın?", // "how are you" in turkish + "hi there 星!", // "hi there star!" + ) +} diff --git a/06-variables/01-basic-data-types/questions/questions.md b/06-variables/01-basic-data-types/questions/questions.md new file mode 100644 index 0000000..2becf70 --- /dev/null +++ b/06-variables/01-basic-data-types/questions/questions.md @@ -0,0 +1,33 @@ +## Which one below is an integer literal? +* -42 *CORRECT* + * This is an integer literal +* "Hello" + * This is a string literal +* false + * This is a bool constant +* 3.14 + * This is a float literal + +## Which one below is a float literal? +* -42 +* "Hello" +* false +* 3.14 *CORRECT* + +## Which one below is a float literal? +* 6,28 +* ,28 +* .28 *CORRECT* +* 628 + +## Which one below is a string literal? +* -42 +* "Hello" *CORRECT* +* false +* 3.14 + +## Which one below is a bool constant? +* -42 +* "Hello" +* false *CORRECT* +* 3.14 diff --git a/06-variables/02-declarations/01-declaration-syntax/01-syntax/main.go b/06-variables/02-declarations/01-declaration-syntax/01-syntax/main.go new file mode 100644 index 0000000..8058b70 --- /dev/null +++ b/06-variables/02-declarations/01-declaration-syntax/01-syntax/main.go @@ -0,0 +1,16 @@ +// 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() { + var speed int + + fmt.Println(speed) +} diff --git a/06-variables/02-declarations/01-declaration-syntax/02-naming-rules/main.go b/06-variables/02-declarations/01-declaration-syntax/02-naming-rules/main.go new file mode 100644 index 0000000..13a847b --- /dev/null +++ b/06-variables/02-declarations/01-declaration-syntax/02-naming-rules/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 + +// VARIABLE NAMING RULES + +func main() { + // CORRECT DECLARATIONS + var speed int + var SpeeD int + + // underscore is allowed but not recommended + var _speed int + + // unicode letters are allowed + var 速度 int + + // keep the compiler happy + _, _, _, _ = speed, SpeeD, _speed, 速度 +} diff --git a/06-variables/02-declarations/01-declaration-syntax/03-order-of-declaration/main.go b/06-variables/02-declarations/01-declaration-syntax/03-order-of-declaration/main.go new file mode 100644 index 0000000..df02432 --- /dev/null +++ b/06-variables/02-declarations/01-declaration-syntax/03-order-of-declaration/main.go @@ -0,0 +1,13 @@ +// 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 + +func main() { + // fmt.Println(speed) + // var speed int +} diff --git a/06-variables/02-declarations/02-example-declarations/01-int/main.go b/06-variables/02-declarations/02-example-declarations/01-int/main.go new file mode 100644 index 0000000..42645d3 --- /dev/null +++ b/06-variables/02-declarations/02-example-declarations/01-int/main.go @@ -0,0 +1,22 @@ +// 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() { + var nFiles int + var counter int + var nCPU int + + fmt.Println( + nFiles, + counter, + nCPU, + ) +} diff --git a/06-variables/02-declarations/02-example-declarations/02-float64/main.go b/06-variables/02-declarations/02-example-declarations/02-float64/main.go new file mode 100644 index 0000000..f8e59d7 --- /dev/null +++ b/06-variables/02-declarations/02-example-declarations/02-float64/main.go @@ -0,0 +1,22 @@ +// 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() { + var heat float64 + var ratio float64 + var degree float64 + + fmt.Println( + heat, + ratio, + degree, + ) +} diff --git a/06-variables/02-declarations/02-example-declarations/03-bool/main.go b/06-variables/02-declarations/02-example-declarations/03-bool/main.go new file mode 100644 index 0000000..ba66356 --- /dev/null +++ b/06-variables/02-declarations/02-example-declarations/03-bool/main.go @@ -0,0 +1,22 @@ +// 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() { + var off bool + var valid bool + var closed bool + + fmt.Println( + off, + valid, + closed, + ) +} diff --git a/06-variables/02-declarations/02-example-declarations/04-string/main.go b/06-variables/02-declarations/02-example-declarations/04-string/main.go new file mode 100644 index 0000000..7435ecf --- /dev/null +++ b/06-variables/02-declarations/02-example-declarations/04-string/main.go @@ -0,0 +1,22 @@ +// 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() { + var msg string + var name string + var text string + + fmt.Println( + msg, + name, + text, + ) +} diff --git a/06-variables/02-declarations/03-zero-values/main.go b/06-variables/02-declarations/03-zero-values/main.go new file mode 100644 index 0000000..3d6cabd --- /dev/null +++ b/06-variables/02-declarations/03-zero-values/main.go @@ -0,0 +1,27 @@ +// 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" + +// EXERCISE: Let's run this to see the zero-values yourself + +func main() { + var speed int // numeric type + var heat float64 // numeric type + var off bool + var brand string + + fmt.Println(speed) + fmt.Println(heat) + fmt.Println(off) + + // I've used printf to print an empty string + // EXERCISE: Use Println to see what happens + fmt.Printf("%q\n", brand) +} diff --git a/06-variables/02-declarations/04-unused-variables-and-blank-identifier/01-unused-variable/main.go b/06-variables/02-declarations/04-unused-variables-and-blank-identifier/01-unused-variable/main.go new file mode 100644 index 0000000..3f67f35 --- /dev/null +++ b/06-variables/02-declarations/04-unused-variables-and-blank-identifier/01-unused-variable/main.go @@ -0,0 +1,19 @@ +// 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 + +// there's no warning for package-level vars +var packageLevelVar string + +func main() { + // unused variable error + // var speed int + + // if you use it, the error will be gone + // fmt.Println(speed) +} diff --git a/06-variables/02-declarations/04-unused-variables-and-blank-identifier/02-blank-identifier/main.go b/06-variables/02-declarations/04-unused-variables-and-blank-identifier/02-blank-identifier/main.go new file mode 100644 index 0000000..9909ae9 --- /dev/null +++ b/06-variables/02-declarations/04-unused-variables-and-blank-identifier/02-blank-identifier/main.go @@ -0,0 +1,16 @@ +// 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 + +func main() { + var speed int + + // let's assign the variable to the blank-identifier + // so that Go compiler won't get grumpy + _ = speed +} diff --git a/06-variables/02-declarations/05-multiple-declarations/01-multiple/main.go b/06-variables/02-declarations/05-multiple-declarations/01-multiple/main.go new file mode 100644 index 0000000..7d29158 --- /dev/null +++ b/06-variables/02-declarations/05-multiple-declarations/01-multiple/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" + +func main() { + var ( + speed int + heat float64 + + off bool + brand string + ) + + fmt.Println(speed) + fmt.Println(heat) + fmt.Println(off) + fmt.Printf("%q\n", brand) +} diff --git a/06-variables/02-declarations/05-multiple-declarations/02-parallel/main.go b/06-variables/02-declarations/05-multiple-declarations/02-parallel/main.go new file mode 100644 index 0000000..41e50b4 --- /dev/null +++ b/06-variables/02-declarations/05-multiple-declarations/02-parallel/main.go @@ -0,0 +1,28 @@ +// 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() { + // this is equal to: + // + // var ( + // speed int + // velocity int + // ) + // + // or: + // + // var speed int + // var velocity int + // + var speed, velocity int + + fmt.Println(speed, velocity) +} diff --git a/06-variables/02-declarations/06-examples/main.go b/06-variables/02-declarations/06-examples/main.go new file mode 100644 index 0000000..6496cad --- /dev/null +++ b/06-variables/02-declarations/06-examples/main.go @@ -0,0 +1,41 @@ +// 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() { + // names are case-sensitive: + // MyAge, myAge, and MYAGE are different variables + + // USE-CASE: + // When to use a parallel declaration? + // + // NOT GOOD: + // var myAge int + // var yourAge int + // + // SO-SO: + // var ( + // myAge int + // yourAge int + // ) + // + // BETTER: + var myAge, yourAge int + fmt.Println(myAge, yourAge) + + var temperature float64 + fmt.Println(temperature) + + var success bool + fmt.Println(success) + + var language string + fmt.Println(language) +} diff --git a/06-variables/02-declarations/exercises/01/main.go b/06-variables/02-declarations/exercises/01/main.go new file mode 100644 index 0000000..f15c65b --- /dev/null +++ b/06-variables/02-declarations/exercises/01/main.go @@ -0,0 +1,22 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Declare and print a variable with an int type +// 2- The declared variable's name should be: height +// +// EXPECTED OUTPUT +// 0 +// --------------------------------------------------------- + +func main() { + // var ? ? + // ? +} diff --git a/06-variables/02-declarations/exercises/01/solution/main.go b/06-variables/02-declarations/exercises/01/solution/main.go new file mode 100644 index 0000000..b5612b1 --- /dev/null +++ b/06-variables/02-declarations/exercises/01/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + var height int + + fmt.Println(height) +} diff --git a/06-variables/02-declarations/exercises/02/main.go b/06-variables/02-declarations/exercises/02/main.go new file mode 100644 index 0000000..cef7ed6 --- /dev/null +++ b/06-variables/02-declarations/exercises/02/main.go @@ -0,0 +1,22 @@ +// 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 + +// --------------------------------------------------------- +// OPTIONAL EXERCISE +// 1- Declare and print a bool variable +// 2- The variable's name should be: isOn +// +// EXPECTED OUTPUT +// false +// --------------------------------------------------------- + +func main() { + // var ? ? + // ? +} diff --git a/06-variables/02-declarations/exercises/02/solution/main.go b/06-variables/02-declarations/exercises/02/solution/main.go new file mode 100644 index 0000000..ebd2eb1 --- /dev/null +++ b/06-variables/02-declarations/exercises/02/solution/main.go @@ -0,0 +1,17 @@ +// 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() { + var isOn bool + fmt.Println(isOn) +} diff --git a/06-variables/02-declarations/exercises/03/main.go b/06-variables/02-declarations/exercises/03/main.go new file mode 100644 index 0000000..e8164fa --- /dev/null +++ b/06-variables/02-declarations/exercises/03/main.go @@ -0,0 +1,22 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Declare and print a variable with a float64 type +// 2- The declared variable's name should be: brightness +// +// EXPECTED OUTPUT +// 0 +// --------------------------------------------------------- + +func main() { + // var ? ? + // ? +} diff --git a/06-variables/02-declarations/exercises/03/solution/main.go b/06-variables/02-declarations/exercises/03/solution/main.go new file mode 100644 index 0000000..552698f --- /dev/null +++ b/06-variables/02-declarations/exercises/03/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + var brightness int + + fmt.Println(brightness) +} diff --git a/06-variables/02-declarations/exercises/04/main.go b/06-variables/02-declarations/exercises/04/main.go new file mode 100644 index 0000000..47b200c --- /dev/null +++ b/06-variables/02-declarations/exercises/04/main.go @@ -0,0 +1,28 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Declare a string variable +// 2- Print that variable +// +// EXPECTED OUTPUT +// "" +// --------------------------------------------------------- + +func main() { + // USE THE BELOW CODE + // You'll learn about Printf later + + // var ? + // fmt.Printf("s (%T): %q\n", s, s) + + // %T prints the type of the value + // %q prints an empty string +} diff --git a/06-variables/02-declarations/exercises/04/solution/main.go b/06-variables/02-declarations/exercises/04/solution/main.go new file mode 100644 index 0000000..e61c87c --- /dev/null +++ b/06-variables/02-declarations/exercises/04/solution/main.go @@ -0,0 +1,15 @@ +// 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() { + var s string + fmt.Printf("s (%T): %q\n", s, s) +} diff --git a/06-variables/02-declarations/exercises/05/main.go b/06-variables/02-declarations/exercises/05/main.go new file mode 100644 index 0000000..e479b45 --- /dev/null +++ b/06-variables/02-declarations/exercises/05/main.go @@ -0,0 +1,33 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Declare the variables below: +// 3speed +// !speed +// spe?ed +// var +// func +// package +// +// 2- Observe the error messages +// +// NOTE +// The types of the variables are not important. +// --------------------------------------------------------- + +func main() { + // var ? int + // var ? int + // var ? int + // var ? int + // var ? int + // var ? int +} diff --git a/06-variables/02-declarations/exercises/05/solution/main.go b/06-variables/02-declarations/exercises/05/solution/main.go new file mode 100644 index 0000000..f76b2df --- /dev/null +++ b/06-variables/02-declarations/exercises/05/solution/main.go @@ -0,0 +1,17 @@ +// 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 + +func main() { + // var 3speed int + // var !speed int + // var spe?ed int + // var var int + // var func int + // var package int +} diff --git a/06-variables/02-declarations/exercises/06/main.go b/06-variables/02-declarations/exercises/06/main.go new file mode 100644 index 0000000..3f4772f --- /dev/null +++ b/06-variables/02-declarations/exercises/06/main.go @@ -0,0 +1,41 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Declare a few variables using the following types +// int +// int8 +// int16 +// int32 +// int64 +// float32 +// float64 +// complex64 +// complex128 +// bool +// string +// rune +// byte +// +// 2- Observe their output +// 3- After you've done, check out the solution +// and read the comments there +// +// EXPECTED OUTPUT +// 0 0 0 0 0 0 0 false 0 0 +// "" +// --------------------------------------------------------- + +func main() { + // var i int + // var i8 int8 + + // CONTINUE FROM HERE.... +} diff --git a/06-variables/02-declarations/exercises/06/solution/main.go b/06-variables/02-declarations/exercises/06/solution/main.go new file mode 100644 index 0000000..42ba29b --- /dev/null +++ b/06-variables/02-declarations/exercises/06/solution/main.go @@ -0,0 +1,40 @@ +// 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() { + // integer types + var i int + var i8 int8 + var i16 int16 + var i32 int32 + var i64 int64 + + // float types + var f32 float32 + var f64 float64 + + // bool type + var b bool + + // string types + var s string + var r rune // also a numeric type + var by byte // also a numeric type + + fmt.Println( + i, i8, i16, i32, i64, + f32, f64, + b, r, by, + ) + + // You could do it with Println as well + fmt.Printf("%q\n", s) +} diff --git a/06-variables/02-declarations/exercises/07/main.go b/06-variables/02-declarations/exercises/07/main.go new file mode 100644 index 0000000..d317979 --- /dev/null +++ b/06-variables/02-declarations/exercises/07/main.go @@ -0,0 +1,32 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Declare two variables using +// multiple variable declaration statement +// +// 2. The first variable's name should be active +// 3. The second variable's name should be delta +// +// 4. Print them all +// +// HINT +// You should declare a bool and an int variable +// +// EXPECTED OUTPUT +// false 0 +// --------------------------------------------------------- + +func main() { + // var ( + // ? + // ) + // fmt.Println(active, delta) +} diff --git a/06-variables/02-declarations/exercises/07/solution/main.go b/06-variables/02-declarations/exercises/07/solution/main.go new file mode 100644 index 0000000..6b207ed --- /dev/null +++ b/06-variables/02-declarations/exercises/07/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + var ( + active bool + delta int + ) + fmt.Println(active, delta) +} diff --git a/06-variables/02-declarations/exercises/08/main.go b/06-variables/02-declarations/exercises/08/main.go new file mode 100644 index 0000000..9a3a2db --- /dev/null +++ b/06-variables/02-declarations/exercises/08/main.go @@ -0,0 +1,34 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Declare and initialize two string variables +// using multiple variable declaration +// +// 2. Use the type once while declaring the variables +// +// 3. The first variable's name should be firstName +// 4. The second variable's name should be lastName +// +// 5. Print them all +// +// EXPECTED OUTPUT +// "" "" +// --------------------------------------------------------- + +func main() { + // ADD YOUR DECLARATION HERE + // + + // REPLACE THE QUESTION-MARKS BELOW + // WITH THE NAME OF YOUR VARIABLES + + // fmt.Printf("%q %q\n", ?, ?) +} diff --git a/06-variables/02-declarations/exercises/08/solution/main.go b/06-variables/02-declarations/exercises/08/solution/main.go new file mode 100644 index 0000000..73ae23d --- /dev/null +++ b/06-variables/02-declarations/exercises/08/solution/main.go @@ -0,0 +1,15 @@ +// 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() { + var firstName, lastName string = "", "" + fmt.Printf("%q %q\n", firstName, lastName) +} diff --git a/06-variables/02-declarations/exercises/09/main.go b/06-variables/02-declarations/exercises/09/main.go new file mode 100644 index 0000000..5568759 --- /dev/null +++ b/06-variables/02-declarations/exercises/09/main.go @@ -0,0 +1,21 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Declare a variable +// 2- Variable's name should be: isLiquid +// 3- Discard it using a blank-identifier +// +// NOTE +// Do not print the variable +// --------------------------------------------------------- + +func main() { +} diff --git a/06-variables/02-declarations/exercises/09/solution/main.go b/06-variables/02-declarations/exercises/09/solution/main.go new file mode 100644 index 0000000..9b012ac --- /dev/null +++ b/06-variables/02-declarations/exercises/09/solution/main.go @@ -0,0 +1,13 @@ +// 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 + +func main() { + var isLiquid bool + _ = isLiquid +} diff --git a/06-variables/02-declarations/exercises/10/main.go b/06-variables/02-declarations/exercises/10/main.go new file mode 100644 index 0000000..703a64a --- /dev/null +++ b/06-variables/02-declarations/exercises/10/main.go @@ -0,0 +1,19 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Declare a variable in the package-scope +// +// 2- Observe whether something happens when you don't +// use it +// --------------------------------------------------------- + +func main() { +} diff --git a/06-variables/02-declarations/exercises/10/solution/main.go b/06-variables/02-declarations/exercises/10/solution/main.go new file mode 100644 index 0000000..e1cbf54 --- /dev/null +++ b/06-variables/02-declarations/exercises/10/solution/main.go @@ -0,0 +1,13 @@ +// 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 + +var isLiquid bool + +func main() { +} diff --git a/06-variables/02-declarations/exercises/11/main.go b/06-variables/02-declarations/exercises/11/main.go new file mode 100644 index 0000000..ed7f52c --- /dev/null +++ b/06-variables/02-declarations/exercises/11/main.go @@ -0,0 +1,24 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Print a variable +// 2- Then declare it +// (This means: Try to print it before its declaration) +// 3- Observe the error +// --------------------------------------------------------- + +func main() { + // First print it: + // fmt.Println(?) + + // Then declare it: + // var ? ? +} diff --git a/06-variables/02-declarations/exercises/11/solution/main.go b/06-variables/02-declarations/exercises/11/solution/main.go new file mode 100644 index 0000000..ddc83cc --- /dev/null +++ b/06-variables/02-declarations/exercises/11/solution/main.go @@ -0,0 +1,15 @@ +// 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 + +func main() { + // UNCOMMENT THE CODE BELOW TO SEE THE ERROR + + // fmt.Println(age) + // var age int +} diff --git a/06-variables/02-declarations/questions/01-what.md b/06-variables/02-declarations/questions/01-what.md new file mode 100644 index 0000000..7fab952 --- /dev/null +++ b/06-variables/02-declarations/questions/01-what.md @@ -0,0 +1,22 @@ +# QUESTIONS: What's a variable? + +* **Where does a variable live?** +* Hard Disk +* Computer Memory - *CORRECT* +* CPU + +* **What do you use a variable's name for?** +* To be able to access it later - *CORRECT* +* It's just a label +* I don't need to use it + +* **How to change the value of a variable?** +* By using its name - *CORRECT* +* By using its value +* By asking to Go + +* **After its declaration can you change the variable's type?** +* Yes : Go is dynamically-typed +* No : Go is statically-typed - *CORRECT* +* Both: Go supports both of them + diff --git a/06-variables/02-declarations/questions/02-declaration.md b/06-variables/02-declarations/questions/02-declaration.md new file mode 100644 index 0000000..bac1a5e --- /dev/null +++ b/06-variables/02-declarations/questions/02-declaration.md @@ -0,0 +1,21 @@ +## Which statement do you need to use for declaring variables? +* name int +* vars string name +* var name integer +* var width int *CORRECT* + +## Which sentence below is correct? +* You can use a variable before declaring it +* You have to declare a variable before using it *CORRECT* + +## What kind of language is Go? +* Weakly-Typed +* Dynamically-Typed +* Strongly-Typed *CORRECT* +* Freely-Typed + +## Which variable name below is correct? +* int +* four *CORRECT* +* 2computers +* one?there \ No newline at end of file diff --git a/06-variables/02-declarations/questions/03-unused-variables.md b/06-variables/02-declarations/questions/03-unused-variables.md new file mode 100644 index 0000000..96b62b9 --- /dev/null +++ b/06-variables/02-declarations/questions/03-unused-variables.md @@ -0,0 +1,14 @@ +## What happens when you don't use a declared variable in the block scope? +* Nothing, it will work fine +* It will get removed automatically +* The program won't compile *CORRECT* + +## What happens when you don't use a declared variable in the package scope? +* Nothing, it will work fine *CORRECT* +* It will get removed automatically +* The program won't compile + +## How can you prevent unused variable error? +* You can change the variable's name +* You can use a blank-identifier to discard it *CORRECT* +* You can change the variable's type \ No newline at end of file diff --git a/06-variables/02-declarations/questions/04-zero-values.md b/06-variables/02-declarations/questions/04-zero-values.md new file mode 100644 index 0000000..0cde3f3 --- /dev/null +++ b/06-variables/02-declarations/questions/04-zero-values.md @@ -0,0 +1,23 @@ +## Which type's zero value is 0? +- bool +- pointer +- string +- all numeric types *CORRECT* + +## Which type's zero value is false? +- bool *CORRECT* +- pointer +- string +- all numeric types + +## Which type's zero value is ""? +- bool +- pointer +- string *CORRECT* +- all numeric types + +## Which type's zero value is nil? +- bool +- pointer *CORRECT* +- string +- all numeric types diff --git a/06-variables/03-short-declaration/01-initialization-and-short-declaration/01-initialization/main.go b/06-variables/03-short-declaration/01-initialization-and-short-declaration/01-initialization/main.go new file mode 100644 index 0000000..04e131c --- /dev/null +++ b/06-variables/03-short-declaration/01-initialization-and-short-declaration/01-initialization/main.go @@ -0,0 +1,26 @@ +// 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() { + // = is the assignment operator + // when used within a variable declaration, it + // initializes the variable to the given value + + // here, Go initializes the safe variable to true + + // OPTION #1 (option #2 is better) + // var safe bool = true + + // OPTION #2 + var safe = true + + fmt.Println(safe) +} diff --git a/06-variables/03-short-declaration/01-initialization-and-short-declaration/02-short-declaration/main.go b/06-variables/03-short-declaration/01-initialization-and-short-declaration/02-short-declaration/main.go new file mode 100644 index 0000000..e21f882 --- /dev/null +++ b/06-variables/03-short-declaration/01-initialization-and-short-declaration/02-short-declaration/main.go @@ -0,0 +1,34 @@ +// 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() { + // OPTION #1 (option #2 is better) + // var safe bool = true + + // OPTION #2 (OK) + // var safe = true + + // OPTION #3 - SHORT DECLARATION (BEST) + // + // You don't even need to type the `var` keyword + // + // Short declaration equals to: + // var safe bool = true + // var safe = true + // + // Go gets (infers) the type from the initializer value + // + // true's default type is bool + // so, the type of the safe variable becomes a bool + safe := true + + fmt.Println(safe) +} diff --git a/06-variables/03-short-declaration/01-initialization-and-short-declaration/03-coding-example/main.go b/06-variables/03-short-declaration/01-initialization-and-short-declaration/03-coding-example/main.go new file mode 100644 index 0000000..d4f6c61 --- /dev/null +++ b/06-variables/03-short-declaration/01-initialization-and-short-declaration/03-coding-example/main.go @@ -0,0 +1,39 @@ +// 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() { + // var name string = "Carl" + // var name = "Carl" + name := "Carl" + + // var isScientist bool = true + // var isScientist = true + isScientist := true + + // var age int = 62 + // var age = 62 + age := 62 + + // var degree float64 = 5. + // var degree = 5. + degree := 5. + + fmt.Println(name, isScientist, age, degree) + + // type inference also works for variables + // + // Go gets the type of the variable and assigns it + // to the newly declared variable + // + // The type of the name2 variable is `string` now + name2 := name + fmt.Println(name2) +} diff --git a/06-variables/03-short-declaration/02-package-scope/main.go b/06-variables/03-short-declaration/02-package-scope/main.go new file mode 100644 index 0000000..5d6ba4d --- /dev/null +++ b/06-variables/03-short-declaration/02-package-scope/main.go @@ -0,0 +1,27 @@ +// 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" + +// You can't use declaration statements without a keyword +// Short declaration doesn't have a keyword (`var`) +// So, it can't be used at the package scope +// +// SYNTAX ERROR: +// "non-declaration statement outside function body" + +// safe := true + +// However, you can use the normal declaration at the +// package scope. Since, it has a keyword: `var` +var safe = true + +func main() { + fmt.Println(safe) +} diff --git a/06-variables/03-short-declaration/03-multiple-short-declaration/01-declaration/main.go b/06-variables/03-short-declaration/03-multiple-short-declaration/01-declaration/main.go new file mode 100644 index 0000000..e76c30f --- /dev/null +++ b/06-variables/03-short-declaration/03-multiple-short-declaration/01-declaration/main.go @@ -0,0 +1,21 @@ +// 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() { + // the number of variables and values should be equal + + // -> `true` is being assigned to `safe` + // -> `50` is being assigned to `speed` + + safe, speed := true, 50 + + fmt.Println(safe, speed) +} diff --git a/06-variables/03-short-declaration/03-multiple-short-declaration/02-coding-example/main.go b/06-variables/03-short-declaration/03-multiple-short-declaration/02-coding-example/main.go new file mode 100644 index 0000000..1d23b12 --- /dev/null +++ b/06-variables/03-short-declaration/03-multiple-short-declaration/02-coding-example/main.go @@ -0,0 +1,32 @@ +// 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() { + name, lastname := "Nikola", "Tesla" + fmt.Println(name, lastname) + + birth, death := 1856, 1943 + fmt.Println(birth, death) + + on, off := true, false + fmt.Println(on, off) + + // there's no limit + // however, more declarations that you declare + // more unreadable it becomes... + degree, ratio, heat := 10.55, 30.5, 20. + fmt.Println(degree, ratio, heat) + + // you can short declare variables with different types + nFiles, valid, msg := 10, true, "hello" + + fmt.Println(nFiles, valid, msg) +} diff --git a/06-variables/03-short-declaration/03-multiple-short-declaration/03-redeclaration/01/main.go b/06-variables/03-short-declaration/03-multiple-short-declaration/03-redeclaration/01/main.go new file mode 100644 index 0000000..74fc95a --- /dev/null +++ b/06-variables/03-short-declaration/03-multiple-short-declaration/03-redeclaration/01/main.go @@ -0,0 +1,35 @@ +// 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() { + // `safe`'s value is `false` + var safe bool + + // `safe`'s value is now `true` + + // `speed` is declared and initialized to `50` + + // redeclaration only works when + // + // at least one of the variables + // should be a new variable + + safe, speed := true, 50 + + fmt.Println(safe, speed) + + // EXERCISE + // + // Declare the speed variable before + // the short declaration "again" + // + // Observe what happens +} diff --git a/06-variables/03-short-declaration/03-multiple-short-declaration/03-redeclaration/02-coding-example/main.go b/06-variables/03-short-declaration/03-multiple-short-declaration/03-redeclaration/02-coding-example/main.go new file mode 100644 index 0000000..e77a520 --- /dev/null +++ b/06-variables/03-short-declaration/03-multiple-short-declaration/03-redeclaration/02-coding-example/main.go @@ -0,0 +1,41 @@ +// 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() { + // EXAMPLE #1 + + name := "Nikola" + fmt.Println(name) + + // name already exists in this block + // name := "Marie" + + // just assigns new values to name + // and declares the new variable age with a value of 66 + name, age := "Marie", 66 + fmt.Println(name, age) + + // EXAMPLE #2 + + // name = "Albert" + // birth := 1879 + + // redeclaration below equals to the statements just above + // + // `name` is an existing variable + // Go just assigns "Albert" to the name variable + // + // `birth` is a new variable + // Go declares it and assigns it a value of `1879` + name, birth := "Albert", 1879 + + fmt.Println(name, birth) +} diff --git a/06-variables/03-short-declaration/04-short-vs-normal/01-declaration/main.go b/06-variables/03-short-declaration/04-short-vs-normal/01-declaration/main.go new file mode 100644 index 0000000..70716fb --- /dev/null +++ b/06-variables/03-short-declaration/04-short-vs-normal/01-declaration/main.go @@ -0,0 +1,41 @@ +// 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 + +// normal declaration use cases + +// ----------------------------------------------------- +// when you need a package scoped variable +// ----------------------------------------------------- + +// version := 0 // YOU CAN'T +var version int + +func main() { + + // ----------------------------------------------------- + // if you don't know the initial value + // ----------------------------------------------------- + + // DON'T DO THIS: + // score := 0 + + // DO THIS: + // var score int + + // ----------------------------------------------------- + // group variables for readability + // ----------------------------------------------------- + + // var ( + // video string + + // duration int + // current int + // ) +} diff --git a/06-variables/03-short-declaration/04-short-vs-normal/02-short-declaration/main.go b/06-variables/03-short-declaration/04-short-vs-normal/02-short-declaration/main.go new file mode 100644 index 0000000..96f7fc5 --- /dev/null +++ b/06-variables/03-short-declaration/04-short-vs-normal/02-short-declaration/main.go @@ -0,0 +1,34 @@ +// 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 + +// short declaration use cases + +func main() { + + // ----------------------------------------------------- + // if you know the initial value + // ----------------------------------------------------- + + // DON'T DO THIS: + // var width, height = 100, 50 + + // DO THIS (concise): + // width, height := 100, 50 + + // ----------------------------------------------------- + // redeclaration + // ----------------------------------------------------- + + // DON'T DO THIS: + // width = 50 + // color := red + + // DO THIS (concise): + // width, color := 50, "red" +} diff --git a/06-variables/03-short-declaration/exercises/01/main.go b/06-variables/03-short-declaration/exercises/01/main.go new file mode 100644 index 0000000..22c1cb8 --- /dev/null +++ b/06-variables/03-short-declaration/exercises/01/main.go @@ -0,0 +1,31 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Declare and then print four variables +// using the short declaration statement +// +// EXPECTED OUTPUT +// i: 314 f: 3.14 s: Hello b: true +// --------------------------------------------------------- + +func main() { + // ADD YOUR DECLARATIONS HERE + // + + // THEN UNCOMMENT THE CODE BELOW + + // fmt.Println( + // "i:", i, + // "f:", f, + // "s:", s, + // "b:", b, + // ) +} diff --git a/06-variables/03-short-declaration/exercises/01/solution/main.go b/06-variables/03-short-declaration/exercises/01/solution/main.go new file mode 100644 index 0000000..67287a8 --- /dev/null +++ b/06-variables/03-short-declaration/exercises/01/solution/main.go @@ -0,0 +1,26 @@ +// 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() { + i := 314 + f := 3.14 + s := "Hello" + b := true + + fmt.Println( + "i:", i, + "f:", f, + "s:", s, + "b:", b, + ) +} diff --git a/06-variables/03-short-declaration/exercises/02/main.go b/06-variables/03-short-declaration/exercises/02/main.go new file mode 100644 index 0000000..7921ce6 --- /dev/null +++ b/06-variables/03-short-declaration/exercises/02/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 + +// --------------------------------------------------------- +// EXERCISE +// Declare two variables using multiple short declaration +// +// EXPECTED OUTPUT +// 14 true +// --------------------------------------------------------- + +func main() { + // ADD YOUR DECLARATIONS HERE + // + + // THEN UNCOMMENT THE CODE BELOW + + // fmt.Println(a, b) +} diff --git a/06-variables/03-short-declaration/exercises/02/solution/main.go b/06-variables/03-short-declaration/exercises/02/solution/main.go new file mode 100644 index 0000000..6f38f8a --- /dev/null +++ b/06-variables/03-short-declaration/exercises/02/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + a, b := 14, true + + fmt.Println(a, b) +} diff --git a/06-variables/03-short-declaration/exercises/03/main.go b/06-variables/03-short-declaration/exercises/03/main.go new file mode 100644 index 0000000..3ffb976 --- /dev/null +++ b/06-variables/03-short-declaration/exercises/03/main.go @@ -0,0 +1,28 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Declare two variables using short declaration +// +// 2- `a` variable's value should be 42 +// 3- `c` variable's value should be "good" +// +// EXPECTED OUTPUT +// 42 good +// --------------------------------------------------------- + +func main() { + // ADD YOUR DECLARATIONS HERE + // + + // THEN UNCOMMENT THE CODE BELOW + + // fmt.Println(a, c) +} diff --git a/06-variables/03-short-declaration/exercises/03/solution/main.go b/06-variables/03-short-declaration/exercises/03/solution/main.go new file mode 100644 index 0000000..6ce809b --- /dev/null +++ b/06-variables/03-short-declaration/exercises/03/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + a, c := 42, "good" + + fmt.Println(a, c) +} diff --git a/06-variables/03-short-declaration/exercises/04/main.go b/06-variables/03-short-declaration/exercises/04/main.go new file mode 100644 index 0000000..348522e --- /dev/null +++ b/06-variables/03-short-declaration/exercises/04/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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Short declare a variable named `sum` +// 2- Initialize it with an expression by adding 27 and 3.5 +// +// EXPECTED OUTPUT +// 30.5 +// --------------------------------------------------------- + +func main() { + // ADD YOUR DECLARATION HERE + // + + // THEN UNCOMMENT THE CODE BELOW + // fmt.Println(sum) +} diff --git a/06-variables/03-short-declaration/exercises/04/solution/main.go b/06-variables/03-short-declaration/exercises/04/solution/main.go new file mode 100644 index 0000000..bdae6f4 --- /dev/null +++ b/06-variables/03-short-declaration/exercises/04/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + sum := 27 + 3.5 + + fmt.Println(sum) +} diff --git a/06-variables/03-short-declaration/exercises/05/main.go b/06-variables/03-short-declaration/exercises/05/main.go new file mode 100644 index 0000000..5d616c1 --- /dev/null +++ b/06-variables/03-short-declaration/exercises/05/main.go @@ -0,0 +1,34 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Short declare two bool variables +// (use multiple short declaration syntax) +// +// 2- Initialize both variables to true +// +// 3- Change your declaration and +// discard the 2nd variable's value +// using the blank-identifier +// +// 4- Print only the 1st variable +// +// EXPECTED OUTPUT +// true +// --------------------------------------------------------- + +func main() { + // ADD YOUR DECLARATIONS HERE + // + + // THEN UNCOMMENT THE CODE BELOW + + // fmt.Println(on) +} diff --git a/06-variables/03-short-declaration/exercises/05/solution/main.go b/06-variables/03-short-declaration/exercises/05/solution/main.go new file mode 100644 index 0000000..ada1663 --- /dev/null +++ b/06-variables/03-short-declaration/exercises/05/solution/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() { + // You can discard values in a short declaration + + on, _ := true, true + + fmt.Println(on) +} diff --git a/06-variables/03-short-declaration/exercises/06/main.go b/06-variables/03-short-declaration/exercises/06/main.go new file mode 100644 index 0000000..efba8b9 --- /dev/null +++ b/06-variables/03-short-declaration/exercises/06/main.go @@ -0,0 +1,33 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Short declare two int variables: age and yourAge +// (use multiple short declaration syntax) +// +// 2- Short declare a new float variable: ratio +// And, change the 'age' variable to 42 +// +// (! You should use redeclaration) +// +// 4- Print all the variables +// +// EXPECTED OUTPUT +// 42, 20, 3.14 +// --------------------------------------------------------- + +func main() { + // ADD YOUR DECLARATIONS HERE + // + + // THEN UNCOMMENT THE CODE BELOW + + // fmt.Println(age, yourAge, ratio) +} diff --git a/06-variables/03-short-declaration/exercises/06/solution/main.go b/06-variables/03-short-declaration/exercises/06/solution/main.go new file mode 100644 index 0000000..d4bcf13 --- /dev/null +++ b/06-variables/03-short-declaration/exercises/06/solution/main.go @@ -0,0 +1,19 @@ +// 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() { + age, yourAge := 10, 20 + age, ratio := 42, 3.14 + + fmt.Println(age, yourAge, ratio) +} diff --git a/06-variables/03-short-declaration/questions/questions.md b/06-variables/03-short-declaration/questions/questions.md new file mode 100644 index 0000000..c0d165b --- /dev/null +++ b/06-variables/03-short-declaration/questions/questions.md @@ -0,0 +1,101 @@ +## Which is a correct declaration? +* var int safe := 3 +* var safe bool := 3 +* safe := true *CORRECT* + +## Which is a correct declaration? +* var short := true +* int num := 1 +* speed := 50 *CORRECT* +* num int := 2 + +## Which is a correct declaration? +* x, y, z := 10, 20 +* x = 10, +* y, x, p := 5, "hi", 1.5 *CORRECT* +* y, x = "hello", 10 + +## Which declaration is equal to the following declaration? +```go +var s string = "hi" +``` + +* var s int = "hi" +* s := "hi" *CORRECT* +* s, p := 2, 3 + +## Which declaration is equal to the following declaration? +```go +var n = 10 +``` + +* n := 10.0 +* m, n := 1, 0 +* var n int = 10 *CORRECT* + +## What's the type of the `s` variable? +```go +s := "hmm..." +``` + +* bool +* string *CORRECT* +* int +* float64 + +## What's the type of the `b` variable? +```go +b := true +``` + +* bool *CORRECT* +* string +* int +* float64 + +## What's the type of the `i` variable? +```go +i := 42 +``` + +* bool +* string +* int *CORRECT* +* float64 + +## What's the type of the `f` variable? +```go +f := 6.28 +``` + +* bool +* string +* int +* float64 *CORRECT* + +## What's the value of the `x` variable? + +```go +y, x := false, 20 +``` + +* 10 +* 20 *CORRECT* +* false + +## What's the value of the `x` variable? + +```go +y, x := false, 20 +x, z := 10, "hi" +``` + +* 10 *CORRECT* +* 20 +* false + +## Which following declaration can be used in the package scope? + +* x := 10 +* y, x := 10, 5 +* var x, y = 5, 10 diff --git a/06-variables/04-assignment/01-assignment/01-assignment/main.go b/06-variables/04-assignment/01-assignment/01-assignment/main.go new file mode 100644 index 0000000..25c4112 --- /dev/null +++ b/06-variables/04-assignment/01-assignment/01-assignment/main.go @@ -0,0 +1,21 @@ +// 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() { + var speed int + fmt.Println(speed) + + speed = 100 + fmt.Println(speed) + + speed = speed - 25 + fmt.Println(speed) +} diff --git a/06-variables/04-assignment/01-assignment/02-strongly-typed/main.go b/06-variables/04-assignment/01-assignment/02-strongly-typed/main.go new file mode 100644 index 0000000..ed50b3d --- /dev/null +++ b/06-variables/04-assignment/01-assignment/02-strongly-typed/main.go @@ -0,0 +1,34 @@ +// 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 + +// Go is a strongly typed programming language. + +// Even a float and an integer are different types. +// Or: int32 and int are different types. + +// EXERCISE: Try uncommenting the lines +// And observe the errors + +func main() { + var speed int + // speed = "100" + + var running bool + // running = 1 + + var force float64 + // speed = force + + // Go automatically converts the typeless + // integer literal to float64 automatically + force = 1 + + // keep the compiler happy + _, _, _ = speed, running, force +} diff --git a/06-variables/04-assignment/01-assignment/03-examples/main.go b/06-variables/04-assignment/01-assignment/03-examples/main.go new file mode 100644 index 0000000..7aa6773 --- /dev/null +++ b/06-variables/04-assignment/01-assignment/03-examples/main.go @@ -0,0 +1,50 @@ +// 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() { + var ( + name string + age int + famous bool + ) + + // Example #1 + name = "Newton" + age = 84 + famous = true + + fmt.Println(name, age, famous) + + // Example #2 + name = "Somebody" + age = 20 + famous = false + + fmt.Println(name, age, famous) + + // Example #3 + // EXERCISE: Why this doesn't work? Think about it. + + // name = 20 + // name = age + + // save the previous value of the variable + // to a new variable + var prevName string + prevName = name + + // overwrite the value of the original variable + // by assigning to it + name = "Einstein" + + fmt.Println("previous name:", prevName) + fmt.Println("current name :", name) +} diff --git a/06-variables/04-assignment/01-overview/main.go b/06-variables/04-assignment/01-overview/main.go new file mode 100644 index 0000000..e1e038a --- /dev/null +++ b/06-variables/04-assignment/01-overview/main.go @@ -0,0 +1,26 @@ +// 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() { + var counter int + + fmt.Println("counter's name : counter") + fmt.Println("counter's value:", counter) + fmt.Printf("counter's type : %T\n", counter) + + counter = 10 // OK + // counter = "ten" // NOT OK + + fmt.Println("counter's value:", counter) + + counter++ + fmt.Println("counter's value:", counter) +} diff --git a/06-variables/04-assignment/05-multiple-assignment/main.go b/06-variables/04-assignment/05-multiple-assignment/main.go new file mode 100644 index 0000000..be5fef8 --- /dev/null +++ b/06-variables/04-assignment/05-multiple-assignment/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" + "time" +) + +func main() { + var ( + speed int + now time.Time + ) + + speed, now = 100, time.Now() + + fmt.Println(speed, now) + + // EXERCISE: + // Try this alternative (formatted time). + + // fmt.Println(speed, now.Format(time.Kitchen)) +} diff --git a/06-variables/04-assignment/06-swapping/main.go b/06-variables/04-assignment/06-swapping/main.go new file mode 100644 index 0000000..fe256a2 --- /dev/null +++ b/06-variables/04-assignment/06-swapping/main.go @@ -0,0 +1,21 @@ +// 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() { + var ( + speed = 100 + prevSpeed = 50 + ) + + speed, prevSpeed = prevSpeed, speed + + fmt.Println(speed, prevSpeed) +} diff --git a/06-variables/04-assignment/07-path-project/main.go b/06-variables/04-assignment/07-path-project/main.go new file mode 100644 index 0000000..91c0854 --- /dev/null +++ b/06-variables/04-assignment/07-path-project/main.go @@ -0,0 +1,22 @@ +// 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" + "path" +) + +func main() { + var dir, file string + + dir, file = path.Split("css/main.css") + + fmt.Println("dir :", dir) + fmt.Println("file:", file) +} diff --git a/06-variables/04-assignment/08-path-project-discarding/main.go b/06-variables/04-assignment/08-path-project-discarding/main.go new file mode 100644 index 0000000..50e0217 --- /dev/null +++ b/06-variables/04-assignment/08-path-project-discarding/main.go @@ -0,0 +1,21 @@ +// 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" + "path" +) + +func main() { + var file string + + _, file = path.Split("css/main.css") + + fmt.Println("file:", file) +} diff --git a/06-variables/04-assignment/09-path-project-shortdecl/main.go b/06-variables/04-assignment/09-path-project-shortdecl/main.go new file mode 100644 index 0000000..d49fd73 --- /dev/null +++ b/06-variables/04-assignment/09-path-project-shortdecl/main.go @@ -0,0 +1,22 @@ +// 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" + "path" +) + +func main() { + _, file := path.Split("css/main.css") + + // or this: + // dir, file := path.Split("css/main.css") + + fmt.Println("file:", file) +} diff --git a/06-variables/04-assignment/exercises/01/main.go b/06-variables/04-assignment/exercises/01/main.go new file mode 100644 index 0000000..6a5334b --- /dev/null +++ b/06-variables/04-assignment/exercises/01/main.go @@ -0,0 +1,28 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Change `color` variable's value to "blue" +// +// 2- Print it +// +// EXPECTED OUTPUT +// blue +// --------------------------------------------------------- + +func main() { + // DON'T TOUCH THIS: + + // color := "green" + + // ADD YOUR CODE BELOW: + + // ? +} diff --git a/06-variables/04-assignment/exercises/01/solution/main.go b/06-variables/04-assignment/exercises/01/solution/main.go new file mode 100644 index 0000000..0159a53 --- /dev/null +++ b/06-variables/04-assignment/exercises/01/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + color := "green" + + color = "blue" + + fmt.Println(color) +} diff --git a/06-variables/04-assignment/exercises/02/main.go b/06-variables/04-assignment/exercises/02/main.go new file mode 100644 index 0000000..98b78e4 --- /dev/null +++ b/06-variables/04-assignment/exercises/02/main.go @@ -0,0 +1,47 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Change the value of `color` variable to "dark green" +// +// 2- Do not assign "dark green" to `color` directly +// +// Instead, use the `color` variable again +// while assigning to `color` +// +// 3- Print it +// +// RESTRICTIONS +// WRONG ANSWER, DO NOT DO THIS: +// `color = "dark green"` +// +// HINT +// + operator can concatenate string values +// +// Example: +// "h" + "e" + "y" returns "hey" +// +// EXPECTED OUTPUT +// dark green +// --------------------------------------------------------- + +func main() { + // DON'T TOUCH THIS + + // color := "green" + + // ADD YOUR CODE BELOW + + // ? + + // UNCOMMENT THE CODE BELOW TO PRINT THE VARIABLE + + // fmt.Println(color) +} diff --git a/06-variables/04-assignment/exercises/02/solution/main.go b/06-variables/04-assignment/exercises/02/solution/main.go new file mode 100644 index 0000000..83b2289 --- /dev/null +++ b/06-variables/04-assignment/exercises/02/solution/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() { + color := "green" + + // `"dark " + color` is an expression + + color = "dark " + color + + fmt.Println(color) +} diff --git a/06-variables/04-assignment/exercises/03/main.go b/06-variables/04-assignment/exercises/03/main.go new file mode 100644 index 0000000..ab0a651 --- /dev/null +++ b/06-variables/04-assignment/exercises/03/main.go @@ -0,0 +1,38 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// 1- Multiply 3.14 with 2 and assign it to `n` variable +// +// 2- Print the `n` variable +// +// HINT +// Example: 3 * 2 = 6 +// * is the product operator (it multiplies numbers) +// +// EXPECTED OUTPUT +// 6.28 +// --------------------------------------------------------- + +func main() { + // DON'T TOUCH THIS + + // Declares a new float64 variable + // 0. means 0.0 + n := 0. + + // ADD YOUR CODE BELOW + + // ? + + fmt.Println(n) +} diff --git a/06-variables/04-assignment/exercises/03/solution/main.go b/06-variables/04-assignment/exercises/03/solution/main.go new file mode 100644 index 0000000..91b2b1c --- /dev/null +++ b/06-variables/04-assignment/exercises/03/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + n := 0. + + n = 3.14 * 2 + + fmt.Println(n) +} diff --git a/06-variables/04-assignment/exercises/04/main.go b/06-variables/04-assignment/exercises/04/main.go new file mode 100644 index 0000000..1cf3b75 --- /dev/null +++ b/06-variables/04-assignment/exercises/04/main.go @@ -0,0 +1,38 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Find the length of a rectangle +// Its width is 5 +// Its height is 6 +// +// 2- Assign the result to the `length` variable +// +// 3- Print the `length` variable +// +// HINT +// Rectangle formula = 2 * (width + height) +// +// EXPECTED OUTPUT +// 22 +// --------------------------------------------------------- + +func main() { + // DO NOT CHANGE THIS + + // var ( + // length int + // width, height = 5, 6 + // ) + + // USE THE VARIABLES ABOVE WHEN CALCULATING YOUR RESULT + + // ADD YOUR CODE BELOW +} diff --git a/06-variables/04-assignment/exercises/04/solution/main.go b/06-variables/04-assignment/exercises/04/solution/main.go new file mode 100644 index 0000000..dd931a7 --- /dev/null +++ b/06-variables/04-assignment/exercises/04/solution/main.go @@ -0,0 +1,26 @@ +// 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() { + var ( + length int + width, height = 5, 6 + ) + + // first calculates: (width + height) + // then : multiplies it with 2 + + // just like in math + + length = 2 * (width + height) + + fmt.Println(length) +} diff --git a/06-variables/04-assignment/exercises/05/main.go b/06-variables/04-assignment/exercises/05/main.go new file mode 100644 index 0000000..d36c8a5 --- /dev/null +++ b/06-variables/04-assignment/exercises/05/main.go @@ -0,0 +1,35 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// 1- Assign "go" to `lang` variable +// and assign 2 to `version` variable +// using a multiple assignment statement +// +// 2- Print the variables +// +// EXPECTED OUTPUT +// go version 2 +// --------------------------------------------------------- + +func main() { + // DO NOT TOUCH THIS + var ( + lang string + version int + ) + + // ADD YOUR CODE BELOW + + // DO NOT TOUCH THIS + fmt.Println(lang, "version", version) +} diff --git a/06-variables/04-assignment/exercises/05/solution/main.go b/06-variables/04-assignment/exercises/05/solution/main.go new file mode 100644 index 0000000..6a0b474 --- /dev/null +++ b/06-variables/04-assignment/exercises/05/solution/main.go @@ -0,0 +1,21 @@ +// 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() { + var ( + lang string + version int + ) + + lang, version = "go", 2 + + fmt.Println(lang, "version", version) +} diff --git a/06-variables/04-assignment/exercises/06/main.go b/06-variables/04-assignment/exercises/06/main.go new file mode 100644 index 0000000..342486c --- /dev/null +++ b/06-variables/04-assignment/exercises/06/main.go @@ -0,0 +1,36 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Assign the correct values to the variables +// to match to the EXPECTED OUTPUT below +// +// 2- Print the variables +// +// HINT +// Use multiple Println calls to print each sentence. +// +// EXPECTED OUTPUT +// Air is good on Mars +// It's true +// It is 19.5 degrees +// --------------------------------------------------------- + +func main() { + // DO NOT TOUCH THIS + + // var ( + // planet string + // isTrue bool + // temp float64 + // ) + + // ADD YOUR CODE BELOW +} diff --git a/06-variables/04-assignment/exercises/06/solution/main.go b/06-variables/04-assignment/exercises/06/solution/main.go new file mode 100644 index 0000000..ee30bbf --- /dev/null +++ b/06-variables/04-assignment/exercises/06/solution/main.go @@ -0,0 +1,24 @@ +// 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() { + var ( + planet string + isTrue bool + temp float64 + ) + + planet, isTrue, temp = "Mars", true, 19.5 + + fmt.Println("Air is good on", planet) + fmt.Println("It's", isTrue) + fmt.Println("It is", temp, "degrees") +} diff --git a/06-variables/04-assignment/exercises/07/main.go b/06-variables/04-assignment/exercises/07/main.go new file mode 100644 index 0000000..93ee098 --- /dev/null +++ b/06-variables/04-assignment/exercises/07/main.go @@ -0,0 +1,40 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Multiple short declare two variables +// +// 2- Initialize variables using `multi` function below +// +// 3- Discard the 1st variable's value in the declaration +// +// 4- Print only the 2nd variable +// +// NOTE +// You should use `multi` function +// while initializing the variables +// +// EXPECTED OUTPUT +// 4 +// --------------------------------------------------------- + +func main() { + // ADD YOUR DECLARATIONS HERE + // + + // THEN UNCOMMENT THE CODE BELOW + + // fmt.Println(b) +} + +// multi is a function that returns multiple int values +func multi() (int, int) { + return 5, 4 +} diff --git a/06-variables/04-assignment/exercises/07/solution/main.go b/06-variables/04-assignment/exercises/07/solution/main.go new file mode 100644 index 0000000..a32af3e --- /dev/null +++ b/06-variables/04-assignment/exercises/07/solution/main.go @@ -0,0 +1,22 @@ +// 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() { + _, b := multi() + + fmt.Println(b) +} + +func multi() (int, int) { + return 5, 4 +} diff --git a/06-variables/04-assignment/exercises/08/main.go b/06-variables/04-assignment/exercises/08/main.go new file mode 100644 index 0000000..fe0c4a6 --- /dev/null +++ b/06-variables/04-assignment/exercises/08/main.go @@ -0,0 +1,27 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Change `color` to "orange" +// and `color2` to "green" at the same time +// +// (use multiple-assignment) +// +// 2- Print the variables +// +// EXPECTED OUTPUT +// orange green +// --------------------------------------------------------- + +func main() { + // color, color2 := "red", "blue" + + // ? +} diff --git a/06-variables/04-assignment/exercises/08/solution/main.go b/06-variables/04-assignment/exercises/08/solution/main.go new file mode 100644 index 0000000..82a9788 --- /dev/null +++ b/06-variables/04-assignment/exercises/08/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + color, color2 := "red", "blue" + + color, color2 = "orange", "green" + + fmt.Println(color, color2) +} diff --git a/06-variables/04-assignment/exercises/09/main.go b/06-variables/04-assignment/exercises/09/main.go new file mode 100644 index 0000000..4188b4f --- /dev/null +++ b/06-variables/04-assignment/exercises/09/main.go @@ -0,0 +1,23 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1- Swap the values of `red` and `blue` variables +// +// 2- Print them +// +// EXPECTED OUTPUT +// blue red +// --------------------------------------------------------- + +func main() { + // red, blue := "red", "blue" + // ? +} diff --git a/06-variables/04-assignment/exercises/09/solution/main.go b/06-variables/04-assignment/exercises/09/solution/main.go new file mode 100644 index 0000000..640bffe --- /dev/null +++ b/06-variables/04-assignment/exercises/09/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + red, blue := "red", "blue" + + red, blue = blue, red + + fmt.Println(red, blue) +} diff --git a/06-variables/04-assignment/exercises/10/main.go b/06-variables/04-assignment/exercises/10/main.go new file mode 100644 index 0000000..1212890 --- /dev/null +++ b/06-variables/04-assignment/exercises/10/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 + +// --------------------------------------------------------- +// EXERCISE +// Print only the directory using `path.Split` +// +// Discard the file part +// +// RESTRICTION +// Use short declaration +// +// EXPECTED OUTPUT +// secret/ +// --------------------------------------------------------- + +func main() { + // ? ?= path.Split("secret/file.txt") +} diff --git a/06-variables/04-assignment/exercises/10/solution/main.go b/06-variables/04-assignment/exercises/10/solution/main.go new file mode 100644 index 0000000..3c5491e --- /dev/null +++ b/06-variables/04-assignment/exercises/10/solution/main.go @@ -0,0 +1,19 @@ +// 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" + "path" +) + +func main() { + dir, _ := path.Split("secret/file.txt") + + fmt.Println(dir) +} diff --git a/06-variables/04-assignment/questions/questions.md b/06-variables/04-assignment/questions/questions.md new file mode 100644 index 0000000..4220631 --- /dev/null +++ b/06-variables/04-assignment/questions/questions.md @@ -0,0 +1,66 @@ +## What happens when you assign a variable to another variable? +* The variables become the same variable +* Assignee variable gets deleted +* Variable's value is changed to the assigned variable's value *CORRECT* + +## Which one is a correct assignment statement? +```go +opened := true +``` +* `closed := true` +* `opened = false` *CORRECT* +* `var x = 2` + +## Which one is a correct assignment statement? +* `a, b = 3; 5` +* `c, d = true, false` *CORRECT* +* `a, b, c = 5, 3` + +## Which one is a correct assignment? +```go +var ( + n = 3 + m int +) +``` + +* `m = "4"` +* `n = 10` *CORRECT* +* `n = true` +* `m = false` + +## Which one is a correct assignment? +```go +var ( + n = 3 + m int + f float64 +) +``` + +* `n, m = 4, f` +* `n = false` +* `n, m = m + n, n + 5.0` *CORRECT* +* `n, m = 3.82, "hi"` + +## Which one is a correct assignment statement? +```go +var ( + a int + c bool +) +``` +* `a = _` +* `c, _ = _, false` *CORRECT* +* `_, _ = true, true` + +## Which one is a correct assignment? +**REMEMBER:** `path.Split` returns two `string` values + +```go + var c, f string +``` +* `_ = path.Split("assets/profile.png")` +* `_, _, c = path.Split("assets/profile.png")` +* `f = path.Split("assets/profile.png")` +* `_, f = path.Split("assets/profile.png")` *CORRECT* diff --git a/06-variables/05-type-conversion/01-destructive/main.go b/06-variables/05-type-conversion/01-destructive/main.go new file mode 100644 index 0000000..f2e66d2 --- /dev/null +++ b/06-variables/05-type-conversion/01-destructive/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" + +func main() { + speed := 100 // int + force := 2.5 // float64 + + // ERROR: invalid op + // speed = speed * force + + // conversion can be a destructive operation + // `force` loses its fractional part... + + speed = speed * int(force) + + fmt.Println(speed) +} diff --git a/06-variables/05-type-conversion/02-correct/main.go b/06-variables/05-type-conversion/02-correct/main.go new file mode 100644 index 0000000..e1ada92 --- /dev/null +++ b/06-variables/05-type-conversion/02-correct/main.go @@ -0,0 +1,31 @@ +// 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" + +// order of conversion matters... + +func main() { + speed := 100 + force := 2.5 + + fmt.Printf("speed : %T\n", speed) + fmt.Printf("conversion: %T\n", float64(speed)) + fmt.Printf("expression: %T\n", float64(speed)*force) + + // TYPE MISMATCH: + // speed is int + // expression is float64 + // speed = float64(speed) * force + + // CORRECT: int * int + speed = int(float64(speed) * force) + + fmt.Println(speed) +} diff --git a/06-variables/05-type-conversion/03-numeric-conversion/main.go b/06-variables/05-type-conversion/03-numeric-conversion/main.go new file mode 100644 index 0000000..007347a --- /dev/null +++ b/06-variables/05-type-conversion/03-numeric-conversion/main.go @@ -0,0 +1,44 @@ +// 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() { + var apple int + var orange int32 + + // ERROR: + // cannot assign orange to apple (different types) + // apple = orange + + // you need to convert orange to apple + + // orange is convertable to int because, + // int and int32 are both numeric types + + apple = int(orange) + + // you can't convert a numeric type to a bool: + // isDelicious := bool(orange) + + // but you can convert an int to a string + // this only works with int types + orange = 65 // 65 is A + color := string(orange) + fmt.Println(color) + + // this doesn't work. 65.0 is a float. + // fmt.Println(string(65.0)) + + // this works: there are two byte values + // byte is also an int + fmt.Println(string([]byte{104, 105})) + + _ = apple +} diff --git a/06-variables/05-type-conversion/exercises/01/main.go b/06-variables/05-type-conversion/exercises/01/main.go new file mode 100644 index 0000000..b29e63c --- /dev/null +++ b/06-variables/05-type-conversion/exercises/01/main.go @@ -0,0 +1,21 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Fix the code by using the conversion expression. +// +// EXPECTED OUTPUT +// 15.5 +// --------------------------------------------------------- + +func main() { + // a, b := 10, 5.5 + // fmt.Println(a + b) +} diff --git a/06-variables/05-type-conversion/exercises/01/solution/main.go b/06-variables/05-type-conversion/exercises/01/solution/main.go new file mode 100644 index 0000000..5ccbeb2 --- /dev/null +++ b/06-variables/05-type-conversion/exercises/01/solution/main.go @@ -0,0 +1,15 @@ +// 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() { + a, b := 10, 5.5 + fmt.Println(float64(a) + b) +} diff --git a/06-variables/05-type-conversion/exercises/02/main.go b/06-variables/05-type-conversion/exercises/02/main.go new file mode 100644 index 0000000..96c5141 --- /dev/null +++ b/06-variables/05-type-conversion/exercises/02/main.go @@ -0,0 +1,22 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Fix the code by using the conversion expression. +// +// EXPECTED OUTPUT +// 10.5 +// --------------------------------------------------------- + +func main() { + // a, b := 10, 5.5 + // a = b + // fmt.Println(a + b) +} diff --git a/06-variables/05-type-conversion/exercises/02/solution/main.go b/06-variables/05-type-conversion/exercises/02/solution/main.go new file mode 100644 index 0000000..ea0d394 --- /dev/null +++ b/06-variables/05-type-conversion/exercises/02/solution/main.go @@ -0,0 +1,16 @@ +// 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() { + a, b := 10, 5.5 + a = int(b) + fmt.Println(float64(a) + b) +} diff --git a/06-variables/05-type-conversion/exercises/03/main.go b/06-variables/05-type-conversion/exercises/03/main.go new file mode 100644 index 0000000..796a693 --- /dev/null +++ b/06-variables/05-type-conversion/exercises/03/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 + +// --------------------------------------------------------- +// EXERCISE +// Fix the code. +// +// EXPECTED OUTPUT +// 5.5 +// --------------------------------------------------------- + +func main() { + // fmt.Println(int(5.5)) +} diff --git a/06-variables/05-type-conversion/exercises/03/solution/main.go b/06-variables/05-type-conversion/exercises/03/solution/main.go new file mode 100644 index 0000000..66ab676 --- /dev/null +++ b/06-variables/05-type-conversion/exercises/03/solution/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Println(5.5) +} diff --git a/06-variables/05-type-conversion/exercises/04/main.go b/06-variables/05-type-conversion/exercises/04/main.go new file mode 100644 index 0000000..7680f0f --- /dev/null +++ b/06-variables/05-type-conversion/exercises/04/main.go @@ -0,0 +1,21 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Fix the code. +// +// EXPECTED OUTPUT +// 9.5 +// --------------------------------------------------------- + +func main() { + // age := 2 + // fmt.Println(int(7.5) + int(age)) +} diff --git a/06-variables/05-type-conversion/exercises/04/solution/main.go b/06-variables/05-type-conversion/exercises/04/solution/main.go new file mode 100644 index 0000000..b5c51f5 --- /dev/null +++ b/06-variables/05-type-conversion/exercises/04/solution/main.go @@ -0,0 +1,15 @@ +// 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() { + age := 2 + fmt.Println(7.5 + float64(age)) +} diff --git a/06-variables/05-type-conversion/exercises/05/main.go b/06-variables/05-type-conversion/exercises/05/main.go new file mode 100644 index 0000000..66f92d5 --- /dev/null +++ b/06-variables/05-type-conversion/exercises/05/main.go @@ -0,0 +1,31 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// Fix the code. +// +// HINTS +// maximum of int8 can be 127 +// maximum of int16 can be +// +// EXPECTED OUTPUT +// 1127 +// --------------------------------------------------------- + +func main() { + // DO NOT TOUCH THIS VARIABLES + min := int8(127) + max := int16(1000) + + // FIX THE CODE HERE + fmt.Println(int8(max) + min) +} diff --git a/06-variables/05-type-conversion/exercises/05/solution/main.go b/06-variables/05-type-conversion/exercises/05/solution/main.go new file mode 100644 index 0000000..78ed39f --- /dev/null +++ b/06-variables/05-type-conversion/exercises/05/solution/main.go @@ -0,0 +1,30 @@ +// 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() { + min := int8(127) + max := int16(1000) + + fmt.Println(max + int16(min)) + + // EXPLANATION + // + // `int8(max)` destroys the information of max + // It reduces it to 127 + // Which is the maximum value of int8 + // + // Correct conversion is int16(min) + // Because, int16 > int8 + // When you do so, min doesn't lose information + // + // You will learn more about this in + // the "Go Type System" section. +} diff --git a/06-variables/05-type-conversion/questions/questions.md b/06-variables/05-type-conversion/questions/questions.md new file mode 100644 index 0000000..10d702f --- /dev/null +++ b/06-variables/05-type-conversion/questions/questions.md @@ -0,0 +1,63 @@ +## Which one is a correct type conversion expression? +* convert(40) +* var("hi") +* int(4.) *CORRECT* +* int[4] + +## What does this code print? +```go +age := 6.5 +fmt.Print(int(age)) +``` +* 6.5 +* 65 +* 6 *CORRECT* +* .5 + +> When you convert a float to integer +> It loses its fractional part + +## What does this code print? +```go +fmt.Print(int(6.5)) +``` +* 6.5 +* 65 +* 6 +* Compile-Time Error *CORRECT* + +> Go can detect conversion errors at the compile-time + +## What does this code print? +```go +area := 10.5 +fmt.Print(area/2) +``` +* 5.25 *CORRECT* +* 5 +* 0 +* Error + +## What does this code print? +```go +area := 10.5 +div := 2 +fmt.Print(area/div) +``` +* 5.25 +* 5 +* ERROR *CORRECT* + +> You can't divide different type of values. +> You need to convert: `area / float64(div)` + +## Which code below can fix the following code? +```go +area := 10.5 +div := 2 +fmt.Print(area/div) +``` +* `fmt.Print(int(area)/div)` // 5 +* `fmt.Print(area/int(div))` // type mismatch +* `fmt.Print(int(area)/int(div))` // 5 +* `fmt.Print(area/float64(div))` *CORRECT* \ No newline at end of file diff --git a/06-variables/06-project-greeter/01-demonstration/main.go b/06-variables/06-project-greeter/01-demonstration/main.go new file mode 100644 index 0000000..353bcb6 --- /dev/null +++ b/06-variables/06-project-greeter/01-demonstration/main.go @@ -0,0 +1,32 @@ +// 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" + "os" +) + +// NOTE: RUN THIS WITH 3 ARGUMENTS AT LEAST +// OR, THERE WILL BE AN ERROR + +func main() { + fmt.Printf("%#v\n", os.Args) + + // Gets an item from the os.Args string slice: + // os.Args[INDEX] + // INDEX can be 0 or greater + fmt.Println("Path:", os.Args[0]) + fmt.Println("1st argument:", os.Args[1]) + fmt.Println("2nd argument:", os.Args[2]) + fmt.Println("3rd argument:", os.Args[3]) + + // `len` function can find how many items + // inside a slice value + fmt.Println("Items inside os.Args:", len(os.Args)) +} diff --git a/06-variables/06-project-greeter/02-version1/main.go b/06-variables/06-project-greeter/02-version1/main.go new file mode 100644 index 0000000..4ffe708 --- /dev/null +++ b/06-variables/06-project-greeter/02-version1/main.go @@ -0,0 +1,35 @@ +// 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" + "os" +) + +// Since, you didn't learn about the control flow statements yet +// I didn't include an error detection here. +// +// So, if you don't pass a name and a lastname, +// this program will fail. +// This is intentional. + +func main() { + var name string + + // assign a new value to the string variable below + name = os.Args[1] + fmt.Println("Hello great", name, "!") + + // changes the name, declares the age with 85 + name, age := "gandalf", 2019 + + fmt.Println("My name is", name) + fmt.Println("My age is", age) + fmt.Println("BTW, you shall pass!") +} diff --git a/06-variables/06-project-greeter/03-version2/main.go b/06-variables/06-project-greeter/03-version2/main.go new file mode 100644 index 0000000..cda72fa --- /dev/null +++ b/06-variables/06-project-greeter/03-version2/main.go @@ -0,0 +1,33 @@ +// 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" + "os" +) + +// Since, you didn't learn about the control flow statements yet +// I didn't include an error detection here. +// +// So, if you don't pass a name and a lastname, +// this program will fail. +// This is intentional. + +func main() { + // assign a new value to the string variable below + name := os.Args[1] + fmt.Println("Hello great", name, "!") + + // changes the name, declares the age with 85 + name, age := "gandalf", 2019 + + fmt.Println("My name is", name) + fmt.Println("My age is", age) + fmt.Println("BTW, you shall pass!") +} diff --git a/06-variables/06-project-greeter/exercises/01/main.go b/06-variables/06-project-greeter/exercises/01/main.go new file mode 100644 index 0000000..86bf9b4 --- /dev/null +++ b/06-variables/06-project-greeter/exercises/01/main.go @@ -0,0 +1,27 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print the count of the command-line arguments +// +// INPUT +// bilbo balbo bungo +// +// EXPECTED OUTPUT +// There are 3 names. +// --------------------------------------------------------- + +func main() { + // FIX THIS CODE + // count := ? + + // DO NOT TOUCH THIS CODE + // fmt.Printf("There are %d names.\n", count) +} diff --git a/06-variables/06-project-greeter/exercises/01/solution/main.go b/06-variables/06-project-greeter/exercises/01/solution/main.go new file mode 100644 index 0000000..7578f9e --- /dev/null +++ b/06-variables/06-project-greeter/exercises/01/solution/main.go @@ -0,0 +1,19 @@ +// 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" + "os" +) + +func main() { + count := len(os.Args) - 1 + + fmt.Printf("There are %d names.\n", count) +} diff --git a/06-variables/06-project-greeter/exercises/02/main.go b/06-variables/06-project-greeter/exercises/02/main.go new file mode 100644 index 0000000..e41e1c2 --- /dev/null +++ b/06-variables/06-project-greeter/exercises/02/main.go @@ -0,0 +1,24 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print the path of the running program +// By getting it from `os.Args` variable +// +// HINT +// Use `go build` to build your program. +// Then run it using the compiled executable program file. +// +// EXPECTED OUTPUT SHOULD INCLUDE THIS +// myprogram +// --------------------------------------------------------- + +func main() { +} diff --git a/06-variables/06-project-greeter/exercises/02/solution/main.go b/06-variables/06-project-greeter/exercises/02/solution/main.go new file mode 100644 index 0000000..0714502 --- /dev/null +++ b/06-variables/06-project-greeter/exercises/02/solution/main.go @@ -0,0 +1,28 @@ +// 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" + "os" +) + +// STEPS: +// +// Compile it by typing: +// go build -o myprogram +// +// Then run it by typing: +// ./myprogram +// +// If you're on Windows, then type: +// myprogram + +func main() { + fmt.Println(os.Args[0]) +} diff --git a/06-variables/06-project-greeter/exercises/03/main.go b/06-variables/06-project-greeter/exercises/03/main.go new file mode 100644 index 0000000..9babde4 --- /dev/null +++ b/06-variables/06-project-greeter/exercises/03/main.go @@ -0,0 +1,34 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print your name +// Get it from the first command-line argument +// +// INPUT +// Call the program using your name +// +// EXPECTED OUTPUT +// It should print your name +// +// EXAMPLE +// go run main.go inanc +// inanc +// +// BONUS: Make the output like this: +// go run main.go inanc +// Hi inanc +// How are you? +// --------------------------------------------------------- + +func main() { + // get your name from the command-line + // and print it +} diff --git a/06-variables/06-project-greeter/exercises/03/solution/main.go b/06-variables/06-project-greeter/exercises/03/solution/main.go new file mode 100644 index 0000000..79bd1d7 --- /dev/null +++ b/06-variables/06-project-greeter/exercises/03/solution/main.go @@ -0,0 +1,21 @@ +// 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" + "os" +) + +func main() { + fmt.Println(os.Args[1]) + + // BONUS SOLUTION: + fmt.Println("Hello", os.Args[1]) + fmt.Println("How are you?") +} diff --git a/06-variables/06-project-greeter/exercises/04/main.go b/06-variables/06-project-greeter/exercises/04/main.go new file mode 100644 index 0000000..ba5b705 --- /dev/null +++ b/06-variables/06-project-greeter/exercises/04/main.go @@ -0,0 +1,34 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Greet more people +// +// RESTRICTIONS +// Be sure to match to the expected output below +// +// INPUT +// bilbo balbo bungo +// +// EXPECTED OUTPUT +// There are 3 people! +// Hello great bilbo ! +// Hello great balbo ! +// Hello great bungo ! +// Nice to meet you all. +// --------------------------------------------------------- + +func main() { + // TYPE YOUR CODE HERE + + // BONUS #1: + // Observe the error if you pass less then 3 arguments. + // Search on the web how to solve that. +} diff --git a/06-variables/06-project-greeter/exercises/04/solution/main.go b/06-variables/06-project-greeter/exercises/04/solution/main.go new file mode 100644 index 0000000..0ae6469 --- /dev/null +++ b/06-variables/06-project-greeter/exercises/04/solution/main.go @@ -0,0 +1,28 @@ +// 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" + "os" +) + +func main() { + var ( + l = len(os.Args) - 1 + n1 = os.Args[1] + n2 = os.Args[2] + n3 = os.Args[3] + ) + + fmt.Println("There are", l, "people !") + fmt.Println("Hello great", n1, "!") + fmt.Println("Hello great", n2, "!") + fmt.Println("Hello great", n3, "!") + fmt.Println("Nice to meet you all.") +} diff --git a/06-variables/06-project-greeter/exercises/05/main.go b/06-variables/06-project-greeter/exercises/05/main.go new file mode 100644 index 0000000..110a27c --- /dev/null +++ b/06-variables/06-project-greeter/exercises/05/main.go @@ -0,0 +1,34 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Greeat 5 people this time. +// Do not copy paste from the previous exercise! +// And do not look at it. +// +// RESTRICTION +// This time do not use variables. +// +// INPUT +// bilbo balbo bungo gandalf legolas +// +// EXPECTED OUTPUT +// There are 5 people! +// Hello great bilbo ! +// Hello great balbo ! +// Hello great bungo ! +// Hello great gandalf ! +// Hello great legolas ! +// Nice to meet you all. +// --------------------------------------------------------- + +func main() { + // TYPE YOUR CODE HERE +} diff --git a/06-variables/06-project-greeter/exercises/05/solution/main.go b/06-variables/06-project-greeter/exercises/05/solution/main.go new file mode 100644 index 0000000..6142c1e --- /dev/null +++ b/06-variables/06-project-greeter/exercises/05/solution/main.go @@ -0,0 +1,23 @@ +// 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" + "os" +) + +func main() { + fmt.Println("There are", len(os.Args)-1, "people !") + fmt.Println("Hello great", os.Args[1], "!") + fmt.Println("Hello great", os.Args[2], "!") + fmt.Println("Hello great", os.Args[3], "!") + fmt.Println("Hello great", os.Args[4], "!") + fmt.Println("Hello great", os.Args[5], "!") + fmt.Println("Nice to meet you all.") +} diff --git a/06-variables/06-project-greeter/exercises/solution-to-the-lecture-exercise/main.go b/06-variables/06-project-greeter/exercises/solution-to-the-lecture-exercise/main.go new file mode 100644 index 0000000..f8cd7a8 --- /dev/null +++ b/06-variables/06-project-greeter/exercises/solution-to-the-lecture-exercise/main.go @@ -0,0 +1,41 @@ +// 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" + "os" +) + +// --------------------------------------------------------- +// This is a solution to the exercise that I asked +// in the lecture. +// --------------------------------------------------------- + +// NOTE: You should run this program with 3 arguments +// at least. Or there will be an error. + +func main() { + // assign a new value to the string variable below + var ( + name = os.Args[1] + name2 = os.Args[2] + name3 = os.Args[3] + ) + + fmt.Println("Hello great", name, "!") + fmt.Println("And hellooo to you magnificient", name2, "!") + fmt.Println("Welcome", name3, "!") + + // changes the name, declares the age with 131 + name, age := "bilbo baggins", 131 // unknown age! + + fmt.Println("My name is", name) + fmt.Println("My age is", age) + fmt.Println("And, I love adventures!") +} diff --git a/06-variables/06-project-greeter/questions/questions.md b/06-variables/06-project-greeter/questions/questions.md new file mode 100644 index 0000000..f90b2de --- /dev/null +++ b/06-variables/06-project-greeter/questions/questions.md @@ -0,0 +1,54 @@ +## What's does `os.Args` variable store in its first item? +* The first argument that is passed to the program +* The second argument that is passed to the program +* Path to the running program *CORRECT* + +## What's the type of the `Args` variable? +```go +var Args []string +``` +* string +* string array +* a slice of strings *CORRECT* + +## What is the type of each value in the `Args` variable? +```go +var Args []string +``` +* string *CORRECT* +* string array +* a slice of strings + +## How to get the first item of the `Args` variable? +```go +var Args []string +``` +* Args.0 +* Args{1} +* Args[0] *CORRECT* +* Args(1) + +## How to get the second item of the `Args` variable? +```go +var Args []string +``` +* Args.2 +* Args[1] *CORRECT* +* Args{1} +* Args(2) + +## How to get the length of the `Args` variable? +```go +var Args []string +``` +* length(Args) +* Args.len +* len(Args) *CORRECT* +* Args.Length + +## How to get the first "argument" from the command-line? +* os.Args[0] +* os.Args[1] *CORRECT* +* os.Args[2] +* os.Args[3] + diff --git a/07-printf/01-intro/01-println-vs-printf/main.go b/07-printf/01-intro/01-println-vs-printf/main.go new file mode 100644 index 0000000..bb8fc44 --- /dev/null +++ b/07-printf/01-intro/01-println-vs-printf/main.go @@ -0,0 +1,23 @@ +// 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() { + ops, ok, fail := 2350, 543, 433 + + fmt.Println( + "total:", ops, "- success:", ok, "/", fail, + ) + + fmt.Printf( + "total: %d - success: %d / %d\n", + ops, ok, fail, + ) +} diff --git a/07-printf/01-intro/02/main.go b/07-printf/01-intro/02/main.go new file mode 100644 index 0000000..49da4b6 --- /dev/null +++ b/07-printf/01-intro/02/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() { + var brand string + + // prints the string in quoted-form like this "" + fmt.Printf("%q\n", brand) + + brand = "Google" + fmt.Printf("%q\n", brand) +} diff --git a/07-printf/02-escape-sequences/main.go b/07-printf/02-escape-sequences/main.go new file mode 100644 index 0000000..ae146a6 --- /dev/null +++ b/07-printf/02-escape-sequences/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" + +func main() { + // without newline + fmt.Println("hihi") + + // with newline: + // \n = escape sequence + // \ = escape character + fmt.Println("hi\nhi") + + // escape characters: + // \\ = \ + // \" = " + fmt.Println("hi\\n\"hi\"") +} diff --git a/07-printf/03-printing-types/main.go b/07-printf/03-printing-types/main.go new file mode 100644 index 0000000..322b58d --- /dev/null +++ b/07-printf/03-printing-types/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" + +func main() { + // I'm using multiple declaration instead of singular + var ( + speed int + heat float64 + off bool + brand string + ) + + fmt.Printf("%T\n", speed) + fmt.Printf("%T\n", heat) + fmt.Printf("%T\n", off) + fmt.Printf("%T\n", brand) +} diff --git a/07-printf/04-coding/main.go b/07-printf/04-coding/main.go new file mode 100644 index 0000000..341112c --- /dev/null +++ b/07-printf/04-coding/main.go @@ -0,0 +1,51 @@ +// 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() { + var ( + planet = "venus" + distance = 261 + orbital = 224.701 + hasLife = false + ) + + // swiss army knife %v verb + fmt.Printf("Planet: %v\n", planet) + fmt.Printf("Distance: %v millions kms\n", distance) + fmt.Printf("Orbital Period: %v days\n", orbital) + fmt.Printf("Does %v have life? %v\n", planet, hasLife) + + // argument indexing - indexes start from 1 + fmt.Printf( + "%v is %v away. Think! %[2]v kms! %[1]v OMG.\n", + planet, distance, + ) + + // why use other verbs than? because: type-safety + // uncomment to see the warnings: + // + // fmt.Printf("Planet: %d\n", planet) + // fmt.Printf("Distance: %s millions kms\n", distance) + // fmt.Printf("Orbital Period: %t days\n", orbital) + // fmt.Printf("Does %v has life? %f\n", planet, hasLife) + + // correct verbs: + // fmt.Printf("Planet: %s\n", planet) + // fmt.Printf("Distance: %d millions kms\n", distance) + // fmt.Printf("Orbital Period: %f days\n", orbital) + // fmt.Printf("Does %s has life? %t\n", planet, hasLife) + + // precision + fmt.Printf("Orbital Period: %f days\n", orbital) + fmt.Printf("Orbital Period: %.0f days\n", orbital) + fmt.Printf("Orbital Period: %.1f days\n", orbital) + fmt.Printf("Orbital Period: %.2f days\n", orbital) +} diff --git a/07-printf/exercises/01/main.go b/07-printf/exercises/01/main.go new file mode 100644 index 0000000..62af94f --- /dev/null +++ b/07-printf/exercises/01/main.go @@ -0,0 +1,23 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print your age using Prinft +// +// EXPECTED OUTPUT +// My age is 30 years old. +// +// NOTE +// You should change 30 to your age, of course. +// --------------------------------------------------------- + +func main() { + // ? +} diff --git a/07-printf/exercises/01/solution/main.go b/07-printf/exercises/01/solution/main.go new file mode 100644 index 0000000..daeb312 --- /dev/null +++ b/07-printf/exercises/01/solution/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Printf("My age is %d years old.\n", 30) +} diff --git a/07-printf/exercises/02/main.go b/07-printf/exercises/02/main.go new file mode 100644 index 0000000..1ecf865 --- /dev/null +++ b/07-printf/exercises/02/main.go @@ -0,0 +1,27 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print your name and lastname using Printf +// +// EXPECTED OUTPUT +// My name is Inanc and my lastname is Gumus. +// +// BONUS +// Store the formatting specifier (first argument of Printf) +// in a variable. +// Then pass it to printf +// --------------------------------------------------------- + +func main() { + // BONUS: Use a variable for the format specifier + + // fmt.Printf("?", ?, ?) +} diff --git a/07-printf/exercises/02/solution/main.go b/07-printf/exercises/02/solution/main.go new file mode 100644 index 0000000..d175bad --- /dev/null +++ b/07-printf/exercises/02/solution/main.go @@ -0,0 +1,18 @@ +// 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() { + fmt.Printf("My name is %s and my lastname is %s.\n", "Inanc", "Gumus") + + // BONUS + msg := "My name is %s and my lastname is %s.\n" + fmt.Printf(msg, "Inanc", "Gumus") +} diff --git a/07-printf/exercises/03/main.go b/07-printf/exercises/03/main.go new file mode 100644 index 0000000..8e6f243 --- /dev/null +++ b/07-printf/exercises/03/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 + +// --------------------------------------------------------- +// EXERCISE +// Use printf to print the expected output using a variable. +// +// EXPECTED OUTPUT +// These are false claims. +// --------------------------------------------------------- + +func main() { + // UNCOMMENT THE FOLLOWING CODE + // AND DO NOT CHANGE IT AFTERWARDS + // tf := false + + // TYPE YOUR CODE HERE + // ? +} diff --git a/07-printf/exercises/03/solution/main.go b/07-printf/exercises/03/solution/main.go new file mode 100644 index 0000000..aa43600 --- /dev/null +++ b/07-printf/exercises/03/solution/main.go @@ -0,0 +1,15 @@ +// 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() { + tf := false + fmt.Printf("These are %t claims.\n", tf) +} diff --git a/07-printf/exercises/04/main.go b/07-printf/exercises/04/main.go new file mode 100644 index 0000000..188f908 --- /dev/null +++ b/07-printf/exercises/04/main.go @@ -0,0 +1,24 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print the current temperature in your area using Printf +// +// NOTE +// Do not use %v verb +// Output "shouldn't" be like 29.500000 +// +// EXPECTED OUTPUT +// Temperature is 29.5 degrees. +// --------------------------------------------------------- + +func main() { + // ? +} diff --git a/07-printf/exercises/04/solution/main.go b/07-printf/exercises/04/solution/main.go new file mode 100644 index 0000000..e0ba160 --- /dev/null +++ b/07-printf/exercises/04/solution/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Printf("Temperature is %.1f degrees.\n", 29.5) +} diff --git a/07-printf/exercises/05/main.go b/07-printf/exercises/05/main.go new file mode 100644 index 0000000..3a6cec0 --- /dev/null +++ b/07-printf/exercises/05/main.go @@ -0,0 +1,24 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print "hello world" with double-quotes using Printf +// (As you see here) +// +// NOTE +// Output "shouldn't" be just: hello world +// +// EXPECTED OUTPUT +// "hello world" +// --------------------------------------------------------- + +func main() { + // ? +} diff --git a/07-printf/exercises/05/solution/main.go b/07-printf/exercises/05/solution/main.go new file mode 100644 index 0000000..9e04114 --- /dev/null +++ b/07-printf/exercises/05/solution/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Printf("%q\n", "hello world") +} diff --git a/07-printf/exercises/06/main.go b/07-printf/exercises/06/main.go new file mode 100644 index 0000000..bde9b3b --- /dev/null +++ b/07-printf/exercises/06/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 + +// --------------------------------------------------------- +// EXERCISE +// Print the type and value of 3 using fmt.Printf +// +// EXPECTED OUTPUT +// Type of 3 is int +// --------------------------------------------------------- + +func main() { + // ? +} diff --git a/07-printf/exercises/06/solution/main.go b/07-printf/exercises/06/solution/main.go new file mode 100644 index 0000000..115ce8f --- /dev/null +++ b/07-printf/exercises/06/solution/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Printf("Type of %d is %T\n", 3, 3) +} diff --git a/07-printf/exercises/07/main.go b/07-printf/exercises/07/main.go new file mode 100644 index 0000000..37bb4ab --- /dev/null +++ b/07-printf/exercises/07/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 + +// --------------------------------------------------------- +// EXERCISE +// Print the type and value of 3.14 using fmt.Printf +// +// EXPECTED OUTPUT +// Type of 3.14 is float64 +// --------------------------------------------------------- + +func main() { + // ? +} diff --git a/07-printf/exercises/07/solution/main.go b/07-printf/exercises/07/solution/main.go new file mode 100644 index 0000000..2c6315d --- /dev/null +++ b/07-printf/exercises/07/solution/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Printf("Type of %f is %T\n", 3.14, 3.14) +} diff --git a/07-printf/exercises/08/main.go b/07-printf/exercises/08/main.go new file mode 100644 index 0000000..5be43a7 --- /dev/null +++ b/07-printf/exercises/08/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 + +// --------------------------------------------------------- +// EXERCISE +// Print the type and value of "hello" using fmt.Printf +// +// EXPECTED OUTPUT +// Type of hello is string +// --------------------------------------------------------- + +func main() { + // ? +} diff --git a/07-printf/exercises/08/solution/main.go b/07-printf/exercises/08/solution/main.go new file mode 100644 index 0000000..24644ec --- /dev/null +++ b/07-printf/exercises/08/solution/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Printf("Type of %s is %T\n", "hello", "hello") +} diff --git a/07-printf/exercises/09/main.go b/07-printf/exercises/09/main.go new file mode 100644 index 0000000..bd116fe --- /dev/null +++ b/07-printf/exercises/09/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 + +// --------------------------------------------------------- +// EXERCISE +// Print the type and value of true using fmt.Printf +// +// EXPECTED OUTPUT +// Type of true is bool +// --------------------------------------------------------- + +func main() { + // ? +} diff --git a/07-printf/exercises/09/solution/main.go b/07-printf/exercises/09/solution/main.go new file mode 100644 index 0000000..0ebeb5c --- /dev/null +++ b/07-printf/exercises/09/solution/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Printf("Type of %t is %T\n", true, true) +} diff --git a/07-printf/exercises/10/main.go b/07-printf/exercises/10/main.go new file mode 100644 index 0000000..e95c4e3 --- /dev/null +++ b/07-printf/exercises/10/main.go @@ -0,0 +1,24 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Get your name and lastname from the command-line +// 2. Print them using Printf +// +// EXAMPLE INPUT +// Inanc Gumus +// +// EXPECTED OUTPUT +// Your name is Inanc and your lastname is Gumus. +// --------------------------------------------------------- + +func main() { + // BONUS: Use a variable for the format specifier +} diff --git a/07-printf/exercises/10/solution/main.go b/07-printf/exercises/10/solution/main.go new file mode 100644 index 0000000..ec18ac1 --- /dev/null +++ b/07-printf/exercises/10/solution/main.go @@ -0,0 +1,23 @@ +// 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" + "os" +) + +func main() { + // WARNING: This program will error + // if you don't pass your name and lastname + + name, lastname := os.Args[1], os.Args[2] + + msg := "Your name is %s and your lastname is %s.\n" + fmt.Printf(msg, name, lastname) +} diff --git a/07-printf/printf cheatsheet.pdf b/07-printf/printf cheatsheet.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e1f0bfbd80db8ea951e5cdc7c916991be20ab46d GIT binary patch literal 40040 zcmce-1z1&E*FTDav~=f|l7>x#ba#VvcWt^ux7Zt0ek{BJOh=RNPe z-?`6spFckCJ=dIbtdVofxyEm8a(Q77i}>&^CZ@1NWFh z*n-C#APga#z~5Hj9=gBLfWR~C!6UX{N;e34@H`NNH5ELAkiD_KBgp<<67ZG0J;=z( z(D>mJ!Ue(-JlPQ}kQMk71SU0tF!@Dd4zdx}cLZx7%*DhAWCH?$?2JGb4kiu_DtKOA zcw-x*drg`D)Y#3|7{DOn>L{k<2&RN*P!a$*+B@AhWB`oxj7;zhhWY>?JtJ5sfgj%j z5=sCQeJh8DIr=M@M6O8vui_v9r0Mv7(ru_AeRO{vm^! zoPmY0p(B{FlY!%dR1((ureG#7&5az*06M6JLo{+M`g4hKiDnXTbn)2v5%EIJu6 zAeKy__7t;fMikF+aO1bBIR(YA6~$6(FCXVNh`uEx;C4Mh&B|&wG~!l%Cb%7k9s=oh zm>3Uw1oo(v4o<0?cjX1T%VBMS1Nxz(i@N-nSye6Mr)es(}fX6~p zF4C;fQpQ2{o>QleO8(Agm%%m;W(P=Zn*0=|7Fr@B0otOpx*oWM*RIK{RR{L%w4H1nwPFB^xC(R^tfc4_MICazf?$s-|&5vsmD(Vs7xPrv71W3fz&yb|>Mg8uTA zsrx0sLEYC$?OkKc*JZkIA2`|1pU}5#RXnvi?|DDD;O>z$Jt7kRx{w3*L-b+AnT z!xiP3a9*flCWUL$p^OfBE6GU1X(QhdDqTLl8>)d40tAxGQeHVL@2S4keK&*AU4UU& z>%bER<;3=nW6^v^uQ)$`Z8jz-ohH21^|=ZxV`df`@VD437>HKx97xrX8rQGawDD+TeJNiu`=9Hl5;Sm(D zrvoE$7uq6M@$t)@?n@{?RvAuTWV}#dl$8eGesMd~`3xR#xLkt@+1Cmrz5S}v@HO(3 zd>U*SV~k%iaDVC?zs{|uY(kL@HCO(%pmLbaak zxZ^mJy9ELb?p4XoU+G-6IkkIy#oeJ`zeI6|X;H2>vPr8`o6~)JH|$O|7FP_BGPR<= zNRJn2YP-E9W6jj5-fyLuyROuT(rv<)_cfreP#F6rIM)&M8W1;N;E<+TtS@2B6AS%R zRhmNO*{tlFJ}c=@{Nh(x@z62UrK@U63DuJdIV7pe;R;GJF9;_ns&1%{s*-xNQD)uA z9C{4)YS}J7n?5%!eX}!Bf65YRIg%vHdz_U-1BmV@nv1HA;8I5UT9MiW6pqevIZOJR8S%HX+d9@U}RgrESG&_FSF=cpiqLl!8T+2Oohtv z>Ngp|xH!^hc&KgLMwfaPN(FThUmI5;CMtoeHMrIi(jNVDL51k8lf($)6erNz{c4P= z#KY8sBz{#Iq-dHQ6Yr@KB z0*1U%RJ3zrJm{HV>h+&hOKvPY{IY6*haFRh?r*8(7V69Qwn@8XE3)uvVLO48y3eP} zKYew!$cxrUqiFX`WYPP)<|6&8Q=iJvGDC>6SG_v?_$^bNzQrBw)xvG^OlC{~FLz6> zN?h8+o0BHQU`u|xAalPYiX5~&8OlPc8#Ipcj~T5;wz|P@R5{r^cwglQO*=6M;a_ey zA7uMp>P$qifwC=_t+pAefRQa>=!V?2YT*He{uMLrmk=s+wr0zaOrJ9eo{+!}zfE-@ zdr5~i+b-l0$hu08(s+oi(^?l}cZfwaU$FUhGQj4MY3FkVUzt_fMdYq3wyLFyckw1p zd>{H76>#Y&zHUbLJeI8;?_3HDbW<%a*5jF#WEc4u-e|mDY|NIIMyanyg_xU&3D-h< zeWo_1(EPEY*jAYeIrSoX!u$o!V<=lg7$|=66`;ioZIAnDr1@>fAypmAapRLp0|Gp5*b z0>?5crZ1362}k<8MLJ^E%^feAYJ$wA=&?Kd>)RN}&B7e_T50PEIYguieHW|Ze4&{4 z{IlMrYNt6bQ2dH7Ra2O?#b`5C@szen)hz?D?hfL@%&%GLVrLrK1Dxi9siS{&z%ejUHFDJ(U7owZv zGt$>6s+%vOowc5;SNxKX61aHoBK0xRd(%=&F`8}qHDEA$kV7nfp$^VH63srwK%<^*6^GY4d&cl9JdZ!YffRo zGr;~gPK%nWUZ(2b>lIzb^k%VR?GGTX1t=5jKatH$zipnDtFIDqBaL*%ikFL~`JChx z6Q<6N0-zo!(cEJ+ZTe>6!f>voN4LJGwMmI@4#f;^zrftabaOW8mR9^-!z4lGtp8M>*OmV)#|>95m^ zNDX!TGWgU9%TRUb#&!NvQ_qneeel?;73Cp$YA%li=2*X>7WO`7g^Dy9uJC?G=QY(} zKswJmW@s)$lmsa*vDA-@ricT$kPbZ`(tvX`rd%G1qbY47K;?n&Izw)Eg(J97>A=1r zH#q{#bT_{q`O%c;NGRau3WOcQhK~Y4xoH7G`eL#Fp)(EA5~;I|z)MFqhS!hsS6=Zx z-otRjGL;$W8?&X3qLu5NT{wgFMtJArePL;UB#>?B4C@YrI32e*nLz#KY)-mKwDl@^ zqkVg1G8Um!^x2z=5riimPF*c=Hi=QYFGs9M$2k$b;ml+A=WB}1*mt*Ze6e`tIjR=C zdDhoE)S2@GKev9CpSXD)g`}*=mtU{RaXMqdLseiE&h_O&qT==9W>!?~%?K!+w}xx%+{jw2;$GbSLhdwcIw|w+qS1AG&0+za_n`EaIlmLTMe)X(z-gu9mizc zFkLVd-)o||l*CYfcJaovCMfydSf3l&xvShgId!Gmd^x}?$QE(BI0_Q2+)1&GY}K|} zIG%<7m;ve1Uwl#VEV>WwP@X!XD5g1T|JkWuMeqEmXlbp!mhP4+zFm6fMJ*GuwUJ0i z{q7ynlu^WRjQijw^Y)`SPrspqOl0#_#trz@M*8*6pcFN-h}DDc6>c4|MaRbckI_M- zG8AY%xdy%rs-=-GX)flTmTJ2!B>ET&ezpGT+SCyppIhqBi=baLDdPtwy^Z9_>7eAh z+o*CQoCjcveqUxMK5+E&fvUb#|0T){_NDe>XP z_>M&J^z-%cbyju7m?v_4F`FQG`itY)!t1C&Yd>pcU3dwlRx6ZuPeuLoUEj)y;&UsX z(x`3@e}X2R(IdkNVx!1@7(z`CMW%`{^GCI2%z7$X-5$b*0~hoz@}tpgK`UNtrS%j( z^T%2RFIF<+*>ZN1SnKRd3-SJ1eb4~etmx_I*YVb!3&|3-k=w=3sAhv&fat$&b0;<| z&WLC&-Ls%4j2~e%TYqshDSUr>S3!I*yBI5c-l75U$OS44DQ^*&cg6@b=`>rj{X=I# zN64hYj~;GS6Q&!(?zXkM=@aX@$o#k@qggYjZ@2HJ*kzizs*mzQXI**o*}nAEj(#1$ zPAwxkB*@buoCyi38`XC@$vmW`uNE1Olc+7Fz)>oST&Gi`ndv2kkSUc#&T1DyWm%&FY)3e@g zCC&beKD^ww#-UR$qG$0NO9K{l^TDy&r|;G*heKWjx? zRnCNp{)}ayeS-acsW7Zzk1%vGj?s5Qu7d0cuGOZ5N&xZC)dK-B(oQTJZ=C+mRPw9h z!okV>JC*!;VAf;=FsP|(0O;7*nHcF=I2aij0RVUoATvE1J16)La9_~)`P>cswR!&{ zV&b4@W&%?@(6F;I(*y5G{zk*}Yj6KW!^{Yz=X{t4=JWqR^Q$QKi{@{9*jPE}fvo@F z^D8(0qWL=?Ms|AOPo{tA!}QBAzi9r(hn1C)o{jk*e16xLej4s?d{|l7=pW4XcN?(& z%-jE!4+|sPf8+D3MD>g2Z+w_J8R?n-sn4&%$bY3_V&|ae{HNW11;RrC@}cl0U}FRN zvk)e2Zet10pa`yx0W|-#5IZ|KV15P(*dg>BKaKTA@PKP!f*^Y%aPRV;D?QUs;hC9Oep`l}6G;DHBu>Ep-@=>P>N^-Z>0AA$7RS2g45lC}eH&9c6Kh90TYGaGM{^s~ z|L_&_uSNauwB%r>XM9iwypn)ytiSyRKJEYZ75EwAmnHtM`RZ4i`FC0{bF%#jeLLy*1k{et;VAN)#q_tNX?zLXM>6%vE2d0e`d5{WKwv6L=QmeN)!h z6+Hj9Fh6(AhmQM`{NH2}1oH%eCAdd#Z2l!bw*SiShv)7Ewl{VLJXk>SPq^i;1pkLg zBpzTSV=w?@3}OE-*>n6?_CHGBzuEso`@d53AMC}zY;EpY-{VsMlD+ zA8X^ch(8SWhlqbA#y>113Kj|UAc#3cDcDw~VC=3G47Y)PB7lFz@dIiH9=VStSFjTI z*EZnjzDEg5!JkeKws!p2QEmKRmH4rie<|_P?0+cnS6=)>2{|zPdw|mZ!M0Xlbxi-Y z7^eSKj31Amzs2}r+CRkjE5ZFC#{X=~dt}uZY&mnV-OTUp_b(l8{$GXskI&>`L~21|J$|yenndT2m6OL!1BXJfAM?BVEqywvfJ~z7@z6jBbNLUI)O>o$B{pnSV&|Fe-Bo>@ui082=iDf7l5e0L=es z=zp@gzxzFphn?gn6Csd|BY0zR0I>Yr%M^_rKu-3CU@=&J?!pfzaPZ}iRKXx)Y-Fx4 z2y*>#h-T$v1#qx1Y5$SqALxE~_;0A?y}t57O16NX>fAT~L^8qGroTEKYNKGA{e{Lq z9Mr{>^Z~#11Z(mGQU~AoeyS)~m4{iPK=5hdClgU1D;O^Sv3H9C*}$i_Uu|{(@b~t> z@Dp%n@z+M831E7d3wSvFD*_&l&)~0zTmWExurm1U z{&V+zF!C?c{6uS6en{~@0`q&?Qe|^o_6wQ1qdSl0)}_$kpdRfSzUZTcuCIr#gsp@f z`bvRwA=|?oetmQJtZ>@K%c%HG{M6prBhRVQZ0gEB_j0vv_dABR+l}S6J3H6A%bi6e z(CWdYuI^OKDed)(`b?K2Y(e5&See1K3gUU4AE`7 z#L{MuM|_%ZPS?CPKS*7yPfqepf4L*UjI4+*M>nfvPvTJKxP!K1+z13xCQ-@{VHk>T>b<#kXTo<}9 znr&h6b7|Qhek*0iCHYm%gzpi^e)w};Q46N`|MS2#`G0Am|$*#%i)N@HqpX zvp;I9*}!`5|NfH8Yxq@L`u?1Ef!GDc$>aF*C7M{?Cq6YHXtFbrO#OAk0r7Vf;+Zz` zIvjmlRj6_CigO_nl2q*jx$%mE1%O=7Pf}EiTycuG_*njWk80x-iI&+HlReO+I3~x< zxmh^7VOB*#6IGx-L$yZbth^x(z??u3tcB_&d%HLQ^nxf0;%XUgZ%aQDG!sjg9HV~H z0LgV{cO7*uzC@Qim-b7VN3s#BZ))(Oay0NNWd=fN^-^Tju zhH?__g1P}}ul8~h#4ZNe*E70ehfW>wkYzOBI$JoF{a&KJM8D=*k>qd}SB*4PTmtR2 zK<=Vas^{OSL)DGza=pkRx`|to8d)SQkhmlBdwg=>rgEA80sCE>kbJ@|71WbWP-D@F zoG*i*Pw-pn8+2IJ;yrJULhD6drPy* z>8%M?EKxH`mh3t6AslTNOfCh{w>@SGu#5I_xb`AbZ7z2w^FcJDr|8&JmI@IJVz%)k zT(&WwNUg84rdnUyj^#0?}2S&^r|O@X!+6cqC9bn|@=3b9I)Yc)kc_TuO$|x_+TPm{;+HJGH0f z%e7+R`AkG!1t?N-zHT_a@vetv2X^S9AydXM`f7F};|dcWCM>$P0;G`8$T(jxp+cU^ zt6z{i5r{iyU*-Vy##c8WoxrncW@nel0akM0m9kTU`%muh)ntAo%#0H%+f)}KCVB-W z6LI%5R{omtLq25bffr8TXxhHTFDrqea4MOg`|6he9;Tfl_dN`!7PZsf5DhmHp)5VO z4Npc_T71P%8K+lp5*mANtSyFb!j>-X&8V_ngm7i%#azk^Yq8~MOf}(z*+BK^&csG> zpcA`U(Mlc*Y&6EA{U0_%Xxo3VnXl|-5%X(5T?1%>Yj8u5e}qzJAR4dx&LvNwV^T-S zEZZP*@y{n-n>veoB&|wfY4idXDiW%E;F<I02--d=pJ1Vq~3YmS`I}!OU4tqy7tBjM^k}F zYz@=+Yb$Ba@R3i`wKeI_KXh<4-%T!_E+20B47IYG@iw{xeK`|RtbK2t{3}nC9GPd~*oYvz_utj+g^rR5l{DVie)dfVRUYG&*Ya3T?EuqF! zl;~$C)-HI5z46+yDaj&7!af0&2$hr+$Nv2f`6i+o{ue1k2&zSLXA4l%4jVJR)EODn~PG(*EX! zA@UH;?btD9>qn-&wQyg$CV!ok{{2H6`3*Mly0<>vls)v5Ed+(Zu8JS#lQpJPP7Xgp zyxTfTmqHCc!g-v5j4~{Cft1%y6L1E{+wOIR*n9kBh#-KifBK2ps*YJ9$Te{3Xs+q= zD~^6?0`k0c%F5~Msabq~j8Q<3xrTz^COW%#9J}m_R!A*X|dD+#8%>49%p6*UOk>OYUhC@ePbouwM8n zE^v5QqvfFN$C~cX^CPy8$%v@-A89=!v)P+G=Mu1-V}Ja#=jqXy>yr3)h<74m`8)}F zpYb;07KwIPN1JtCMEdCmLShRE(&{fm9+5#|X=!LuQUM#Q)n{octJF zA-bm+Pm^KoXC7@To1Dm+sMI95@!4kF|IosP=!vR3+*!w`0KL!f1F>kl?NSH~Hde%f zczV!s5g61ksOjX&OjwS5>FPak&At`o5y@oDaK+)3MCGoMAx>fP-7Fs-6VupPx$A|k z5TQhr1PuF@qvsb`jI~w-v1cj39ZbY;f3w&&`~+rZLDVgmlSip)tw1p&DDzazI=K^H z?i-ec1?rKK4A19@?BYVgp~~-o;ozETnQoZaiw`(${>UGW+E03=NO-#1pR=QXM}MAa zJqvMdg;8cJ5YJ=sU3>(3N-k*e*-7MA-L8?a%Q^Jb_F#)-E+~!X+_DWJ35qZ;l{}~6 zTasQcmhJSC@QRA?RLfz=};7e zJfwn4e34u?%j@g3rE3-S_=}k_g(s|1i6-A+H{)*Z%_vMja8_YCucTQ~)(#1(4nN1s z>@*?r96X&Ad8HWHEgemrjW0M@Bp^ft+Wt4}adNBKI8O0~!UV7*1Hct7=;vKy36TTg zo$@rC3lY4s7@WE-*F|B7;(JeDS$#juSJ4G#{pSV7H#m7Qq|3`uotnX zX(+#(a+TR}F1uFtzRc)9D7-cx%<)dX9gNRx6trc{zOKzd;|GrgCg^sULXl%^UGKDS zp0VIjWWU4zChBkDhO%ImEG7WNqnxHNj;^2zw<(6)x6*kkvQ>(Z-A>QT&36F{aVEng zxLE!o^KAwi^oHmVU#wv)T2`e$#KJSHj(HTfyyztRHcWTHezjiQ3mm*5cEk7;JJ4yhjjDAmqRF%} zP@kmQ9eG!^=+_It#!T)&ee!b@*QtfMa7PyO)9yIys>MU(YY6_#!w}I$k!$IOV}mmG z)w*GR^2Wr=!@f&~0LYL>D~1uKJ)9^0epY&^Tlf%fk2v?ttf4O&TWvWxZ>K6eB34}z z9_9w|BP^VB>;}di)ET9`;3cCaf4lE^IT>$HwtRkpXah_Ou!`RxbHp(%8^4PRJ9im@ zZ(pAk@ug7$OB>z*W@S|DS_6b zF$avnR|~Yo#b!jSsxV!t#zbgH`k#nUl)eabTwAQ+xPDgPASZw*E9$z&XmNX2VkZ<@ z8mkl;7(Tnt*$Q*V92<(Za1xcd6r!UjFS@_bmB^}6Rz)gTBHe6IP8x!?+R&smxX{!j zON=?#Dm~W?U&W9*ysYG)C+#Vs;Gumgozt{inNLk08XM3{8s6`GLR1-)ySLY)c< zd-rCK0o*W~2!xXK>q^J$B^zh$1c&(kXNplZ^h5ns41l=`P6z%q1zj^Lq43IefW_511Bzec5U|z~@Scp`Cp-j^}YBGVZ3OvqR|(=~7Ez&Tkh? zk@g)UA1~J(8c97!PWa}9C&x~d1?`fp22<*sY*m2kU4Kygtgz{_gzX2@PS9 z{U^O1_ve}ppGa1^(m!0}9S@%t8eJ&3$R^py67IY^mm=ryScbBfl**5 zMk004B4&-g=W^dMhoh(^y6T0hX**5NZ{B?-+cB3FvXdonra5?fyN}nO!o9F9-_H;e zg?=spf zI^|6Lqm46dEHRqdoesMdjA95WNk%7%^66$CyMwm7vB!#IBulT0`kmd-NFsy1bl3$F!~^3iqIVy;@as$wsh zZWZTNw94=$ezK2E^Ti0*QDckLB##}QKO?vCvF3j>9!Zd3jXz@WI@m?=hX1|x`$e=K zZ#vGFC6NK(7p+1@0^^xdYY8ml5vlCoy=l`c|oR5B2BX zR8t?{`Evq=Qxl?|TY{-T_pR|@JZ}Nu8I@}q* zqmk?)Ia?ev;;A9aBt;%p(Q|x~^3B68%RC)tWNUKDKwC--|5DJnD!*HBeKFKm(;j7r z3T&PBxZC9zqhD)qc>Y8JYhQC(v#DGQkanb~C`3^bx+>rn_D^Fo9x$JYhjYz zLd(;60r9Jwp?MBC{@Ntp)=FlGM9Zbt^;z^H4c-Tca7na}HyzXDp3smBP}ub$mg2|e z(0O%&37-9>zfiRa7ej-u)O@S(F%Wp(rtjKd^PH7A)saoW~ z%~NtFKgUVVb{@z*Te&AL&tQJ_{V{;je2q|i#tkRhnfKJJZf^Cv!~|vM0;DJJEiO;Y zYa8POd|CN;Mdmm?$XFZ9TStR4jprw01VS$jd*)XHyitMXB1M{0w&JBS9v=0`Vwb%!F5v;%9NHwmg7AveQ9N`nt1S3z)VCS=qPI< zvTOC37M=iH%rk8(KXdu&JO{`jP=}YAvCsrU*+OOA0Gg=A8A@Wjj1;AYK!X!d0J8r& zzU9&Wvc&~-oWal&3O5trGpgN&tE?L;>e8t;;<_iM<88j`H6h80=H$GSMTy~N9upG# zijV7Z$CwN*)umLZQmi%f(u%>+-38)lkg{|5Jl;7l#2nh!IVJq~65lszF0hXL+Ux z@`Wc+&v-&y{37yt|MyTM-Yy4!wgUIc;VZi+!?U>+BOg>d)w|NL5}Eofy@8KjJE8QM z(`Eboas!>N+oAax;EyjST>}}KJE6FC>O8=(uH1rSn9t~)nC#vx=^rpoy3BD(1K??w z-+j#ld1#Jug@%KSX|}P+%I>e|2A17sK^D+|{LK$R@yTzW5hnik8!HWhY;{jzAoNQGxP7P;(KBr;K!6PVTP8*;#w z-UYD1C%v=uX5~$KU+|tD>v=)tSK;`1HxSLv>c_J4_OdXQiVkm=S)?6O}~i8W;HEx@)x1J+IhXJTY=L$ z?p%>)!_mA`+RYSYpn;9eMXDKXeQ@+BcPiD@Gn-bkz%;S`75a{_XMkLN2qtmiO0gdL zn>71^@J@t7AT9TVj6BAwc_VDVMuO7d>Bpya6ln0NeHhyCGGB!$;M-CAuo)uv+*uhd zDL4$AJz+Dgnvz6qErZVZ)GWWZwHdHYJ{uff_neb1G>{xshNBB};`<^N@BY^HDfslU zm3dgCU?bYeO2aGA$b(l$H@EL|=RwNLAu`8q!t!?L4Sg-%lycMR7S_>D$@Mmth_r z#Uo0QaLRE(`C2#%G8|+%6Cb84Qsyg0Y;C_rj&?Q?W+leZy2f@6k21+@B`OauKDM_L z+XD_=qm49aP63$c&(4cWG{i%?UYsIPyP0evlsJ{oBE6`x&855;;?_5wdwoHu6&ljn z>Lqx<*hi2fsSq%S_H0RftETQur&{78%hj{bU&nQnHsV(90AB-QnfNMb-_11(HE8?E zN02_lrxJjR51VYTx87L~w*Mk}7)0ht1jQ%eXJe~U*sT`eac%?i4A@kM?!hQI?WEO5 zMDMKCgWKecF1xr9D@$wcNlv;YIGPn?ng9to+v8#2k4o3+DFpY9+rj6=l!@zUM{Msbf@uraazeru2QuQ!DN49X8c*gXUT zM%fJQ&27Oy+57>?$m&~z5i13CK>+~)8f9ZOF*kc-abtUs1D&F=sgsrd{WL3mQ!u~; z_(34}04~w7f)N!uCJr!^!_3MI0D^zIdH)WS1Q;nZHx#fj1^+gc5lkT9V0iyt#KZ6d z6!viWKnDh&^lg9sO7iEplB2P;DgZ3_4^H6ULK-sxSbyQRfS)MML&x7y8X$lJ2o~k{ z>-wy};HCfJ*OZZ*aB6WcYtQHIJ(=!k4dX8^07$5zJthkY z36Vm@WG})jn$jr3$gYXgOpP<#PHIrHD#RK9Ni9WInlw`BY63Xw4*YNZ_+x~o-gXF~) z;&@aYM*;+&ilqiFd!Y%!Z9Owk%%PSG7U&H={IwypllkN9HC{69^YYZvQM(gTI|R8`+?>}HUz5M`DxhW^D)(V9QF&kI zKY@ZWTNf4p3g-lBQ}-&YGL+qH83n z#to6hZ~l`SI#~O+-qT@9{=@r(EWx(C3{P;8O2g85|pjfW}m)%Zq=EwkkM_YgoJUhhL3;H^`Sr3S%ZA}oO z{N*!}=LPD#3QQlm3PvcoZ+N~7_kxDbB$Uq+#3kAf>39Q&$=k^iU*qB_(BsK*^YCxy zMTN*`8yU5VBctQq_Q^9XXLTJu$|A^p^yU?;H558oCX;@aGUnrhmeX~*b3-DTVk;Sl zwu%vck_efKvp66mz6oaTxBebZMBJY65=BR(GbSF@$D!;a#OD)_ z+rCoDu)*_@tI3yY_s*EZMp3P1%U%{=aX5QRtJLgGz7~I!7oq;PY@fJU+QD8kso3{R zJO`06UKrL&Jp-yC1e{od>*c4%kFRl?n22>pIY_FrFrP?5J_h>%l|a%^7&Mmzv*Lcz z)7n#C--Df)^u9mkX@B$O#5Zg7=;(tzjRntsEVndg>xBm5C4LW&NjRIE+Z6+~DJSdB z#`DY}DD5Jy?&>-^GqW7c8Ki>xf~q()c*~ zI2OJ*$z&%?-_X;OcsY1oXEuq|4nJGxc%)RekaeC=ZKoqs_s$I==rtYvY!)G9dZ_eJ zHZ2W=-5AnzJRHJoT+<#ST~opiuPi6y=QiJxq6{ldw6GIBf(9QAoJHY83nU}t$8DGH!oo)*5$25-WEIV#Bswg`?Y(H_x`t|Y{rVpA?ONX9^t;ZE zB%k+?NyDB|vF@Jn!*4ry#Hh4RQiq>a7f{jItJ_>UHt{fy$G`2*I6zKgYLDkdFcOe> zrW`Kl(1^^<``iPG3x(Vb=OvL?;Z4wh^R__5dt?Gw%q$U6NZeFkX3rd(u#gzi@4W&P zsakpmC7nySi(|TVu8RlP(SuVq(k0p$@Q$3XvXhWMv37T>u0eHLgA07FhED_2hLAHX zZXCqir=k}6a~XF+2fMYGhPJk`pFjrC7nvy16W%7b1uG)CD6)_O_+J zD?^kps5ZaBZO|~`yXN7TBe$B@5)y(>Ws=i*rS!UXHT~4`b}HjB3d9%dR2@FuQ>|_S zj+(;{Vx_Zu3+Yd;YIWqd*LogXhtk8oy)r)@4N`kmAGE-<+;lRA`}B$B44}l-a9;^j zstZv;gZyrema-0IA`CfYYXC8;9hhsDW`I84NqKdMZjAM~B2tW+mPg^tbfZOC)6Vhf zdgv0jW$aKIAx6CQmo2LXYA(Z0kEXGAhj2v=Wpj1AALqOvi^I>~WRnbfBw@Iu)L>){ zdL*a$F`j9!b%tP9iGJW?&nx6lVWzQvsf+m@VKF19pJNec5OFXY^;T|Q{zzY_%mL2- zV1~4FDaLEHS0MK(um;JeXP#d=F0J-J(rOAnGz&+x0pB^?E~iypiK2I=1~FWocKw^v;%q8 zHKTG}Fb$_t?{#QAMzJ7^609HjdKbo+2SNmXA13U!(j7_0YvQKsU>{+pU~+gSt!&(z z<`(Ez^cIdPoUpZ98?{B?1e8N-I|SK;zlJ5=mtg3PFVobhe;3vNRHwH&dY7t*w}Oqg zbt_=4L-(C?OG3xh#@#U~*@XSk79)QZvDy5Ew-}d}A=(s;(Wqy9gztgF@T#(b%MASuwD}EF7ap|S8S$K^gH?g1c zl^&Mk^oqEs?e~CjED;;g<|Os3mN9$B1#ge*NS8rsxn`mlmzt4@C4vs>^tjqQwqlxu~E-Y~^E>3=$lq(ULpw%0( zLs6{e%FY{PA=Go1bQ6^6+0H`Yva4Y{saaOiziP>2?Mp1&Ef#oG@x44G_zNXrg(d6x zcemv*+nvWOw=j5zyhDtxxXZFD?)Y{ZIN@KcAr0b*TA4l5x6=}P*_5{Z*0d)FH4z#D=n^b ze&on|cZ1E6_fGBvdLY0%HtFLD>hu$F&Keq>3yJwOOhVNFgC!BMeBgNd#seZ=mFQ5g=LuzTuF%7rG`&FH116UKCb+!3W6N(=OuV;xu_5(o+ZKR9X3s z+>&9?xQ}{>R6h1zCZgnQi`i`tD}@oM%&b#O4-*m1K6*@t|0U(I@0+J3eglFT z&9Czp0dlSkS=n<0*_64-y2+>)&zN=Q?#@WdeCCTi8_20_*20a0{JHjXy*^F$UlLz^ zib-VGK&SVk?j=~fL>%ZZQbW=7C2~T>YFB*qH4#0bXQ4-}j~2xnqxIEHzY<9BdlL$* z2O1Fl!*Nrbg@ekjje!t6g0^BNwc=)cO={Alzvm{y7lMgT2k)v^b5M^RJNh$X+e&hR zk!dw|Ciw(jPG{8Z3tDn}o-g*S(tRxRV`}oeeXJCPW~j2#W4dIZ4ie&C(9}behAyCC zm{BeA?RN>lFGFyC2NSE|-NaJJCReCoB(*U2>iai9)1?TqZjyHxi@PDOa*V6up#vG7 ziWGMmMtTi`gW?6N*^#{>>C95; z=y*uXnV|P|b^;@t0XA6gl$-|1HT0Iuvp-~iz0Nk@ktBhrCLg3Sr-N;@vcBA{Zdkc2 zsTO~-6y-?nIQLQ#8hTbyQ+R64(y35(3d6VvfekYn+D)0uLdKu-bYDF11Je@2)Uh${ z>C5`n{h1muYMQ-B1Ot5C_iV(nCxWNR=!({ElExWyoTITkQcO!YeSA7#6rsr>*l_*yroIV%a*YN zK6Q>;3v|nt5SJ+!${_EOYMo8FgW|W@2riXtnT&z|9vHRg;G@$xE77$wBuPdxGZ%7mVa-rG*R#KO?$$*O*4|iux zJgA487sL&3cpg|>{)D-gRQ!<%Qq6!EqNl6Mf_=+Y%*SW9Diq!GzPP9#1r}a^?s3*@ zeI4+QjhIUTZM`1y{Fb5`b_dky67`}^pV(2sSH~qS&pZkMlZzz=GaDUcE0 zl~p+Fer$VDfNDwqv}|d?SlRCasog17&}^p1sQl@WmA~pF#GYaQp;Ie=CH?`HQul0O z9bf*3AT-p}8BMGAZmmmJ+rIJov0u`i58|Ha>I}_fk{UT}c)EBAxa=~9uHwzS6DlwN zLca^bwqJs>+(OXYX_;N1R*Y{mywlaVEw9ee#`oQ@D zDW{L#E8i+~5`}&N12zv~?)GtF;YU!^uaFxTwiy+?eA-aplJq2yT|E2jWjO+P&d?&6 zQe%a~@C+JW!!2koM_Ngd5Z<-!T&&MSI<{9GAdDYWQe)A>RycBE9-n($jPn)^S@Z3b zIBXq0eJ0zi#M^O^u=J*k<+aL&nq0k25=tsAi+Ak#d(&%Z-UMj^Wz+2(*Wk6cs?=gZ zlvEvkd_KMi0P#uc_AzGQx8(Ri%ZrDeQo-P|ZZFg7agdQ&Y+pdkP=I%ka z5CAJwXM`7l&ad#yFmA~_1uHHdlMe(GcF^TCKg#Wnt~5WAEPRrIJ@)ptEr7DA1)F+H zy&OJ93zKQyq0cTzBb$&@N9y4N7V_>@cYzx8)KWdpAir|u@AFs}%GZ_X;A>HbAoAwEBU0lO? zIiK?PLHHxe8wDd+JC7*?!NsYg*1mLTpNYmUV-<_THb(YnpN-?;K+v>1E!uo=;_9&mExMjxdvp)4T}KQloL1*}H^C$&nb84kilJ=6CZ`D>mg>_spkw zYIGCZwi^_Wx4tOXt9+MqAQ^xU3W`ID-m>@(^;WLNXoIDeg-zvvCgmxWby)=m!{vhW zI!{&z)$)W62W!qUHwgA6^N=Cl@_};(#_iS=1wr82Ng?XTIp=9WzfsOnU9huV(Ys=; zo~3B7__t=4X)e7sCuF%wu~~J%xF?~NKAXYkSDY;C?Pj+!h{2}NPKfTr&lLGgxkZ|j zyK$@WxO2Tb$+QM5(BPXHws(2FbELT4P)oj+NHc^W? zMJSDUiv_*Kyi5ZHbvbV(*o7y`B%hOdnBN@+i$F%~oukTL>Y<$Zg~((M{-9W)`!b;v2A$Uh|iVj3<b0!YsA+u7L7)k%h1%(y^z^SoAWv2eY|tp zbBpuSeTMqG_M50)DC9t~l3s&o`)E3%GIuR}X19S|;E~~gAzRcI(eYODm0?Q{bfgSX z(?lU(tT?>(-KDFV%kHX7t2^V}8YB*2)$Z%A(f|iv!zIDGe*5PQv37BB3~9U5+%6b+ zZ7=p%1eGCP6jWrfSEgr|kDAxFAyc~cY)`3hhffR3#bL}*Kuz~-Dr=?ttiT-xDEmL;P=&%9g>ogxMXAnsQz|B$SO zqOz*~NT`?Bv7Kg zR)sAlnyIBUr^D!}_XoMK)**wzar_+_a;N3?ckdjsrtccE?9%DNi>W8=AB{ytd`j2j zM9@yR54R4?bE5%F%6OZHI z9a`KaNA46b6@60h1iMqTFEF%6jBTDpPz)~d@-|+K5-@#341*`SOUq?vvzPi~ z9>sWPkSgu6g0#4`R#;t|&V|$yG6VR6_8J7uA^}SFLB(i*HC=*1kgVErBUTju`}@{R zk}lcBJ_!*B?1K-V%E^VF9g8Azeh84cGdQ+LDcYG3eooz4gAnLusf6BPKW3pw^otqvG7J;l zJUnZ2tECFPh2uMt=7y_W*lF>hhdRxuAx_Ao$Rc+RSRZ^T#oT=G0WoXZ&rOk-ho+R1 z449dEKk~_)(pBA|fUyMh+IuIDXiXjN1-=80GKD?Y{d_f3Ez2OFw|EdA_C2XOfm=HuK0MQNrz~0xo zw$HDo26r8DGZ)Iiv+ybHrU?l$G3-VJD0U`d^E}h^G6kmvl2}$XKT~B`02S-(e z__An#UDAnJS;6`M@NND11+apBO~GF_u*)YT1%Uuyj~hm?S1BVgE7+&xhdd-7@|&6c zCJ#~$`3?C(H~kdS4cS||HnOQq5SIA1J=R(eAc(FD{Nq=Bk*kB#zSNP z&#suI3D_$Y9HdiU;kg~Lp^2q|&8_)nJU0Y41b(w z1l-=y&E2xz)C1fMiRI>s<@RoGCq&@-jsUma-dq9suQP!+lFYZ0ebYPWw(NEyZz?d~ z3>R?Q?TrY_%>bEihkJWRH<`d2N#4JUk|DDDcT>Rr6>~8A)uJOGtztQ-D*nijf z-Z%mB@ccvL`_ohZhl-bpn1z{*?H7&jt^4DDil6JNdPh-lvSI#;-3WmwkfVnWmD=dR zgHCHhoir{XbZ1_8KYs=T9YSJQZ=g5qBVm$d8uE&#U7bQB4}_D|%PLIF^+u|jS#v`N zLaBo5Z9bdUt1+``6-<`SZWZ<{eLsJ{-y`0%r<-c~z4@y9`{~t;5iuW9qTnfJT*M)D zYRk#@ZCtDMhi~w}Mj@Y0-ma(cf71{kG)f`>HLd7o+}m_zGqQXa=)7k8U7*Jcn=Q=D z^2jl@G~I)+n{3Q)cGN&(cPL`{%oZQFdb*KA^l<^X0ZF)i%iP4e6R)?@B3m$eL0ox= zpL4uukN$hz+n$$|~3Ni0v zh-w@gR;g8r)}3T;CsWH9GPkyYwTpv%sEVj=?)$Aq-9gaRrz*|qan*+1j7I6sYbZz^ z6KxgxL*Z#9UTCK2^GRfrw6dlnSVhTDF!KbHjR2^Lo%+@J;W~Q@gG^fGMR_-!*ix}A6`VbT z$4fkYPo1QXf<=xy$2~-i9ya|N_rTBh13ngOMxE%BEY*)*iqGXqbnakRS@jm?K^Pn*m6@|7GYR&0PRRTYMqjJ(L^%<;PMnRI99j<4lr`CHF^{m7&1()Gf z`HSaC^6PTjt4HfXw9LCs8JR^~-`AAB%Cx3(Uq;j#ex)vE9 zNffea&LFb+p&P(~YN$P2@X-)#f!J>Hwwi)9>s*9j5X&&2zYwSnGNgK~o0U_d0^qW) ztHdvqS#U4ho=8-uq}|Ys$m) zix{;6hfI$w=mFzc3jd3B%-N6afp#oa8OQoKj}ubncn_+zKSn|?`S}6znF@4WqgzOY z*+tIOQEbUuzH;8D&F6hETgk|y91TNZiZ(5&Q%+F+hnR+S1qm7(ZrC+ z+y_Y_fwJ-pu5SEzR9?j=6lhFl=)hAtSz>ZY9&GzyXJtg)4ij_hxKSj>c#xu#1B_52 zXl-na;njYFZxel^!_^@x6f3V&pS!LM)$y46(z00>w?+umiRHPD2nXMR2a&s?vKie%XaEFsvU3SU3`CL=eRJHo;>=+Qm4P+yB}Hm z)SK9XugHFd<0yU)v7Y*PbRhDCLDup35^b=VqQf=%brNKkDkGHu{+;c1OF`-uJ7 z)lmRfSdQ_l(H>={s$@)nzzM;9J;xB-ZH&Co3`uqrVG-W>x-VHqckZNL?#^?wrqf`z zz$jS?nl3&J=(i9>6(|L2PDOB7;-3Ok6 zBHtEV$GS(Tcb~p!p#)pAjq#N8XWaFrT9s~$EZ4zh3eZ{=?%DZP(eyR@)hX>|Z(MH1 z9DKV6zoveX5s}VBa;Ux8O2WqA?5A2jXELnsvU3Wusty+ukt3l zeD0Xl14^TWMo{(C;D<}m$(6`@J_PG&tn~SC>U&_j-!&p`%ooaavT4}sy$GB6_*`=> zi(%^QfY4;Q5fiHSBSs(`pJ92tLKBNqqgLA}9aBP8kn9W8*j-6?8~LY%81%KW5;3cN zon+;M117t@1-vW$p0*4urx%iUf`m(hpAkAqIqlzx-%jlvW%yVSXcQYwx}cFk&*qeM z#PC)VakygA^>UNT^K=2lUs=Rt9RBCt4IZoUpU3^=Zc^@TI_F}};=5_uUpyFW(0;5F9#zGfAuPlQoov2U%XWYsw#y;}`kS3z;@g*y__e0G! zp{iyF_KybiQbKPpNfedv1!S<_F`Mi74K8u2oPlc)4b6 zW+xGMAA5am#TlohKfoInZ9Hn}Kw`~5j|?p+Do0Y3*&#HnkK9OdO%CI!Jckk$N~!4C zasL$tk1x-#4pT^Z;7YuSQDMr3x(%AGN5vG8QV6vr$IwA_0MNgCNU@_Dm-I`ky`a%R@%i*x?Lni9)PwdfN3vh`IM0F~ zr!`tF-|5t%KqWD~;P!qC?boX>M4u^*&P|*d8W98?6hrHUxFA`r%YDN3Y$1~D!cv4) z_+ztow7WBI>Y&i-v38v^Hx!ZG>n>YkYdrPU-ojzlbn!Qs?<)&lD-<0v9w-=E`!GMS zHb>a|Kk^+ZYHa^&SQeQak?D*LMMXRYAg^xRKx z6XT#Op+xwT+7{B#1ek-lb-9FW4VZ*{jgiH?V@cG-4Rp{x=w`!kcr)LHp3y~KM+md* zc@pT+qXBU=b-i~%(33gj>WSYWX?5tPSxJ8x}jAQNB z)b7_ha6ZeX8$W!W1|?m)Ps6qAmX$TV?r=z$XPi0uY5$v3{N(8LlEm2I#5xWU(Y}+0 zvzse{rT@UE@}tyU2mkmltej^TThuh^pG>|SkI$1SDpoT#KAUr%3GOs%s0`7rprob#dfPtw-tKz2@;%jVmeBeW@YOzh(uEyKP@2t#vh&+XL&xgrVQbLah1roQ zs-SAuiw)@XxZ)>-Bm9kl@zvMl_}9)>&G3U<`;(lN)&v*P&K_ZGfp#=%k{ zZVGTCno#gmFJmllnF#U7R0iUt3wJ(E5~VqW=`jcSrAT1{^a9Bn8)9gv%U0#K8DswjnxIm zQ4;#si|}r-cXBJQ;KpMYaADjzkw#WGM%j()Gu{vH=Hy*B$dZ+Wt8^sJcRENVAXxjKnXCTz5z>F{%7epjAVL~bB< zDvY%V;i|#PXy}MbOa>E6l$~Zt!dT|ou}5J};>7tCnW;deF>;s1srWX=rK3@jTwnNk zUL|KlJndn1kA$!#-d;ZTuG~Y4LHTDae#bSGW~5Y|La4@$2ioUeZq z;~yb%fNVhKe-L9xu$zBNG{7A2KNI5{i10r{hCevq-`wy9S^Pv8U}E}T;$jFgg`im= z+btFbBUK1ig`igmc7^$Z`-9ltV#k|SSZ{D8^DRrhX@&WQef~h1w~+Og7vEyl zn=6)^3J|FKzsfNGIWYW1NA&L*=FO}A-vZ3*SX#dW%Ob`ex0nYz7Op_fP z&=>#!fLK7kl1%`R1rp7e4IE1ESHSt3@D1i<2GX-Lvj8FB^kz@Hac}|s>ELpmakD}E z#GOnaMzAvJ$2BqNzw~g~#gj6BqVjdR*D}7Y+)knZJ%4T7G)IfszHn|t5J#3`kd$eA zs(`!Kp${|f6v-$gq@_sK%QC{ttptHIDr_Y5_={eh8N^|tqN2joO>PTc z<}aqDlcXwnMLY4%2a-QueSUe=)h`}Wac76q{8fdr(dNwVgTr4(jHF3uBVs0 z{^j3kF5FFs3F=&BuK@ZvMoZrYpv^TeR)nD^@vJWUbnY)`TySrEIV~&wG2wz&yAUo~x|)X_T(j+0gDv_#J&)7i{g+6Y;O0V8`sL(=dBZX zhlUH9=9V69O;LlhF zb2cN)iC+zG!%w{}n+)FZwGl2Gsu+r$JpRVz+QK!meWzIddyDongZ{OJjgvfWEo~>U zGKPw_Lw}b`TGyp_;m6+qO;#(D#qVH1hKRHVgxgin!HGI5s_|OBLuNbnPFnus^n6IQ zFpNyWi>wAw?B<=QQzby6#iDY+G`hA|je@=vgKY!{eDTd8iN|s;$g3qOrsKy_c!v)nZfxD z>BTQyK|nS*Yo&Q>2l7l#tBaNpA1v{uuX>PEPOv&yVa{g zKZkL^#o@R5;zZf~YMR7ML?_FW*6FUfdulT@tHA^3R}7^}WpCw3S%i%V%LwHZctY(v z$=qCDp@IKpwj6Rn=sg)Xjrm4XN*~Qw5t`L=2%U>6P+OFz3`Pr?%^baZWi5vcH(*by zP>vC~sS?-crP>MEN9}&Gh)z+=ysBx>FN}MnU8O!qC{sMGdf91G7bH3M^|nxqutrPcUMpZ2LoRSrS}&cj*pDN+G#g-uUv8Rl!PHq6 z=f9zw?LsdHk0GSC=AF4~QiRj&Q9Tj3 z7E`^#oT(p06Wl17^|G>iLlr;Dd_zv@ON}3(`$LZ7s_rm_`tm(KyTCLru?v4|uCZ9U z1KD_|Cen&LGOgU5>KeOyvZ(n01D>AA%=a)=v8{+_sz!o}o>DesRjMrrs|RqKI}xiX zrnXEIF)U+h%lJz}p3?joFL^VEirw+MahXzuoGLlgvrOCMMV2z9?j28O)@Kd4l4cb> zF%h+Xl*=hDUtN%VXcf%#|O5Qwj!61jC3ZSu9I_9ZC6vJqq z|CqgW6b^FU%aJ~^i@ubj@~AED(6%LQn;B&rxm=rECi|8h`4&WWH3|;2D7ItV{RXn$E%xqt99meC0Y5psarj-(ij%0-P9&Hg*&#ksIP)mDJpLgcW2tXH(w?T zn)hOnH?OeB%rUNBA~0F-Bgm%PcvNNyblcufD}Q+sjyBl_@jw=btl;kX!^uoV`{e}DT4WWA*KadEC-F5&AIrLXe|w_I zT9eXeW04sK66h1HLcnC%Kyj>Fvc0p&VIUK2r#u1@^Jl3qv#St=Swkk1yUSDO$tqCn ztqD?tW}kjELd=`VQ{)q_`2sU0syL@PWIIG$X;!}X)#-v5yQAn8!W+2F7ad_zEljW6 z1y}=9?K3@XY%AY^HjSbP)H`*37l(t4hn_yN-c3<4HDEZ#6K@euk*pLoDkVp*dE>Y) zj3~oi9PN&yR>(h0P@V<9e_|)nrJrh7VxmElM7^gZ$!s%G5*fBJUH7UeN57AB7Q2Rz zckc05AYBZi*P_UrWP$&vPw>=IwA^I)1@xz@gxUtrnwBx_o zj>A4RQNffYdXSyk%QH7HcwCW|3g`QYYjtOM`bGAes4B{=oNwa(jqs~D0~PB_6!+7e z0r;cNW%WCw922R?TdW_->#yd=My&u>i6^fTw!MYool^zpzmt4*El$tjs$@KkR+3^h z=(SZd#H#j8Y6{JLlO{|guVFZ<0AJ0|OVJ=vyDmAfA3>4l68pYB?P6@R*l0p#lq&#t zoqsfSGAkMJpy7!4AmJ1Lh3e63J&J{a-k1Rn%$^k~*-uicXIRNJfr$;6S67-;tSWuv zs8`9hWZG=H=^r%%hn&@2AFYeK?P*etZXCRiJ;ONi>ISMT@92IKpMI7)Xj2-qreR%b zH?`?{W+%p-UL?J2l1y){oI*d4ZpoFliOJjE_}Hi|&$X|U>YZUzVmI&eWtoGRKpCmT zN~5`M_UFq)oX;`CE&4i%^!i8%r(*)6?>cyHO^T^Mx4_!tZJEYM-#(&X;amn*Jvrz! z6uB6lmIJ}M99KR0j$W0OwZStpEhn#r#w%w?!E*tJm3|SkP7D5}h?PEVR2wbj=482S zJN9vjmYn$j^|DSCWU|yW;e-P>e%MaH!eY=?Cs+{lGD2;AcY*!f`6U4jTbc6*3;&YPR{ zJ=SC-txq5C1+dv^vfH#bJ>HN~SaOr_d%}>(FyI{dphp&_%zfkRbt816x4I_+&YklS zbZcV#E54BD51<9g@w~gZ7+*bUBxXkJaU#azv~OxNMqNZgNAu)RRr`jaS|&hVpb4*6 zYL!X3;c{<(#d0_`B%6qnsc-biZ!w!jG!k8iVx#L^=X;-06lu%~qZNggn1LqR{UZm8 z3ZaKi&}~qxj{aDkey=@G|zpIxW12h=MTZ%@k&DFLDf;sSQC81Q}pcX5MR~ML|s;z56~kr8AFt9`vWdh7^kDFV98VLQTVLlujNo4yWj%W+2`n-m0fJ zDV6j`-tr-yG}!RLf7k#x{KgabL8Spj^g)I+Zh%lgWDk~G| z5SG7oz%H$|r(|(tbe0WZ(b7L=CjCYxhK9^UAt*3|Ccugr%Zx_T!`aYAlTeEdc<7Xy zar9h8fShC;`3%aU64$9f=TIB<;gghu2P6H{1mbTp>=66v7ZVi_q-hbP;qK==D@T5+ zg)>*0a8`=Lh7}uaCEk(s<`KN$ds7=cj$RDGV!{X-m-zkKl zp;RT##6OkIzbjE(lT^D=J+Y%)5&dq7E0^|N_RiL`&+OlC{Nc)mut%8Q9dvqdNCjjGUZ4Eu3HzZuRUWI6=P zvJ^Cq^XnxmH`PMNuZuU!)xE%TE3ssKMUFIylRE{Nb!e`gW1Q79Yib_fQsvXf?g4EN z)PSriKyjsp}M$H$P;n2-q^;z%}D@9-$Bbgyaa^e|g zktlCDbX#hylecHuPb8N}1((&-CKu&uyb#_{SI!w&RRx+3Ln_^FEdT2m0e*KQl;UCg=(Q-|WZI$hXBOdN!_HM$sg zWLteaSfWD4UU)T%c3al$D#mD>ggr;9()#>bWMYO69rtotJqu|H!8P+JNnQIGV{hOo zA|FYuveh^MkFVTYuN8rzRlrW|OQQ|GzWMg5a!zDleqkzta0xXGdiypHY3WQB8NNh> zn%!3ZRvf{U6Fy$yvHVxh4Zmp7SZzlc0r-&e7&sEN@`N2%jOQHqud?`3ogdrywW5|Y z1|y-E<^bWUDY2^}WVzT;yE70e&ogiZnJ@<790WgezeM%_mhkwlP%gAhZ;*EsZ_I}W z(o`>G?x7Vz;Vr8@jdF5lKoU{62RvidmR9#wJr1nL>*PW@S88Lo*UYm_4Lg5EIGBsJ zT!@BAvFYtJff$H&%I{W-HX__25tF(4K4J8=;oUpDQe~u3+JU?T&6q0gP6pt=$gYLj zf$6$somJXmDdj55zAcMz&6vE!Dwy4$W1UJ>`tn|6Xn7ss2X(5|NNCncEGzC}G6YBr zg&NHPta;J%EECTXYb7=7B$rp|u+l9PJH^z=dFq~HqKyKqXFpC~xZLRiu}Y5-97rCG z+KF&&hIN`WK1ur=)*0CNgr|-ZGjB7DQT0w>6o}_y_?-xs&`L+RrCTS{BO>WgnF`0V zGe2_a#f8k=S1LufYNYJ}@%@|8eLh7?%7*me;nK*WuiMp+x_R3;)Vy z{?~x;H8cVr2?OrnU)v8uLg7Q`&aYOppnqaD3k0$KABz32P%I;asDh7l6Sy|O{ogXO z{Av65t3~54OYbj0`RkhdFUjQf0{`8#@_KFjVp<9M=cM$fUgC!7-Nw_A{)0@?{Tstd zCPolDJrHbE3H%*TvV)HuV`OIqcg)1Z$_zfk4U8)Pf54M}T4REKK9L_dk_pUG8GjAH z1GebAw&?#)=9s&9Loa^d$;%22abG^VGC7*v%mM|6SwaVB`(;T?M*_|ois`*TX|AEu zof+W7d$wssB=Y66 z;GO%?`uXMdEuRGL_?w4S7`h(Fn{8h{Ya5hVbSz?>+D_DF&>iEohK4Q)3S~K~8 zCe?d;^1R&obVvrNUfX*c_q^lm^nyqYw-iy-9^DTA6x*pn9(6L$e{dIrv#SMea>3th ziBs^Q`$~Lz>j`4W%TFxINmpuL9iiH!wRlc!r(SBm9K7_k@0S58^SByX?j5@|a9ugB zFI-}s@6dZ3qy`^Jav4Xze!k$kHJi>Pm3fv` z5IePLcgKnlZ{9hKiFrT^v_SSJoGeH$i)~5Xd;6?0dy>qd_Bc{Ie%fu5*M+-#FxFRl zsH3rDGH+^SjcDV<tJk2k@kJumuT}OdS|?dcN|PHcgE4c z6f^nlH!3UIfh^utr2aX zJgk-dUZS1;T&3@7WSc^}5#T6QmTqua#ISk7e!4Thuzlj}u)f8s8VhgUd8*pOHKG3! zN6<%8hrj`YhoM4c(f*Y9odos z%;3E@O;EjPZ6_x&MR!viJSU}E51=2dSwVf8XQS`fB44zqVB)ibnjkTaV#FU2g~BG9 z7lEp}Ysc-0Q?Pz?7!+{S^Gc$#v1* z2M?8C2(dSax>l?WsTSL2_Fzd5Ip(3KxH;8>`tytcR9b)9u#igOmVj0-=;<$*POAV{ zY|9?TLzripg_`a)qU;&xhooAh=ZCanjY696e8nBW$Q-;gouLOGJ19lvp}dX~cBPlT_0i0u*vlgbbpImUaOm3ThfY6oCa*((+Sr-(T= zTHyVKQIrh65sm=1_D=bO2@0%YQ4{RF9camPM(%pn$IW`;sMYGC4-&*h?Ty-_qFoGn z+2~1E+-Izu21c}eePD~;jOX^l_#?4>3nnTYOL2RZy!$JuB-L2(f{;pKFsZ~7dLcSi zxe6wgJP=Yz_%BGM9SqGsA(bI{_kJgpSJbCCHgc7GX@Z*ZJWryEVA;YZ!WD4rhz0zG z$Z|OYUpAIM+K_CF=u5C}P4YYJMt@8FEmGeQ>*GVYiTc(I!KpxIdJ}YoVvCNsggt7) z3#fa3gX{N1LFCkZQIOUTn= zC(A!_t3ec72>p31T2O6XiTd&PHg$?Pg^{q4fH1LA+ATn+LoZ>6ptWdahJ@1G0t^=JR%#PRTsv&Xs7 zNHWSIW{Om*Tf~~xy=goe_Z_R9;|=WWN$*lhci4%D1%d2sd4@!^Y`i$^9=j+45$w7Y zLN%lAU#@*B{y>vb6r><8NDxGb-Gm%2b}sZRU*R#?4XO-JDvI1& z@SPy-ttE{vw|$V^ERxR@bYJBDhq}e)Jnq~+Evdu*oP+Vu>+-oMAMnBEK^v+-8GAYIGS`Fdw{Q9*_=)uuPP5XiX-)+EimbXv*16A zo+IoKTkuah+238nksvdcJFH3I% zI?u8kPHIB4Y%`@<^$&{HWqO%uRhF@KLPIU^nsfvTOkM9mS^L}==LQTdFGeumIy29W!ro0@m4k6in8`3E zdlO8knd?+&hCExqavzS@v$RkPC&i4wWiW8GQH!hLNK1brbp2fViHg}#s%K$)5gccV zXTQVvXcR|PrBSNeXV5k>r!$9gv)jmt;hcZr+*Tsx!b#&;dJ!R`cvF<;w^rg^Nrz2K zhurYv9a2iJPb5bEgYHk(VpD`4(v7QdjL{L-8T+w(;mtT3i(U#CbnSW`KT)wKDf_m< zuj|s0BF!)Q>rgdXrz8<+BYv4Bob-;faKTkV;?QpmQHBD zMa*D>*p>M3j%}iv49o_{P|=)i3RQYTwwGj5rCnMtDF?irbP1WKo#);=HO~5*2Xr+ynX*IJNQ4dSf@(^v1cK9 zH6psDokoMPfKbBsnt?=&3{H%;WP^M5r7@S|o1~w_)mpGWiJzyooXfD z8g*@s>}8MQnuHEZOR+Z6NGSGM%>=Oe~WK0{uap%jbh(l@Et6(+Lh-69@=| zzI-1Hi|T8<3IuQ;EQutXt>!UPupcaSC!I<1RuNb%ci3X3d#~T4V9E&0+j!enDF#cy zOzl)Fx zTivUaDuNx+5k6rI^JNUk8ZCS&#aQ$0cS4|Va8=85Ei2O z(3v~Ea|29qNke4wV1*ucimnEdvy+H{L$&J+>VTj_qkAIdU=J?Z()Mk12jx;_mmPPI zad0snVO~aM`}-tBlaTBpWZ(t*GNa)}INEqK=*-YZ^tJ-+QyFG40mubmZHUIvsjU+5 z(RKPg50*lnMraS7@-5%Z;go#`Ka?`!sQ1C~@oUOUFDb^Y$_d#ljxwoOo)AL5WCziW`OPB1XIP)X$&Ski}NzZ-c>DdmSuLqNbNKT`t_z zp!bYbBQC$D>uFJgl16P@m)Er4HoTx_*|gb!lm?Vgzxfoakgae+DBiMqNrgGm_HJn0 zxaEY8SJ#5X#&w{0vP*uza+a=NQ$)3T&@`*T z@*sBIP=C7GY`{;ok`LRqz>;d3FV=LfxCBpcT9Z{pF4YIwi^?8gYbB&w^GLbB7%#I~ zSIMj%igQ9;d7NCdoLekGzeQxl=j zSz#88Z10KYdt$^z5wAkyL~Ai*O8P&sdXNv45gLR_fW|Sz;O8%~L-54O(Zj(EqVQj$ zJcfGCdaOrD-u|$|a-+E@d`Xr_A^TMiGatR0wd|ok;87dZ+HOyqHoT4?SHCFc&}W1t z*&^xn?M{^_WfC0j$9o7&Xzkj(9qH@)k(ygTK-5vQH-F9*CtPv7&l zi~Nk9L&**_WX@OXXb&Hy3Cvbm4u4wK8B4UMQP!!{PFSR}Off}s0LGIdJF!j~3~T~r zonN+NJC|I^(zas{H#Lmo7rFc&Tg&faPVPprDK~ZASst-n5v!B@h}K!5LGGSOhc!tT z&F0eINufb*9TOKgWS-dBTrK$#R5!6;o;kHX6A9*%ChjSF0TgqQY|@UMN8m)>vD3&w zWSMOa1Hp_$ZseW+=h?FWS7W!cw$N}kFtaQ;pgO(x?Bj4J@&(=)^FE53V%U)f0me_OBQd`)|4B-xif%d<{l#KP@VMqItk$D7$i&#Y>81c zP;OQt*edhpbaE!}F|fA*A~YCeg`R?`E99th1~Cip`LqJJso<{^-N^o*@_5DN?VXkA0%pLyU{74jjryv+l$F+;HHZ+VQ207!W6Kk|T(gIRyiV`G9Gc>8-E zBiNMvuRJzzobo^A*dS-%{$3Zr#`u>U;Gfh5f*{Az{$3Xdf*jladmb~yF8udA0Du{C zcKvU109IDW@z}rRfvv~^f6f8;&cLBUewPDBtALz^{99dc9yo-^pLuM|f40ZS%m${- zf69TaQ~%Cm|LfjZ82_Go7AD5O`e0##geUlYd@KOQzxrSSu>9Qz^WV>$1q3|k5|Kjc_h!BGYN$OAF6{WU%i2y$>VAT5D{~?0?P+_zeVmE&N`W2|T<%?*qUDUTuHK0hn0+TK8Z%z@PIB056R{#|HpM z0{V0A0U%be&&Ka#0pG=6&kZd1*Lx0b4y?NPqb_)V`7;j)fcTO8E(ZiaJU9N#1448~ zkk3EX1`x=^^5+}?fy{tEp9lDfv;BD=Kvwp@-Yu{kI0nZ*`mnRn1)r5*g916jTfxNT yIb<(pxQ@~Uo;+f(W>o&g3-D3t;8B4uu0sOp+Spxh+dwuJ5F-j1nXt@Ll>Y~Y?mpK5 literal 0 HcmV?d00001 diff --git a/07-printf/questions/questions.md b/07-printf/questions/questions.md new file mode 100644 index 0000000..12927c1 --- /dev/null +++ b/07-printf/questions/questions.md @@ -0,0 +1,90 @@ +## Which code is correct? +* `fmt.Printf("Hi %s")` +* `fmt.Printf("Hi %s", "how", "are you")` +* `fmt.Printf("Hi %s", "hello")` *CORRECT* +* `fmt.Printf("Hi %s", true)` + +## Which code is correct? +* `fmt.Printf("Hi %s %s", "there")` +* `fmt.Printf("Hi %s %s", "5", true)` +* `fmt.Printf("Hi %s %s", "there", ".")` *CORRECT* +* `fmt.Printf("Hi %s %s", "true", false)` + +## Which verb is used for an int value? +* %f +* %d *CORRECT* +* %s +* %t + +## Which verb is used for a float value? +* %f *CORRECT* +* %d +* %s +* %t + +## Which verb is used for a string value? +* %f +* %d +* %s *CORRECT* +* %t + +## Which verb is used for a bool value? +* %f +* %d +* %s +* %t *CORRECT* + +## Which verb you can use for any type of value? +* %f +* %d +* %v *CORRECT* +* %t + +## What does "\n" print? +* \n +* Prints a newline *CORRECT* +* Prints an empty string + +## What does "\\n" print? +* \n *CORRECT* +* Prints a newline +* Prints an empty string + +## What does "c:\\secret\\directory" print? +* "c:\\secret\\directory" +* c:\\secret\\directory +* c:\secret\directory *CORRECT* + +## What does "\"heisenberg\"" print? +* ERROR +* heisenberg +* "heisenberg" *CORRECT* +* 'heisenberg' + +## What does `fmt.Printf("%T", 3.14)` print? +* ERROR +* int +* float64 *CORRECT* +* string +* bool + +## What does `fmt.Printf("%T", true)` print? +* ERROR +* int +* float64 +* string +* bool *CORRECT* + +## What does `fmt.Printf("%T", 42)` print? +* ERROR +* int *CORRECT* +* float64 +* string +* bool + +## What does `fmt.Printf("%T", "hi")` print? +* ERROR +* int +* float64 +* string *CORRECT* +* bool \ No newline at end of file diff --git a/08-numbers-and-strings/01-numbers/01-arithmetic-operators/01/main.go b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/01/main.go new file mode 100644 index 0000000..1539ea6 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/01/main.go @@ -0,0 +1,33 @@ +// 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() { + // when an integer and a float value used together + // in an expression, the result always becomes + // a float value + fmt.Println(8 * -4.0) // -32.0 not -32 + + // two integer values result in an integer value + fmt.Println(-4 / 2) + + // remainder operator + // it can only used with integers + fmt.Println(5 % 2) + // fmt.Println(5.0 % 2) // wrong + + // addition operators + fmt.Println(1 + 2.5) + fmt.Println(2 - 3) + + // negation operator + fmt.Println(-(-2)) + fmt.Println(- -2) // this also works +} diff --git a/08-numbers-and-strings/01-numbers/01-arithmetic-operators/02/main.go b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/02/main.go new file mode 100644 index 0000000..d8b7c5a --- /dev/null +++ b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/02/main.go @@ -0,0 +1,22 @@ +// 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() { + var ( + myAge = 30 + yourAge = 35 + average float64 + ) + + average = float64(myAge+yourAge) / 2 + + fmt.Println(average) +} diff --git a/08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/01/main.go b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/01/main.go new file mode 100644 index 0000000..f510d82 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/01/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" + +func main() { + ratio := 1.0 / 10.0 + + // after 10 operations + // the inaccuracy is clear + // + // BTW, don't mind about this loop syntax for now + // I'm going to explain it afterwards + for range [...]int{10: 0} { + ratio += 1.0 / 10.0 + } + + fmt.Printf("%.60f", ratio) +} diff --git a/08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/02/main.go b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/02/main.go new file mode 100644 index 0000000..b49b1cf --- /dev/null +++ b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/02/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" + +func main() { + // Go compiler sees these numbers as integers, + // since, there are no fractional parts in + // integer values, + // So, the result becomes 1 instead of 1.5 + + // So, ratio variable here is an int variable, + // it's because, 3 divided by 2 results + // in an integer. + + ratio := 3 / 2 + + fmt.Printf("%d", ratio) +} diff --git a/08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/03/main.go b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/03/main.go new file mode 100644 index 0000000..77faef5 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/01-arithmetic-operators/03-float-inaccuracy/03/main.go @@ -0,0 +1,27 @@ +// 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() { + // When you use a float value with an integer + // in a calculation, + // the result always becomes a float. + + ratio := 3.0 / 2 + + // OR: + // ratio = 3 / 2.0 + + // OR - if 3 is inside an int variable: + // n := 3 + // ratio = float64(n) / 2 + + fmt.Printf("%f", ratio) +} diff --git a/08-numbers-and-strings/01-numbers/02-arithmetic-operators-examples/01/main.go b/08-numbers-and-strings/01-numbers/02-arithmetic-operators-examples/01/main.go new file mode 100644 index 0000000..83f0371 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/02-arithmetic-operators-examples/01/main.go @@ -0,0 +1,30 @@ +// 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() { + fmt.Println("sum :", 3+2) // sum - int + fmt.Println("sum :", 2+3.5) // sum - float64 + fmt.Println("dif :", 3-1) // difference - int + fmt.Println("dif :", 3-0.5) // difference - float64 + fmt.Println("prod:", 4*5) // product - int + fmt.Println("prod:", 5*2.5) // product - float64 + fmt.Println("quot:", 8/2) // quotient - int + fmt.Println("quot:", 8/1.5) // quotient - float64 + + // remainder is only for integers + fmt.Println("rem :", 8%3) + // fmt.Println("rem:", 8.0%3) // error + + // you can do this + // since the fractional part of a float is zero + f := 8.0 + fmt.Println("rem :", int(f)%3) +} diff --git a/08-numbers-and-strings/01-numbers/02-arithmetic-operators-examples/02/main.go b/08-numbers-and-strings/01-numbers/02-arithmetic-operators-examples/02/main.go new file mode 100644 index 0000000..99c2e69 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/02-arithmetic-operators-examples/02/main.go @@ -0,0 +1,33 @@ +// 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() { + // what's the value of the ratio? + // 3 / 2 = 1.5? + var ratio float64 = 3 / 2 + fmt.Println(ratio) + + // explain + // above expression equals to this: + ratio = float64(int(3) / int(2)) + fmt.Println(ratio) + + // how to fix it? + // + // remember, when one of the values is a float value + // the result becomes a float + ratio = float64(3) / 2 + fmt.Println(ratio) + + // or + ratio = 3.0 / 2 + fmt.Println(ratio) +} diff --git a/08-numbers-and-strings/01-numbers/03-precedence/01/main.go b/08-numbers-and-strings/01-numbers/03-precedence/01/main.go new file mode 100644 index 0000000..f7ad257 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/03-precedence/01/main.go @@ -0,0 +1,27 @@ +// 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() { + fmt.Println( + 2+2*4/2, + 2+((2*4)/2), // same as above + ) + + fmt.Println( + 1+4-2, + (1+4)-2, // same as above + ) + + fmt.Println( + (2+2)*4/2, + (2+2)*(4/2), // same as above + ) +} diff --git a/08-numbers-and-strings/01-numbers/03-precedence/02/main.go b/08-numbers-and-strings/01-numbers/03-precedence/02/main.go new file mode 100644 index 0000000..80b42db --- /dev/null +++ b/08-numbers-and-strings/01-numbers/03-precedence/02/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() { + n, m := 1, 5 + + fmt.Println(2 + 1*m/n) + fmt.Println(2 + ((1 * m) / n)) // same as above + + // let's change the precedence using parentheses + fmt.Println(((2 + 1) * m) / n) +} diff --git a/08-numbers-and-strings/01-numbers/03-precedence/03/main.go b/08-numbers-and-strings/01-numbers/03-precedence/03/main.go new file mode 100644 index 0000000..b8a7ba1 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/03-precedence/03/main.go @@ -0,0 +1,30 @@ +// 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 + +func main() { + // // Precedence: Order of expressions + // // Multiplication operators runs first: * and / + + // fmt.Println( + // 1 + 5 - 3*10/2, + // ) + + // // 3 * 10 = 30 + // // 30 / 2 = 15 + // // 1 + 5 = 6 + // // 6 - 15 = -9 + + // // **** TIP **** + // // Use parentheses to change the order of evaluation. + // // First, (1+5-3), then (10/2) will be calculated. + + // fmt.Println( + // (1 + 5 - 3) * (10 / 2), + // ) +} diff --git a/08-numbers-and-strings/01-numbers/03-precedence/04/main.go b/08-numbers-and-strings/01-numbers/03-precedence/04/main.go new file mode 100644 index 0000000..74a4fd2 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/03-precedence/04/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/08-numbers-and-strings/01-numbers/04-incdec-statement/01/main.go b/08-numbers-and-strings/01-numbers/04-incdec-statement/01/main.go new file mode 100644 index 0000000..3e56005 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/04-incdec-statement/01/main.go @@ -0,0 +1,23 @@ +// 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() { + var n int + + // ALTERNATIVES: + // n = n + 1 + // n += 1 + + // BETTER: + n++ + + fmt.Println(n) +} diff --git a/08-numbers-and-strings/01-numbers/04-incdec-statement/02/main.go b/08-numbers-and-strings/01-numbers/04-incdec-statement/02/main.go new file mode 100644 index 0000000..dc4f0e6 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/04-incdec-statement/02/main.go @@ -0,0 +1,23 @@ +// 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() { + n := 10 + + // ALTERNATIVES: + // n = n - 1 + // n -= 1 + + // BETTER: + n-- + + fmt.Println(n) +} diff --git a/08-numbers-and-strings/01-numbers/04-incdec-statement/03/main.go b/08-numbers-and-strings/01-numbers/04-incdec-statement/03/main.go new file mode 100644 index 0000000..1b7df1a --- /dev/null +++ b/08-numbers-and-strings/01-numbers/04-incdec-statement/03/main.go @@ -0,0 +1,30 @@ +// 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" + +// incdec is a statement + +func main() { + var counter int + + // following "statements" are correct: + + counter++ // 1 + counter++ // 2 + counter++ // 3 + counter-- // 2 + fmt.Printf("There are %d line(s) in the file\n", + counter) + + // the following "expressions" are incorrect: + + // counter = 5+counter-- + // counter = ++counter + counter-- +} diff --git a/08-numbers-and-strings/01-numbers/05-assignment-operations/main.go b/08-numbers-and-strings/01-numbers/05-assignment-operations/main.go new file mode 100644 index 0000000..dcbe5e4 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/05-assignment-operations/main.go @@ -0,0 +1,41 @@ +// 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() { + width, height := 5., 12. + + // calculates the area of a rectangle + area := width * height + fmt.Printf("%gx%g=%g\n", width, height, area) + + area = area - 10 // decreases area by 10 + area = area + 10 // increases area by 10 + area = area * 2 // doubles the area + area = area / 2 // divides the area by 2 + fmt.Printf("area=%g\n", area) + + // // ASSIGNMENT OPERATIONS + area -= 10 // decreases area by 10 + area += 10 // increases area by 10 + area *= 2 // doubles the area + area /= 2 // divides the area by 2 + fmt.Printf("area=%g\n", area) + + // finds the remainder of area variable + // since: area is float, this won't work: + // area %= 7 + + // this works + area = float64(int(area) % 7) + fmt.Printf("area=%g\n", area) +} diff --git a/08-numbers-and-strings/01-numbers/06-project-feet-to-meters/main.go b/08-numbers-and-strings/01-numbers/06-project-feet-to-meters/main.go new file mode 100644 index 0000000..045be9c --- /dev/null +++ b/08-numbers-and-strings/01-numbers/06-project-feet-to-meters/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" + "os" + "strconv" +) + +func main() { + arg := os.Args[1] + + // feet is a float64 now + feet, _ := strconv.ParseFloat(arg, 64) + + meters := feet * 0.3048 + + fmt.Printf("%f feet is %f meters.\n", feet, meters) + + // pretty print it: + + // fmt.Printf("%g feet is %g meters.\n", feet, meters) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/01/main.go b/08-numbers-and-strings/01-numbers/exercises/01/main.go new file mode 100644 index 0000000..56b6792 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/01/main.go @@ -0,0 +1,34 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Print the sum of 50 and 25 +// 2. Print the difference of 50 and 15.5 +// 3. Print the product of 50 and 0.5 +// 4. Print the quotient of 50 and 0.5 +// 5. Print the remainder of 25 and 3 +// 6. Print the negation of `5 + 2` +// +// EXPECTED OUTPUT +// 75 +// 34.5 +// 25 +// 100 +// 1 +// -7 +// --------------------------------------------------------- + +func main() { + // ADD YOUR CODE BELOW + // USE `fmt.Println` for each question + + // DO NOT TOUCH THIS + // fmt.Println(x) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/01/solution/main.go b/08-numbers-and-strings/01-numbers/exercises/01/solution/main.go new file mode 100644 index 0000000..b57b006 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/01/solution/main.go @@ -0,0 +1,19 @@ +// 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() { + fmt.Println(50 + 25) + fmt.Println(50 - 15.5) + fmt.Println(50 * 0.5) + fmt.Println(50 / 0.5) + fmt.Println(25 % 3) + fmt.Println(-(5 + 2)) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/02/main.go b/08-numbers-and-strings/01-numbers/exercises/02/main.go new file mode 100644 index 0000000..223bb27 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/02/main.go @@ -0,0 +1,23 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// Fix the program to print 2.5 instead of 2 +// +// EXPECTED OUTPUT +// 2.5 +// --------------------------------------------------------- + +func main() { + x := 5 / 2 + fmt.Println(x) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/02/solution/main.go b/08-numbers-and-strings/01-numbers/exercises/02/solution/main.go new file mode 100644 index 0000000..57bac13 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/02/solution/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() { + // Below solutions are correct: + x := 5. / 2 + // x := 5 / 2. + // x := float64(5) / 2 + // x := 5 / float64(2) + + fmt.Println(x) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/03/main.go b/08-numbers-and-strings/01-numbers/exercises/03/main.go new file mode 100644 index 0000000..aa3099a --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/03/main.go @@ -0,0 +1,38 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// Change the expressions to produce the expected outputs +// +// RESTRICTION +// Use parentheses to change the precedence +// --------------------------------------------------------- + +func main() { + // This expression should print 20 + fmt.Println(10 + 5 - 5 - 10) + + // This expression should print -16 + fmt.Println(-10 + 0.5 - 1 + 5.5) + + // This expression should print -25 + fmt.Println(5 + 10*2 - 5) + + // This expression should print 0.5 + fmt.Println(0.5*2 - 1) + + // This expression should print 24 + fmt.Println(3 + 1/2*10 + 4) + + // This expression should print 15 + fmt.Println(10 / 2 * 10 % 7) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/03/solution/main.go b/08-numbers-and-strings/01-numbers/exercises/03/solution/main.go new file mode 100644 index 0000000..03a4345 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/03/solution/main.go @@ -0,0 +1,30 @@ +// 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() { + // 10 + 5 - 5 - 10 + fmt.Println(10 + 5 - (5 - 10)) + + // -10 + 0.5 - 1 + 5.5 + fmt.Println(-10 + 0.5 - (1 + 5.5)) + + // 5 + 10*2 - 5 + fmt.Println(5 + 10*(2-5)) + + // 0.5*2 - 1 + fmt.Println(0.5 * (2 - 1)) + + // 3 + 1/2*10 + 4 + fmt.Println((3+1)/2*10 + 4) + + // 10 / 2 * 10 % 7 + fmt.Println(10 / 2 * (10 % 7)) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/04/main.go b/08-numbers-and-strings/01-numbers/exercises/04/main.go new file mode 100644 index 0000000..6f937c6 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/04/main.go @@ -0,0 +1,32 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Increase the `counter` 5 times +// 2. Decrease the `factor` 2 times +// 3. Print the product of counter and factor +// +// RESTRICTION +// Use only the incdec statements +// +// EXPECTED OUTPUT +// -75 +// --------------------------------------------------------- + +func main() { + // DO NOT TOUCH THIS + counter, factor := 45, 0.5 + + // TYPE YOUR CODE BELOW + // ... + + // LASTLY: REMOVE THE CODE BELOW + _, _ = counter, factor +} diff --git a/08-numbers-and-strings/01-numbers/exercises/04/solution/main.go b/08-numbers-and-strings/01-numbers/exercises/04/solution/main.go new file mode 100644 index 0000000..c28b6a4 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/04/solution/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" + +func main() { + counter, factor := 45, 0.5 + + counter++ + counter++ + counter++ + counter++ + counter++ + + factor-- + factor-- + + fmt.Println(float64(counter) * factor) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/05/main.go b/08-numbers-and-strings/01-numbers/exercises/05/main.go new file mode 100644 index 0000000..231c8b5 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/05/main.go @@ -0,0 +1,41 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// 1. Write the simplest line of code to increase +// the counter variable by 1. +// +// 2. Write the simplest line of code to decrease +// the counter variable by 1. +// +// 3. Write the simplest line of code to increase +// the counter variable by 5. +// +// 4. Write the simplest line of code to multiply +// the counter variable by 10. +// +// 5. Write the simplest line of code to divide +// the counter variable by 5. +// +// EXPECTED OUTPUT +// 10 +// --------------------------------------------------------- + +func main() { + // DO NOT CHANGE THE CODE BELOW + var counter int + + // TYPE YOUR CODE HERE + + // DO NOT CHANGE THE CODE BELOW + fmt.Println(counter) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/05/solution/main.go b/08-numbers-and-strings/01-numbers/exercises/05/solution/main.go new file mode 100644 index 0000000..b6fa6b9 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/05/solution/main.go @@ -0,0 +1,23 @@ +// 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() { + var counter int + + counter++ + counter-- + + counter += 5 + counter *= 10 + counter /= 5 + + fmt.Println(counter) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/06/main.go b/08-numbers-and-strings/01-numbers/exercises/06/main.go new file mode 100644 index 0000000..4d29663 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/06/main.go @@ -0,0 +1,35 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// Simplify the code (refactor) +// +// RESTRICTION +// Use only the incdec and assignment operations +// +// EXPECTED OUTPUT +// 3 +// --------------------------------------------------------- + +func main() { + width, height := 10, 2 + + width = width + 1 + width = width + height + width = width - 1 + width = width - height + width = width * 20 + width = width / 25 + width = width % 5 + + fmt.Println(width) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/06/solution/main.go b/08-numbers-and-strings/01-numbers/exercises/06/solution/main.go new file mode 100644 index 0000000..f8fa714 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/06/solution/main.go @@ -0,0 +1,24 @@ +// 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() { + width, height := 10, 2 + + width++ + width += height + width-- + width -= height + width *= 20 + width /= 25 + width %= 5 + + fmt.Println(width) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/07-capstone1/main.go b/08-numbers-and-strings/01-numbers/exercises/07-capstone1/main.go new file mode 100644 index 0000000..3e5cb06 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/07-capstone1/main.go @@ -0,0 +1,45 @@ +// 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" +) + +// --------------------------------------------------------- +// EXERCISE +// Calculate the area of a circle from the given radius +// +// CIRCLE AREA FORMULA +// area = πr² +// https://en.wikipedia.org/wiki/Area#Circles +// +// HINT +// For PI you can use `math.Pi` +// +// EXPECTED OUTPUT +// 314.1592653589793 +// +// BONUS EXERCISE! +// 1. Print the area as 314.16 +// 2. To do that you need to use the correct Printf verb :) +// Instead of `%g` verb below. +// --------------------------------------------------------- + +func main() { + var ( + radius = 10. + area float64 + ) + + // ADD YOUR CODE HERE + // ... + + // DO NOT TOUCH THIS + fmt.Printf("radius: %g -> area: %g\n", radius, area) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/07-capstone1/solution/main.go b/08-numbers-and-strings/01-numbers/exercises/07-capstone1/solution/main.go new file mode 100644 index 0000000..6f5658e --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/07-capstone1/solution/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/08-numbers-and-strings/01-numbers/exercises/08-capstone2/main.go b/08-numbers-and-strings/01-numbers/exercises/08-capstone2/main.go new file mode 100644 index 0000000..32ebb5a --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/08-capstone2/main.go @@ -0,0 +1,41 @@ +// 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" +) + +// --------------------------------------------------------- +// EXERCISE +// 1. Get the radius from the command-line +// 2. Convert it to a float64 +// 3. Calculate the surface area of a sphere +// +// SPHERE SURFACE AREA FORMULA +// area = 4πr² +// https://en.wikipedia.org/wiki/Sphere#Surface_area +// +// RESTRICTION +// Use `math.Pow` to calculate the area +// Read its documentation to see how it works. +// https://golang.org/pkg/math/#Pow +// +// EXPECTED OUTPUT +// 1256.64 +// --------------------------------------------------------- + +func main() { + var radius, area float64 + + // ADD YOUR CODE HERE + // ... + + // DO NOT TOUCH THIS + fmt.Printf("radius: %g -> area: %.2f\n", radius, area) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/08-capstone2/solution/main.go b/08-numbers-and-strings/01-numbers/exercises/08-capstone2/solution/main.go new file mode 100644 index 0000000..fd96875 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/08-capstone2/solution/main.go @@ -0,0 +1,26 @@ +// 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, area float64 + + radius, _ = strconv.ParseFloat(os.Args[1], 64) + + area = 4 * math.Pi * math.Pow(radius, 2) + + fmt.Printf("radius: %g -> area: %.2f\n", + radius, area) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/09-capstone3/main.go b/08-numbers-and-strings/01-numbers/exercises/09-capstone3/main.go new file mode 100644 index 0000000..062409e --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/09-capstone3/main.go @@ -0,0 +1,38 @@ +// 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" +) + +// --------------------------------------------------------- +// EXERCISE +// 1. Get the radius from the command-line +// 2. Convert it to a float64 +// 3. Calculate the volume of a sphere +// +// SPHERE VOLUME FORMULA +// https://en.wikipedia.org/wiki/Sphere#Enclosed_volume +// +// RESTRICTION +// Use `math.Pow` to calculate the volume +// +// EXPECTED OUTPUT +// 4188.79 +// --------------------------------------------------------- + +func main() { + var radius, vol float64 + + // ADD YOUR CODE HERE + // ... + + // DO NOT TOUCH THIS + fmt.Printf("radius: %g -> volume: %.2f\n", radius, vol) +} diff --git a/08-numbers-and-strings/01-numbers/exercises/09-capstone3/solution/main.go b/08-numbers-and-strings/01-numbers/exercises/09-capstone3/solution/main.go new file mode 100644 index 0000000..ca27075 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/exercises/09-capstone3/solution/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) +} diff --git a/08-numbers-and-strings/01-numbers/questions/01-arithmetic-operators.md b/08-numbers-and-strings/01-numbers/questions/01-arithmetic-operators.md new file mode 100644 index 0000000..0db3511 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/questions/01-arithmetic-operators.md @@ -0,0 +1,95 @@ +## Which group of operators below is arithmetic operators? +1. **, /, ^, !, ++, -- +2. *, /, %, +, - *CORRECT* +3. &, |, +, -, / + + +## Which value below you can use with a remainder operator? +1. 3.54 +2. true +3. 57 *CORRECT* +4. "Try Me!" + +> 4. Nice Try. But, that's not right. Sorry. +> 3. That's right. The remainder operator only works on integer values. + + +## What's the result of this expression? +```go +8 % 3 +``` +1. 4 +2. 2 +3. 0 +4. 1 *CORRECT* + + +## What's the result of this expression? +```go +-(3 * -2) +``` +1. -6 +2. -1 +3. 0 +4. 6 *CORRECT* + + +## What's the result of this expression? +```go +var degree float64 = 10 / 4 +``` +1. 2.5 +2. 2.49 +3. 2 *CORRECT* +4. 0 + +> 3. That's right. An integer value cannot contain fractional parts. + + +## What's the result of this expression? +```go +var degree float64 = 3. / 2 +``` +1. 1.5 *CORRECT* +2. 1.49 +3. 1 +4. 0 + +> 1. That's right. `3.` makes the whole expression a float value. + + +## What's the type of the `x` variable? +```go +x := 5 * 2. +``` +1. int +2. float64 *CORRECT* +3. bool +4. string + +> 1. Look closely to 2 there. +> 2. Why? Because, `2.` there makes the expressions a float value. Cool. +> 3. Oh, come on! Life is not always true and false. +> 4. I can't see any double-quotes or back-quotes, can you? + + +## What's the type of the `x` variable? +```go +x := 5 * -(2) +``` +1. int *CORRECT* +2. float64 +3. bool +4. string + +> 1. Why? Because, there only integer numbers. +> 2. I can't see any fractional parts there, can you? +> 3. Oh, come on! Life is not always true and false. +> 4. I can't see any double-quotes or back-quotes, can you? + + +## Which kind of values can result in inaccurate calculations? +1. integers +2. floats *CORRECT* +3. bools +4. strings diff --git a/08-numbers-and-strings/01-numbers/questions/02-precedence.md b/08-numbers-and-strings/01-numbers/questions/02-precedence.md new file mode 100644 index 0000000..e3e4897 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/questions/02-precedence.md @@ -0,0 +1,44 @@ +## What's the result of the expression? +```go +5 - 2 * 5 + 7 +``` +1. 2 *CORRECT* +2. 22 +3. -19 +4. 36 +5. -12 + + +## What's the result of the expression? +```go +5 - (2 * 5) + 7 +``` +1. 2 +2. 22 *CORRECT* +3. -19 +4. 36 +5. -12 + + +## What's the result of the expression? +```go +5 - 2 * (5 + 7) +``` +1. 2 +2. 22 +3. -19 *CORRECT* +4. 36 +5. -12 + + +## What's the result of the expression? +```go +5. -(2 * 5 + 7) +``` +1. 2 +2. 22 +3. -19 +4. -12 +5. -12.0 *CORRECT* + +> 4. You're close but remember! The result of an expression with floats and integers is always a float. \ No newline at end of file diff --git a/08-numbers-and-strings/01-numbers/questions/03-assignment-operations.md b/08-numbers-and-strings/01-numbers/questions/03-assignment-operations.md new file mode 100644 index 0000000..775d9e3 --- /dev/null +++ b/08-numbers-and-strings/01-numbers/questions/03-assignment-operations.md @@ -0,0 +1,98 @@ +## Which expression increases `n` by 1? +```go +var n float64 +``` +1. `n = +1` +2. `n = n++` +3. `n = n + 1` *CORRECT* +4. `++n` + +> 1. This just assigns 1 to n. +> 2. IncDec statement can't be used as an operator. +> 4. Go doesn't support prefix incdec notation. + + +## Which expression decreases `n` by 1? +```go +var n int +``` +1. `n = -1` +2. `n = n--` +3. `n = n - 1` *CORRECT* +4. `--n` + +> 1. This just assigns -1 to n. +> 2. IncDec statement can't be used as an operator. +> 4. Go doesn't support prefix incdec notation. + + +## Which code below equals to `n = n + 1`? +1. `n++` *CORRECT* +2. `n = n++` +3. `++n` +4. `n = n ++ 1` + +> 2. IncDec statement can't be used as an operator. +> 3. Go doesn't support prefix incdec notation. +> 4. What's that? ++? + + +## Which code below equals to `n = n + 1`? +1. `n = n++` +2. `n += 1` *CORRECT* +3. `++n` +4. `n = n ++ 1` + +> 1. IncDec statement can't be used as an operator. +> 3. Go doesn't support prefix incdec notation. +> 4. What's that? ++? + + +## Which code below equals to `n -= 1`? +1. `n = n--` +2. `n += 1--` +3. `n--` *CORRECT* +4. `--n` + +> 1. IncDec statement can't be used as an operator. +> 2. IncDec statement can't be used as an operator. And also, you can't use it with `1--`. The value should be addressable. You're going to learn what that means soon. +> 4. Go doesn't support prefix incdec notation. + + +## Which code below divides the `length` by 10? +1. `length = length // 10` +2. `length /= 10` *CORRECT* +3. `length //= 10` + +> 1. What's that? `//`? +> 2. That's right. This equals to: `length = length / 10` +> 3. What's that? `//=`? + + +## Which code below equals to `x = x % 2`? +1. `x = x / 2` +2. `x =% x` +3. `x %= x` *CORRECT* + +> 1. This is a division. You need to use the remainder operator. +> 2. Close... But, the `%` operator is on the wrong side of the assignment. + + +## Which function below converts a string value into a float value? +1. `fmtconv.ToFloat` +2. `conv.ParseFloat` +3. `strconv.ParseFloat` *CORRECT* +4. `strconv.ToFloat` + + +## Which code is correct? +If you don't remember it, this its function signature: +```go +func ParseFloat(s string, bitSize int) (float64, error) +``` +1. `strconv.ParseFloat("10", 128)` +2. `strconv.ParseFloat("10", 64)` *CORRECT* +3. `strconv.ParseFloat("10", "64")` +4. `strconv.ParseFloat(10, 64)` + +> 1. There are no 128-bit floating point values in Go (Actually there are, but they only belong to the compile-time). \ No newline at end of file diff --git a/08-numbers-and-strings/02-strings/01-raw-string-literal/main.go b/08-numbers-and-strings/02-strings/01-raw-string-literal/main.go new file mode 100644 index 0000000..94446b8 --- /dev/null +++ b/08-numbers-and-strings/02-strings/01-raw-string-literal/main.go @@ -0,0 +1,37 @@ +// 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() { + // The type of a string and a raw string literal + // is the same. They both are strings. + // + // So, they both can be used as a string value. + var s string + s = "how are you?" + s = `how are you?` + fmt.Println(s) + + // string literal + s = "\n\t\"Hello\"\n" + fmt.Println(s) + + // raw string literal + s = ` + + "Hello" +` + + fmt.Println(s) + + // windows path + fmt.Println("c:\\my\\dir\\file") // string literal + fmt.Println(`c:\my\dir\file`) // raw string literal +} diff --git a/08-numbers-and-strings/02-strings/02-concatenation/01/main.go b/08-numbers-and-strings/02-strings/02-concatenation/01/main.go new file mode 100644 index 0000000..963b20b --- /dev/null +++ b/08-numbers-and-strings/02-strings/02-concatenation/01/main.go @@ -0,0 +1,16 @@ +// 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() { + name, last := "carl", "sagan" + + fmt.Println(name + " " + last) +} diff --git a/08-numbers-and-strings/02-strings/02-concatenation/02-assignment-operation/main.go b/08-numbers-and-strings/02-strings/02-concatenation/02-assignment-operation/main.go new file mode 100644 index 0000000..28bf2c0 --- /dev/null +++ b/08-numbers-and-strings/02-strings/02-concatenation/02-assignment-operation/main.go @@ -0,0 +1,22 @@ +// 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() { + name, last := "carl", "sagan" + + // assignment operation using string concat + name += " edward" + + // equals to this: + // name = name + " edward" + + fmt.Println(name + " " + last) +} diff --git a/08-numbers-and-strings/02-strings/02-concatenation/03-concat-non-strings/main.go b/08-numbers-and-strings/02-strings/02-concatenation/03-concat-non-strings/main.go new file mode 100644 index 0000000..5d2fbb1 --- /dev/null +++ b/08-numbers-and-strings/02-strings/02-concatenation/03-concat-non-strings/main.go @@ -0,0 +1,51 @@ +// 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() { + 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)) + + // + + // invalid op + // eq = true + " " + false + + eq = strconv.FormatBool(true) + + " " + + strconv.FormatBool(false) + + fmt.Println(eq) +} diff --git a/08-numbers-and-strings/02-strings/03-string-length/01-len/main.go b/08-numbers-and-strings/02-strings/03-string-length/01-len/main.go new file mode 100644 index 0000000..1246224 --- /dev/null +++ b/08-numbers-and-strings/02-strings/03-string-length/01-len/main.go @@ -0,0 +1,18 @@ +// 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() { + name := "carl" + + // strings are made up of bytes + // len function counts the bytes in a string value + fmt.Println(len(name)) +} diff --git a/08-numbers-and-strings/02-strings/03-string-length/02-unicode-len/main.go b/08-numbers-and-strings/02-strings/03-string-length/02-unicode-len/main.go new file mode 100644 index 0000000..2d062f5 --- /dev/null +++ b/08-numbers-and-strings/02-strings/03-string-length/02-unicode-len/main.go @@ -0,0 +1,38 @@ +// 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" + "unicode/utf8" +) + +func main() { + // strings are made up of bytes + + // len function counts the bytes in a string value. + // + // This string literal contains unicode characters. + // + // And, unicode characters can be 1-4 bytes. + // So, "İnanç" is 7 bytes long, not 5. + // + // İ = 2 bytes + // n = 1 byte + // a = 1 byte + // n = 1 byte + // ç = 2 bytes + // TOTAL = 7 bytes + name := "İnanç" + fmt.Printf("%q is %d bytes\n", name, len(name)) + + // To get the actual characters (or runes) inside + // a utf-8 encoded string value, you should do this: + fmt.Printf("%q is %d characters\n", + name, utf8.RuneCountInString(name)) +} diff --git a/08-numbers-and-strings/02-strings/04-project-banger/exercise-solution/main.go b/08-numbers-and-strings/02-strings/04-project-banger/exercise-solution/main.go new file mode 100644 index 0000000..8cc2964 --- /dev/null +++ b/08-numbers-and-strings/02-strings/04-project-banger/exercise-solution/main.go @@ -0,0 +1,33 @@ +// 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 ( + "os" + "strings" +) + +// NOTE: You should always pass it at least one argument + +func main() { + msg := os.Args[1] + + // it's important to calculate things only once + // so, do not call the repeat function twice + // calling it once is enough + marks := strings.Repeat("!", len(msg)) + s := marks + msg + marks + s = strings.ToUpper(s) + + // you can also type this program more concisely + // like this: + // + // msg := strings.ToUpper(os.Args[1]) + // marks := strings.Repeat("!", len(msg)) + // fmt.Println(marks + msg + marks) +} diff --git a/08-numbers-and-strings/02-strings/04-project-banger/main.go b/08-numbers-and-strings/02-strings/04-project-banger/main.go new file mode 100644 index 0000000..2ba0a4c --- /dev/null +++ b/08-numbers-and-strings/02-strings/04-project-banger/main.go @@ -0,0 +1,26 @@ +// 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" + "os" + "strings" +) + +// NOTE: You should always pass it at least one argument + +func main() { + msg := os.Args[1] + l := len(msg) + + s := msg + strings.Repeat("!", l) + s = strings.ToUpper(s) + + fmt.Println(s) +} diff --git a/08-numbers-and-strings/02-strings/exercises/01/main.go b/08-numbers-and-strings/02-strings/exercises/01/main.go new file mode 100644 index 0000000..e87b2d9 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/01/main.go @@ -0,0 +1,34 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// 1. Change the following program +// 2. It should use a raw string literal instead +// +// HINT +// Run this program first to see its output. +// Then you can easily understand what it does. +// +// EXPECTED OUTPUT +// Your solution should output the same as this program. +// Only that it should use a raw string literal instead. +// --------------------------------------------------------- + +func main() { + // HINTS: + // \\ equals to backslash character + // \n equals to newline character + + path := "c:\\program files\\duper super\\fun.txt\n" + + "c:\\program files\\really\\funny.png" + fmt.Println(path) +} diff --git a/08-numbers-and-strings/02-strings/exercises/01/solution/main.go b/08-numbers-and-strings/02-strings/exercises/01/solution/main.go new file mode 100644 index 0000000..918e9b7 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/01/solution/main.go @@ -0,0 +1,22 @@ +// 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() { + // this one uses a raw string literal + + // can you see how readable it is? + // compared to the previous one? + + path := `c:\program files\duper super\fun.txt +c:\program files\really\funny.png` + + fmt.Println(path) +} diff --git a/08-numbers-and-strings/02-strings/exercises/02/main.go b/08-numbers-and-strings/02-strings/exercises/02/main.go new file mode 100644 index 0000000..df19c51 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/02/main.go @@ -0,0 +1,42 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// 1. Change the following program +// 2. It should use a raw string literal instead +// +// HINT +// Run this program first to see its output. +// Then you can easily understand what it does. +// +// EXPECTED OUTPUT +// Your solution should output the same as this program. +// Only that it should use a raw string literal instead. +// --------------------------------------------------------- + +func main() { + // HINTS: + // \t equals to TAB character + // \n equals to newline character + // \" equals to double-quotes character + + json := "\n" + + "{\n" + + "\t\"Items\": [{\n" + + "\t\t\"Item\": {\n" + + "\t\t\t\"name\": \"Teddy Bear\"\n" + + "\t\t}\n" + + "\t}]\n" + + "}\n" + + fmt.Println(json) +} diff --git a/08-numbers-and-strings/02-strings/exercises/02/solution/main.go b/08-numbers-and-strings/02-strings/exercises/02/solution/main.go new file mode 100644 index 0000000..1a14d75 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/02/solution/main.go @@ -0,0 +1,28 @@ +// 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() { + // this one uses a raw string literal + + // can you see how readable it is? + // compared to the previous one? + + json := ` +{ + "Items": [{ + "Item": { + "name": "Teddy Bear" + } + }] +}` + + fmt.Println(json) +} diff --git a/08-numbers-and-strings/02-strings/exercises/03/main.go b/08-numbers-and-strings/02-strings/exercises/03/main.go new file mode 100644 index 0000000..ac0760b --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/03/main.go @@ -0,0 +1,44 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// 1. Initialize the name variable +// by getting input from the command line +// +// 2. Use concatenation operator to concatenate it +// with the raw string literal below +// +// NOTE +// You should concatenate the name variable in the correct +// place. +// +// EXPECTED OUTPUT +// Let's say that you run the program like this: +// go run main.go inanç +// +// Then it should output this: +// hi inanç! +// how are you? +// --------------------------------------------------------- + +func main() { + // uncomment the code below + // name := "and get the name from the command-line" + + // replace and concatenate the `name` variable + // after `hi ` below + + msg := `hi CONCATENATE-NAME-VARIABLE-HERE! +how are you?` + + fmt.Println(msg) +} diff --git a/08-numbers-and-strings/02-strings/exercises/03/solution/main.go b/08-numbers-and-strings/02-strings/exercises/03/solution/main.go new file mode 100644 index 0000000..31fd362 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/03/solution/main.go @@ -0,0 +1,22 @@ +// 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" + "os" +) + +func main() { + name := os.Args[1] + + msg := `hi ` + name + `! +how are you?` + + fmt.Println(msg) +} diff --git a/08-numbers-and-strings/02-strings/exercises/04/main.go b/08-numbers-and-strings/02-strings/exercises/04/main.go new file mode 100644 index 0000000..0c9a27e --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/04/main.go @@ -0,0 +1,36 @@ +// 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" + "os" +) + +// --------------------------------------------------------- +// EXERCISE +// 1. Change the following program to work with unicode +// characters. +// +// INPUT +// "İNANÇ" +// +// EXPECTED OUTPUT +// 5 +// --------------------------------------------------------- + +func main() { + // Currently it returns 7 + // Because, it counts the bytes... + // It should count the runes (codepoints) instead. + // + // When you run it with "İNANÇ", it should return 5 not 7. + + length := len(os.Args[1]) + fmt.Println(length) +} diff --git a/08-numbers-and-strings/02-strings/exercises/04/solution/main.go b/08-numbers-and-strings/02-strings/exercises/04/solution/main.go new file mode 100644 index 0000000..17ed646 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/04/solution/main.go @@ -0,0 +1,19 @@ +// 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" + "os" + "unicode/utf8" +) + +func main() { + length := utf8.RuneCountInString(os.Args[1]) + fmt.Println(length) +} diff --git a/08-numbers-and-strings/02-strings/exercises/05/main.go b/08-numbers-and-strings/02-strings/exercises/05/main.go new file mode 100644 index 0000000..15f55d1 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/05/main.go @@ -0,0 +1,34 @@ +// 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" + "os" + "strings" +) + +// --------------------------------------------------------- +// EXERCISE +// Change the Banger program the work with Unicode +// characters. +// +// INPUT +// "İNANÇ" +// +// EXPECTED OUTPUT +// İNANÇ!!!!! +// --------------------------------------------------------- + +func main() { + msg := os.Args[1] + + s := msg + strings.Repeat("!", len(msg)) + + fmt.Println(s) +} diff --git a/08-numbers-and-strings/02-strings/exercises/05/solution/main.go b/08-numbers-and-strings/02-strings/exercises/05/solution/main.go new file mode 100644 index 0000000..804d5e0 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/05/solution/main.go @@ -0,0 +1,24 @@ +// 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" + "os" + "strings" + "unicode/utf8" +) + +func main() { + msg := os.Args[1] + l := utf8.RuneCountInString(msg) + + s := msg + strings.Repeat("!", l) + + fmt.Println(s) +} diff --git a/08-numbers-and-strings/02-strings/exercises/06/main.go b/08-numbers-and-strings/02-strings/exercises/06/main.go new file mode 100644 index 0000000..5abcf68 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/06/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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Look at the documentation of strings package +// 2. Find a function that changes the letters into lowercase +// 3. Get a value from the command-line +// 4. Print the given value in lowercase letters +// +// INPUT +// "SHEPARD" +// +// EXPECTED OUTPUT +// shepard +// --------------------------------------------------------- + +func main() { +} diff --git a/08-numbers-and-strings/02-strings/exercises/06/solution/main.go b/08-numbers-and-strings/02-strings/exercises/06/solution/main.go new file mode 100644 index 0000000..cd86f15 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/06/solution/main.go @@ -0,0 +1,18 @@ +// 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" + "os" + "strings" +) + +func main() { + fmt.Println(strings.ToLower(os.Args[1])) +} diff --git a/08-numbers-and-strings/02-strings/exercises/07/main.go b/08-numbers-and-strings/02-strings/exercises/07/main.go new file mode 100644 index 0000000..28185c6 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/07/main.go @@ -0,0 +1,37 @@ +// 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" +) + +// --------------------------------------------------------- +// EXERCISE +// 1. Look at the documentation of strings package +// 2. Find a function that trims the spaces from +// the given string +// 3. Trim the text variable and print it +// +// EXPECTED OUTPUT +// The weather looks good. +// I should go and play. +// --------------------------------------------------------- + +func main() { + msg := ` + + The weather looks good. +I should go and play. + + + + ` + + fmt.Println(msg) +} diff --git a/08-numbers-and-strings/02-strings/exercises/07/solution/main.go b/08-numbers-and-strings/02-strings/exercises/07/solution/main.go new file mode 100644 index 0000000..1894457 --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/07/solution/main.go @@ -0,0 +1,26 @@ +// 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" + "strings" +) + +func main() { + msg := ` + + The weather looks good. +I should go and play. + + + + ` + + fmt.Println(strings.TrimSpace(msg)) +} diff --git a/08-numbers-and-strings/02-strings/exercises/08/main.go b/08-numbers-and-strings/02-strings/exercises/08/main.go new file mode 100644 index 0000000..d76c42c --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/08/main.go @@ -0,0 +1,35 @@ +// 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" +) + +// --------------------------------------------------------- +// EXERCISE +// 1. Look at the documentation of strings package +// 2. Find a function that trims the spaces from +// only the right-most part of the given string +// 3. Trim it from the right part only +// 4. Print the number of characters it contains. +// +// RESTRICTION +// Your program should work with unicode string values. +// +// EXPECTED OUTPUT +// 5 +// --------------------------------------------------------- + +func main() { + // currently it prints 17 + // it should print 5 + + name := "inanç " + fmt.Println(len(name)) +} diff --git a/08-numbers-and-strings/02-strings/exercises/08/solution/main.go b/08-numbers-and-strings/02-strings/exercises/08/solution/main.go new file mode 100644 index 0000000..5e9e0ab --- /dev/null +++ b/08-numbers-and-strings/02-strings/exercises/08/solution/main.go @@ -0,0 +1,23 @@ +// 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" + "strings" + "unicode/utf8" +) + +func main() { + name := "inanç " + + name = strings.TrimRight(name, " ") + l := utf8.RuneCountInString(name) + + fmt.Println(l) +} diff --git a/08-numbers-and-strings/02-strings/questions/questions.md b/08-numbers-and-strings/02-strings/questions/questions.md new file mode 100644 index 0000000..39e0604 --- /dev/null +++ b/08-numbers-and-strings/02-strings/questions/questions.md @@ -0,0 +1,194 @@ +## What's the result of this expression? +```go +"\"Hello\\"" + ` \"World\"` +``` + +1. "Hello" "World" +2. "Hello" \"World\" *CORRECT* +3. "Hello" `"World"` +4. "\"Hello\" `\"World\"`" + +> 1. Go doesn't interpret the escape sequences in raw string literals. +> 2. That's right. Go interprets `\"` as `"` but it doesn't do so for ` \"World\"`. + + +## What's the best way to represent the following text in the code? +```xml + + + "Teddy Bear" + + +``` + +1. *CORRECT* +```go +` + + "Teddy Bear" + +` +``` + +2. +```go +" + + "Teddy Bear" + +" +``` + +3. +```go +" + + \"Teddy Bear\" + +" +``` + +4. +```go +` + + \"Teddy Bear\" + +` +``` + +> 2-3. You can't write a string literal like that. It can't be multiple-lines. +> 4. You don't need to use escape sequences inside raw string literals. + + +## What's the result of the following expression? +```go +len("lovely") +``` + +1. 7 +2. 8 +3. 6 *CORRECT* +4. 0 + +> 2. Remember! "a" is 1 char. `a` is also 1 char. + + +## What's the result of the following expression? +```go +len("very") + len(`\"cool\"`) +``` + +1. 8 +2. 12 *CORRECT* +3. 16 +4. 10 + +> 1. There are also double-quotes, count them as well. +> 2. That's right. Go doesn't interpreted \" in raw string literals. +> 3. Remember! "very" is 4 characters. `very` is also 4 characters. +> 4. Remember! Go doesn't interpreted \" in raw string literals. + + +## What's the result of the following expression? +```go +len("very") + len("\"cool\"") +``` + +1. 8 +2. 12 +3. 16 +4. 10 *CORRECT* + +> 1. There are also double-quotes, count them as well. +> 2. Remember! Go interprets escape sequences in string literals. +> 4. That's right. Go does interpret \" in a string literal. So, "\"" means ", which is 1 character. + + +## What's the result of the following expression? +```go +len("péripatéticien") +``` + +**HINT:** é is 2 bytes long. And, the len function counts the bytes not the letters. + +**USELESS INFORMATION:** "péripatéticien" means "wanderer". + +1. 14 +2. 16 *CORRECT* +3. 18 +4. 20 + +> 1. Remember! é is 2 bytes long. +> 2. An english letter is 1 byte long. However, é is 2 bytes long. So, that makes up 16 bytes. Cool. +> 3. You didn't count the double-quotes, did you? + + +## How can you find the correct length of the characters in this string literal? +```go +"péripatéticien" +``` + +1. `len(péripatéticien)` +2. `len("péripatéticien")` +3. `utf8.RuneCountInString("péripatéticien")` *CORRECT* +4. `unicode/utf8.RuneCountInString("péripatéticien")` + +> 1. Where are the double-quotes? +> 2. This only finds the bytes in a string value. +> 4. You're close. But, the package's name is utf8 not unicode/utf8. + + +## What's the result of the following expression? +```go +utf8.RuneCountInString("péripatéticien") +``` + +1. 16 +2. 14 *CORRECT* +3. 18 +4. 20 + +> 1. This is its byte count. `RuneCountInString` counts the runes (codepoints) not the bytes. +> 2. That's right. `RuneCountInString` returns the number of runes (codepoints) in a string value. + + +## Which package contains string manipulation functions? +1. string +2. unicode/utf8 +3. strings *CORRECT* +4. unicode/strings + + +## What's the result of this expression? +```go +strings.Repeat("*x", 3) + "*" +``` + +**HINT:** Repeat function repeats the given string. + +1. `*x*x*x` +2. `x*x*x` +3. `*x3` +4. `*x*x*x*` *CORRECT* + +> 1. You're close but you missed the concatenation at the end. +> 2. Look closely. +> 3. Wow! You should really watch the lectures again. Sorry. +> 4. That's right. Repeat function repeats the given string. And, the concatenation operator combines the strings. + + +## What's the result of this expression? +```go +strings.ToUpper("bye bye ") + "see you!" +``` + +1. `bye bye see you!` +2. `BYE BYE SEE YOU!` +3. `bye bye + see you!` +4. `BYE BYE see you!` *CORRECT* + +> 1. You missed the ToUpper? +> 2. You're close but look closely. ToUpper only changes the first part of the string there. +> 3. Not even close. Sorry. +> 4. Perfect! Good catch! ToUpper only changes the first part of the string there. \ No newline at end of file diff --git a/09-go-type-system/01-bits/main.go b/09-go-type-system/01-bits/main.go new file mode 100644 index 0000000..1ce938d --- /dev/null +++ b/09-go-type-system/01-bits/main.go @@ -0,0 +1,46 @@ +// 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() { + // %b verb prints bits + + // true false, on off, ... + // 1 bit can encode 2 different state: 0 or 1 + fmt.Printf("%b\n", 0) + fmt.Printf("%b\n", 1) + + // 2 bits can encode 4 different states + // 0 0 + // 0 1 + // 1 0 + // 1 1 + + // %02b prints 2 bits with leading zeros if any + + fmt.Printf("%02b = %d\n", 0, 0) + fmt.Printf("%02b = %d\n", 1, 1) + fmt.Printf("%02b = %d\n", 2, 2) + fmt.Printf("%02b = %d\n", 3, 3) + + // run this and analyze: + // how 1 moves from right to the left + + // %08b prints 8 bits with leading zeros if any + + fmt.Printf("%08b = %d\n", 1, 1) + fmt.Printf("%08b = %d\n", 2, 2) + fmt.Printf("%08b = %d\n", 4, 4) + fmt.Printf("%08b = %d\n", 8, 8) + fmt.Printf("%08b = %d\n", 16, 16) + fmt.Printf("%08b = %d\n", 32, 32) + fmt.Printf("%08b = %d\n", 64, 64) + fmt.Printf("%08b = %d\n", 128, 128) +} diff --git a/09-go-type-system/02-bytes/main.go b/09-go-type-system/02-bytes/main.go new file mode 100644 index 0000000..df996fe --- /dev/null +++ b/09-go-type-system/02-bytes/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" + +func main() { + // byte is an integer number with 8 bits (1 byte) + var b byte + + // all bits are empty or 0 + // this is the minimum number a byte can represent + b = 0 + fmt.Printf("%08b = %d\n", b, b) + + // all bits are full or 1 + // this is the maximum number a byte can represent + b = 255 + fmt.Printf("%08b = %d\n", b, b) +} diff --git a/09-go-type-system/03-predeclared-types/main.go b/09-go-type-system/03-predeclared-types/main.go new file mode 100644 index 0000000..9d7ab73 --- /dev/null +++ b/09-go-type-system/03-predeclared-types/main.go @@ -0,0 +1,59 @@ +// 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" + "unsafe" +) + +func main() { + // you can find the limits of numerics types + // in the math package + fmt.Println("int8 :", math.MinInt8, math.MaxInt8) + fmt.Println("int16 :", math.MinInt16, math.MaxInt16) + fmt.Println("int32 :", math.MinInt32, math.MaxInt32) + fmt.Println("int64 :", math.MinInt64, math.MaxInt64) + + // unsigned type can only represent positive integers + fmt.Println("uint8 :", 0, math.MaxUint8) + fmt.Println("uint16 :", 0, math.MaxUint16) + fmt.Println("uint32 :", 0, math.MaxUint32) + fmt.Println("uint64 :", 0, uint64(math.MaxUint64)) + // You can't print MaxUint64 directly + // Its size is way bigger for the runtime + // It can only be used in constant expressions + + // e means zeros after the number (scientific notation) + // 1e1, is 10 1e2, is 100, 12e1 is 120 + fmt.Println("float32:", math.SmallestNonzeroFloat32, + math.MaxFloat32) + fmt.Println("float64:", math.SmallestNonzeroFloat64, + math.MaxFloat64) + + // memory costs + fmt.Println("int :", unsafe.Sizeof(int(1)), "bytes") + fmt.Println("int8 :", unsafe.Sizeof(int8(1)), "bytes") + fmt.Println("int16 :", unsafe.Sizeof(int16(1)), "bytes") + fmt.Println("int32 :", unsafe.Sizeof(int32(1)), "bytes") + fmt.Println("int64 :", unsafe.Sizeof(int64(1)), "bytes") + + fmt.Println("uint :", unsafe.Sizeof(uint(1)), "bytes") + fmt.Println("uint8 :", unsafe.Sizeof(uint8(1)), "bytes") + fmt.Println("uint16 :", unsafe.Sizeof(uint16(1)), "bytes") + fmt.Println("uint32 :", unsafe.Sizeof(uint32(1)), "bytes") + fmt.Println("uint64 :", unsafe.Sizeof(uint64(1)), "bytes") + + fmt.Println("float32:", unsafe.Sizeof(float32(1)), "bytes") + fmt.Println("float64:", unsafe.Sizeof(float64(1)), "bytes") + + fmt.Println("hello :", len("hello")+8, "bytes") + fmt.Println("hi :", len("hi")+8, "bytes") + fmt.Println("hi :", len("hi")+8, "bytes") +} diff --git a/09-go-type-system/04-overflow/01-problem/main.go b/09-go-type-system/04-overflow/01-problem/main.go new file mode 100644 index 0000000..e4bca7a --- /dev/null +++ b/09-go-type-system/04-overflow/01-problem/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" + +func main() { + var ( + width uint8 = 255 + height = 255 // int + ) + + width++ + + if int(width) < height { + fmt.Println("height is greater") + } + + fmt.Printf("width: %d height: %d\n", width, height) +} diff --git a/09-go-type-system/04-overflow/02-explain/main.go b/09-go-type-system/04-overflow/02-explain/main.go new file mode 100644 index 0000000..0489cb9 --- /dev/null +++ b/09-go-type-system/04-overflow/02-explain/main.go @@ -0,0 +1,45 @@ +// 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() { + // go catches overflow at compile-time + // + // fmt.Println(int8(math.MaxInt8 + 1)) // overflows + + // but it cannot catch them in runtime + var n int8 = math.MaxInt8 + + // wrap arounds to its negative maximum + fmt.Println("max int8 :", n) // 127 + fmt.Println("max int8 + 1 :", n+1) // -128 + + // wrap arounds to its positive maximum + n = math.MinInt8 + fmt.Println("min int8 :", n) // -128 + fmt.Println("min int8 - 1 :", n-1) // 127 + + // wrap arounds to its maximum + var un uint8 + + fmt.Println("max uint8 :", un) // 0 + fmt.Println("max uint8 - 1:", un-1) // 255 + + // wrap around to its minimum + un = math.MaxUint8 + fmt.Println("max uint8 + 1:", un+1) // 255 + + // floats goes to infinity when overflowed + f := float32(math.MaxFloat32) + fmt.Println("max float :", f*1.2) +} diff --git a/09-go-type-system/04-overflow/03-destructive/main.go b/09-go-type-system/04-overflow/03-destructive/main.go new file mode 100644 index 0000000..c17d4e4 --- /dev/null +++ b/09-go-type-system/04-overflow/03-destructive/main.go @@ -0,0 +1,27 @@ +// 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() { + // uint16 max value is 65535 + big := uint16(65535) + + // uint8 destroys its value + // to its own max value which is 255 + // + // 65535 - 255 is lost. + small := uint8(big) + + // fmt.Printf("%b %d\n", big, big) + // fmt.Printf("%b %[1]d\n", big) + + fmt.Printf("%016b %[1]d\n", big) + fmt.Printf("%016b %[1]d\n", small) +} diff --git a/09-go-type-system/05-defined-types/01-duration-example/main.go b/09-go-type-system/05-defined-types/01-duration-example/main.go new file mode 100644 index 0000000..e5a4a34 --- /dev/null +++ b/09-go-type-system/05-defined-types/01-duration-example/main.go @@ -0,0 +1,35 @@ +// 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" + "time" +) + +func main() { + h, _ := time.ParseDuration("4h30m") + + // why would you want to create a new type? + + // 1- adding new methods to the type + fmt.Println(h.Hours(), "hours") + + // 2- make it a distinct type for type-safety + // you can't use the defined type + // with its underlying type directly. + // + // you need to convert one of them. + var m int64 = 2 + h *= time.Duration(m) + fmt.Println(h) + + // type of `h` and its underlying type are different + fmt.Printf("Type of h: %T\n", h) + fmt.Printf("Type of h's underlying type: %T\n", int64(h)) +} diff --git a/09-go-type-system/05-defined-types/02-type-definition-create-your-own-type/main.go b/09-go-type-system/05-defined-types/02-type-definition-create-your-own-type/main.go new file mode 100644 index 0000000..05d6852 --- /dev/null +++ b/09-go-type-system/05-defined-types/02-type-definition-create-your-own-type/main.go @@ -0,0 +1,54 @@ +// 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" +) + +// type definitions usually declared at the package level +// +// EXERCISE: Move the declaration into main()'s scope +// +type ( + gram float64 // float64 is the underlying type of gram + ounce float64 // float64 is the underlying type of ounce +) + +// above code is the same as the following: +// type gram float64 +// type ounce float64 + +func main() { + // type definitions are also allowed in blocks + + // underlying types are the same + var g gram = 1000 // gram -> float64 + var o ounce // ounce -> float64 + + // when the underlying types are the same + // then they can be converted between: + // gram, ounce or float64 + + // afterwards, you'll see also that, + // the important thing is the structure + // of the type. not just its name. + // + // float64 has the real structure, representation, + // and size. + // + // so, it gives the structure to the newly defined type. + + // TYPE ERROR: ounce and grams are different types + // o = g * 0.035274 + + // BUT: They're convertable to each other + o = ounce(g) * 0.035274 + + fmt.Printf("%g grams is %.2f ounce\n", g, o) +} diff --git a/09-go-type-system/05-defined-types/03-underlying-types/main.go b/09-go-type-system/05-defined-types/03-underlying-types/main.go new file mode 100644 index 0000000..5d5b235 --- /dev/null +++ b/09-go-type-system/05-defined-types/03-underlying-types/main.go @@ -0,0 +1,72 @@ +// 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" + + "github.com/inancgumus/learngo/09-go-type-system/05-defined-types/03-underlying-types/weights" +) + +type ( + // Gram underlying type is int + Gram int + + // Kilogram underlying type is int + Kilogram Gram + + // Ton underlying type is int + Ton Kilogram +) + +func main() { + var ( + salt Gram = 100 + apples Kilogram = 5 + truck Ton = 10 + ) + + // types with different names cannot be used together + // salt = apples + // apples = truck + + // since their underlying type is int + // they can be converted to any type + // with an underlying type of int + salt = Gram(apples) + apples = Kilogram(truck) + truck = Ton(Kilogram(Gram(int(apples)))) + + fmt.Printf("salt: %d, apples: %d, truck: %d\n", + salt, apples, truck) + + // ----------------------------------------------------- + // TYPES FROM DIFFERENT PACKAGES + // ----------------------------------------------------- + + // Gram and weights.Gram are different types + + // You cannot do this + // salt = weights.Gram(100) + + // But, you can convert it back to Gram + // Since, their underlying type is int + salt = Gram(weights.Gram(100)) + + fmt.Printf("Type of weights.Gram: %T\n", weights.Gram(1)) + fmt.Printf("Type of main.Gram : %T\n", Gram(1)) + + // You can declare a new type + // from a type of an external package + type myGram weights.Gram + + var pepper myGram = 20 + pepper = myGram(salt) + + fmt.Printf("Type of pepper : %T\n", pepper) +} diff --git a/09-go-type-system/05-defined-types/03-underlying-types/weights/weights.go b/09-go-type-system/05-defined-types/03-underlying-types/weights/weights.go new file mode 100644 index 0000000..c800574 --- /dev/null +++ b/09-go-type-system/05-defined-types/03-underlying-types/weights/weights.go @@ -0,0 +1,19 @@ +// 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 weights + +type ( + // Gram underlying type is int64 + Gram int + + // Kilogram underlying type is int64 + Kilogram Gram + + // Ton underlying type is int64 + Ton Kilogram +) diff --git a/09-go-type-system/06-aliased-types/main.go b/09-go-type-system/06-aliased-types/main.go new file mode 100644 index 0000000..6f37520 --- /dev/null +++ b/09-go-type-system/06-aliased-types/main.go @@ -0,0 +1,39 @@ +// 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 + +func main() { + // aliased types are the same types + // just with different names + // for readability and refactoring + var ( + // type byte = int8 + byteVal byte + uint8Val uint8 + intVal int + ) + + uint8Val = byteVal // ok + + var ( + // type rune = int32 + runeVal rune + int32Val int32 + ) + + runeVal = int32Val // ok + + runeVal = rune(int32Val) + runeVal = rune(runeVal) + + // keep the compiler happy + _, _, _, _ = byteVal, uint8Val, intVal, runeVal +} + +// For the curious - compiler internals: +// https://github.com/golang/go/blob/4f1f503373cda7160392be94e3849b0c9b9ebbda/src/cmd/compile/internal/gc/universe.go#L409 diff --git a/09-go-type-system/exercises/01/main.go b/09-go-type-system/exercises/01/main.go new file mode 100644 index 0000000..23b5f28 --- /dev/null +++ b/09-go-type-system/exercises/01/main.go @@ -0,0 +1,49 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Choose the optimal data types for the given situations. +// 2. Print them all +// 3. Try converting them to lesser data types. +// For example try converting int64 variable to int32. +// Then observe the result. +// Search the web why the result is so? +// +// NOTE +// This is just an exercise for teaching you different +// data types. Do not apply it to the real-life. +// +// As I said in the lectures that, premature optimization +// is not a good thing. +// --------------------------------------------------------- + +func main() { + // DONT FORGET: There are also unsigned data types. + // (For positive numbers) + + // DO NOT USE: int data type + // Use only the ones with the bitsizes + + // --- + + // an english letter (search web for: ascii control code) + + // a non-english letter (search web for: unicode codepoint) + + // a year in 4 digits like 2040 + + // a month in 2 digits: 1 to 12 + + // the speed of the light + + // angle of a circle + + // PI number: 3.141592653589793 +} diff --git a/09-go-type-system/exercises/01/solution/main.go b/09-go-type-system/exercises/01/solution/main.go new file mode 100644 index 0000000..be6f7d7 --- /dev/null +++ b/09-go-type-system/exercises/01/solution/main.go @@ -0,0 +1,41 @@ +// 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() { + // an english letter (search web for: ascii control code) + var letter byte = 'A' + fmt.Println("an english letter:", letter) + + // a non-english letter (search web for: unicode codepoint) + var unicode rune + unicode = 'Ç' + fmt.Println("a non-english letter:", unicode) + + // a year in 4 digits like 2040 + var year uint16 = 2040 + fmt.Println("a year in 4 digits like 2040:", year) + + // a month in 2 digits: 1 to 12 + var month uint8 = 6 + fmt.Println("a month in 2 digits: 1 to 12:", month) + + // the speed of the light + var lightSpeed int64 = 670616629 // miles + fmt.Println("the speed of the light:", lightSpeed) + + // angle of a circle + var angle float32 = 35.8 + fmt.Println("angle of a circle:", angle) + + var pi float64 + pi = 3.141592653589793 + fmt.Println("PI number:", pi) +} diff --git a/09-go-type-system/exercises/02/main.go b/09-go-type-system/exercises/02/main.go new file mode 100644 index 0000000..81ae157 --- /dev/null +++ b/09-go-type-system/exercises/02/main.go @@ -0,0 +1,39 @@ +// 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" +) + +// --------------------------------------------------------- +// EXERCISE +// Solve the data type problem in the program. +// +// EXPECTED OUTPUT +// width: 265 height: 265 +// are they equal? true +// --------------------------------------------------------- + +func main() { + // FIX THIS: + // Change the following data types to the correct + // data types where appropriate. + var ( + width uint8 + height uint16 + ) + + // DONT TOUCH THIS: + width, height = 255, 265 + width += 10 + fmt.Printf("width: %d height: %d\n", width, height) + + // UNCOMMENT THIS: + // fmt.Println("are they equal?", width == height) +} diff --git a/09-go-type-system/exercises/02/solution/main.go b/09-go-type-system/exercises/02/solution/main.go new file mode 100644 index 0000000..772956f --- /dev/null +++ b/09-go-type-system/exercises/02/solution/main.go @@ -0,0 +1,23 @@ +// 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() { + var ( + width uint16 + height uint16 + ) + + width, height = 255, 265 + width += 10 + + fmt.Printf("width: %d height: %d\n", width, height) + fmt.Println("are they equal?", width == height) +} diff --git a/09-go-type-system/exercises/03/main.go b/09-go-type-system/exercises/03/main.go new file mode 100644 index 0000000..9a407fc --- /dev/null +++ b/09-go-type-system/exercises/03/main.go @@ -0,0 +1,78 @@ +// 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" + "os" + "strconv" +) + +// --------------------------------------------------------- +// EXERCISE +// Use strconv.ParseInt function to get int8, int16, and +// int32, and int64 values from command-line. +// +// HINT +// The third argument to ParseInt function represents +// the bitsize. +// +// So, giving it 8 returns an int8 convertable value; +// whereas 16 returns an int16 convertable value. +// +// Please explore the documentation of ParseInt function +// and learn how it works. +// +// EXPECTED OUTPUT +// When runned like this: +// go run main.go 50 25000 2000000 50000000000000000 00000100 +// +// It should return this: +// int8 value is : 50 +// int16 value is: 25000 +// int32 value is: 2000000 +// int64 value is: 50000000000000000 +// 00000100 is: 4 +// --------------------------------------------------------- + +func main() { + // -------------------------------------- + // EXAMPLE: + // -------------------------------------- + // How to get an int8 from command-line: + // First argument should be a value of -128 to 127 range + // + // Second argument: 10 means decimal number + // Third argument : 8 means 8-bits (int8) + val, _ := strconv.ParseInt(os.Args[1], 10, 8) + + // Now the val variable is int64 because ParseInt + // returns an int64. But, since I passed 8 as its third + // argument, it returns int8 convertable value. + // + // Try running the program with a value of -128 to 127 + // Running it beyond that range will result in + // either -128 or 127. + fmt.Println("int8 value is:", int8(val)) + + // -------------------------------------- + // NOW IT'S YOUR TURN! + // -------------------------------------- + + // 1. Get an int16 value using ParseInt and convert it and print it + + // 2. Get an int32 value using ParseInt and convert it and print it + + // 3. Get an int64 value using ParseInt and convert it and print it + + // 4. Get an int8 value using ParseInt and convert it and print it + // But this time, get the value in bits. + // + // For example : 00000100 + // Should print: 4 +} diff --git a/09-go-type-system/exercises/03/solution/main.go b/09-go-type-system/exercises/03/solution/main.go new file mode 100644 index 0000000..d3fa1de --- /dev/null +++ b/09-go-type-system/exercises/03/solution/main.go @@ -0,0 +1,44 @@ +// 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" + "os" + "strconv" +) + +func main() { + // FOR EXAMPLE: + // + // Run this program with 5 arguments like this: + // + // int8 int16 int32 int64 as bits + // go run main.go 50 25000 2000000 5000000000 00000100 + + // first argument is an int8 + val, _ := strconv.ParseInt(os.Args[1], 10, 8) + fmt.Println("int8 value is :", int8(val)) + + // 2nd argument is an int16 + val, _ = strconv.ParseInt(os.Args[2], 10, 16) + fmt.Println("int16 value is:", int16(val)) + + // 3rd argument is an int32 + val, _ = strconv.ParseInt(os.Args[3], 10, 32) + fmt.Println("int32 value is:", int32(val)) + + // 4th argument is an int64 + val, _ = strconv.ParseInt(os.Args[4], 10, 64) + fmt.Println("int64 value is:", int64(val)) + + // 5th argument is a number in bits. And its int8. + // ParseInt(.., 2, ...) -> 2 means binary base + val, _ = strconv.ParseInt(os.Args[5], 2, 8) + fmt.Printf("%s is: %d\n", os.Args[5], int8(val)) +} diff --git a/09-go-type-system/exercises/04/main.go b/09-go-type-system/exercises/04/main.go new file mode 100644 index 0000000..73474ff --- /dev/null +++ b/09-go-type-system/exercises/04/main.go @@ -0,0 +1,51 @@ +// 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" + "time" +) + +// --------------------------------------------------------- +// EXERCISE +// You should get an argument from the command-line and +// you need to multiply the time duration value `t` with +// the given argument. +// +// 1. Get an argument from the command-line +// 2. Convert it to int64 and store it in a variable +// 3. Multiply the `t` variable with that variable +// 4. Print the result +// +// HINT +// You can use ParseInt to convert the command-line +// argument into an int64 value. +// +// You can skip the error value using a blank-identifier. +// +// EXPECTED OUTPUT +// +// When runned like this: +// go run main.go 2 +// +// It should print this: +// 3h0m0s +// --------------------------------------------------------- + +func main() { + // DONT TOUCH THIS + // 1h30m means: 1 hour 30 minutes + t, _ := time.ParseDuration("1h30m") + + // TYPE YOUR CODE HERE + // .... + + // DONT TOUCH THIS + fmt.Println(t) +} diff --git a/09-go-type-system/exercises/04/solution/main.go b/09-go-type-system/exercises/04/solution/main.go new file mode 100644 index 0000000..cb79f7a --- /dev/null +++ b/09-go-type-system/exercises/04/solution/main.go @@ -0,0 +1,32 @@ +// 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" + "os" + "strconv" + "time" +) + +func main() { + t, _ := time.ParseDuration("1h30m") + + // 1. get the first command-line argument + // 2. convert it to int64 + multiplier, _ := strconv.ParseInt(os.Args[1], 10, 64) + + // 3. multiply the time duration with the given argument + // + // converts the int64 value to time.Duration to be + // able to multiply it with the time.Duration value + t *= time.Duration(multiplier) + + // 4. print it + fmt.Println(t) +} diff --git a/09-go-type-system/exercises/05/main.go b/09-go-type-system/exercises/05/main.go new file mode 100644 index 0000000..209acaf --- /dev/null +++ b/09-go-type-system/exercises/05/main.go @@ -0,0 +1,47 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Remember the feet to meters program? +// Now, it's time to refactor it. +// Define your own Feet and Meters types. +// +// Follow the steps inside the code. +// --------------------------------------------------------- + +func main() { + // ---------------------------- + // 1. Define Feet and Meters types below + // Their underlying type can be int64 + // ... + + // ---------------------------- + // 2. UNCOMMENT THE CODE BELOW THEN DON'T TOUCH IT + // var ( + // feet Feet + // meters Meters + // ) + + // ---------------------------- + // 3. Get feet value from the command-line + // 4. Convert it to an int64 first using ParseFloat + // 5. Then, convert it into a Feet type + // ... TYPE YOUR CODE HERE + + // ---------------------------- + // 6. Uncomment the code below + // 7. And, convert the expression to Meters type + + // meters = feet * 0.3048 + + // ---------------------------- + // 8. UNCOMMENT THE CODE BELOW + // fmt.Printf("%g feet is %g meters.\n", feet, meters) +} diff --git a/09-go-type-system/exercises/05/solution/main.go b/09-go-type-system/exercises/05/solution/main.go new file mode 100644 index 0000000..1d90214 --- /dev/null +++ b/09-go-type-system/exercises/05/solution/main.go @@ -0,0 +1,41 @@ +// 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" + "os" + "strconv" +) + +// WHY? +// Because, you can add methods to your types in the future +// And, they're type-safe and more readable now. + +func main() { + type ( + Feet float64 + Meters float64 + ) + + var ( + feet Feet + meters Meters + ) + + arg := os.Args[1] + + // feet is a Feet value now + val, _ := strconv.ParseFloat(arg, 64) + feet = Feet(val) + + // meters is a Meters value now + meters = Meters(feet * 0.3048) + + fmt.Printf("%g feet is %g meters.\n", feet, meters) +} diff --git a/09-go-type-system/exercises/06/main.go b/09-go-type-system/exercises/06/main.go new file mode 100644 index 0000000..dd29443 --- /dev/null +++ b/09-go-type-system/exercises/06/main.go @@ -0,0 +1,51 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// Convert the variables to appropriate types. +// +// EXPECTED OUTPUT +// 325.5 299.5 +// --------------------------------------------------------- + +func main() { + // DONT TOUCH THIS: + type ( + Temperature float64 + Celsius Temperature + Fahrenheit Temperature + ) + + // DONT TOUCH THIS: + var ( + celsius Celsius = 15.5 + fahr Fahrenheit = 59.9 + celsiusDegree Temperature = 10.5 + fahrDegree Temperature = 2.5 + factor = 2. + ) + + // ---------------------------------------- + // FIX THE CODE BELOW: + // You should solve it only by using conversions. + // Do not change the code in any other way. + + // celsius *= celsiusDegree * factor + // fahr *= fahrDegree * factor + + // ---------------------------------------- + // DONT TOUCH THIS + fmt.Println(celsius, fahr) + + // YOU MAY REMOVE THESE WHEN YOU'RE DONE + _, _, _ = celsiusDegree, fahrDegree, factor +} diff --git a/09-go-type-system/exercises/06/solution/main.go b/09-go-type-system/exercises/06/solution/main.go new file mode 100644 index 0000000..4aeed8f --- /dev/null +++ b/09-go-type-system/exercises/06/solution/main.go @@ -0,0 +1,31 @@ +// 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() { + type ( + Temperature float64 + Celsius Temperature + Fahrenheit Temperature + ) + + var ( + celsius Celsius = 15.5 + fahr Fahrenheit = 59.9 + celsiusDegree Temperature = 10.5 + fahrDegree Temperature = 2.5 + factor = 2. + ) + + celsius *= Celsius(float64(celsiusDegree) * factor) + fahr *= Fahrenheit(float64(fahrDegree) * factor) + + fmt.Println(celsius, fahr) +} diff --git a/09-go-type-system/questions/01-questions-predeclared-types.md b/09-go-type-system/questions/01-questions-predeclared-types.md new file mode 100644 index 0000000..f765a09 --- /dev/null +++ b/09-go-type-system/questions/01-questions-predeclared-types.md @@ -0,0 +1,119 @@ +## Which one is **not** a predeclared data type of Go? +1. int +2. float64 +3. uint64 +4. uint +5. duration *CORRECT* +6. int8 +7. rune +8. byte +9. float32 +10. complex128 + + +## What's a predeclared data type? +1. A data type used only in the compiler +2. A built-in data type that comes with Go that you can use it from anywhere without importing any package *CORRECT* +3. The data type of a variable + + +## By using only 8 bits, how many different numbers or states can you represent? +1. 8 +2. 16 +3. 256 *CORRECT* +4. 65536 + +> 3. 2^8 is 256, so you can represent 256 different states. + + +## How many bits 2 bytes contains? +1. 2 +2. 8 +3. 16 *CORRECT* +4. 32 +5. 64 + + +## What's the output of following code? +```go +fmt.Printf("%08b = %d", 2) +``` +1. 00000001 +2. 00000010 *CORRECT* +3. 00000100 +4. 00001000 + +> EXPLANATION = From right to left, each bit goes from 2^0 to 2^(n - 1). + +> 1. EXPLANATION. Here: 1 is the first digit from the right. So, it is 2^(1 - 1) = 2^0 = 1. +> 2. EXPLANATION. Here: 1 is the second digit from the right. So, it is 2^(2 - 1) = 2^1 = 2. +> 3. EXPLANATION. Here: 1 is the third digit from the right. So, it is 2^(3 - 1) = 2^2 = 4. +> 4. EXPLANATION. Here: 1 is the fourth digit from the right. So, it is 2^(4 - 1) = 2^3 = 8. + + +## How many bytes of memory does an int64 value use? +1. 4 +2. 8 *CORRECT* +3. 32 +4. 64 + +> 2. 1 byte is 8 bits and int64 is 64 bits. So, 64/8=8 bytes. + + +## How many bytes are needed to store a value of uint32 type? +1. 4 *CORRECT* +2. 8 +3. 32 +4. 64 + +> 2. 1 byte is 8 bits and uint32 is 32 bits. So, 32/8=4 bytes. + + +## What's the size of int data type? +1. Depends: 32 bits or 64 bits. *CORRECT* +2. 32 bits +3. 64 bits + +> 1. That's right. Go can change its size at the compile-time depending on which target machine you're compiling your program into. + + +## English letters can be represented by the numbers within the range of: 0-255. For example, 'A' can be 65. Or, 'B' can be 66. So, what's the best data type for storing an English letter? + +1. byte *CORRECT* +2. rune +3. int64 +4. float64 + +> 1. That's right. A byte can represent 0-255 different values. So, it's a great fit for representing English letters, and numbers. +> 2. In practice, you can do it with a rune value. However, rune is 32-bits long and it can store almost every letter in existince. I'm asking for the optimal data type. Try again. +> 3. That would be too large for only 255 different numbers. +> 4. Float is not the best data type for numbers without fractional parts. + + + +## What does the following code print? +```go +var letter uint8 = 255 +fmt.Print(letter + 5) +``` +1. 0 +2. 4 *CORRECT* +3. 5 +4. 260 + +> 2. Unsigned integers wrap around after their maximum capacity. Uint8's max is 255, so, if 255 + 1 is 0, then 255 + 5 is 4. +> 3. You're very close. +> 4. Uint8's max capacity is 255. It can't be 260. + + +## What does the following code print? +```go +var num int8 = -128 +fmt.Print(num - 3) +``` +1. -131 +2. 125 *CORRECT* +3. -125 + +> 1. int8's min capacity is -128. It can't be -131. +> 2. Signed integers wrap around after their minimum capacity. int8's min is -128, so, if -128 - 1 is 127, then -128 - 3 is 125. \ No newline at end of file diff --git a/09-go-type-system/questions/02-questions-defined-types.md b/09-go-type-system/questions/02-questions-defined-types.md new file mode 100644 index 0000000..655abc5 --- /dev/null +++ b/09-go-type-system/questions/02-questions-defined-types.md @@ -0,0 +1,116 @@ +## Why you want to define new types? +1. To declare new methods +2. For type-safety +3. For readability and conveying meaning +4. All of the options above *CORRECT* + +> 1-3. Yes, that's only one of the reasons. + + +## Let's suppose that you've declared the following defined type. Which property below that the new type doesn't get from its underlying type? + +```go +// For example, let's say that you've defined a new type +// using time.Duration type like this: +type Millennium time.Duration +``` +1. Methods *CORRECT* +2. Representation +3. Size +4. Range of values + +> 1. That's right. A defined type doesn't get its source type's methods. +> 2-4. Actually the defined type gets it from its underlying type. + + +## How to define a new type using float32? +1. `var radius float32` +2. `radius = type float32` +3. `type radius float32` *CORRECT* +4. `type radius = float32` + +> 1-2. This is not a correct syntax. +> 3. `radius` is a new type, defined using `float32`. +> 4. This declares `radius` as an alias to `float32`. So, they become the same types. + + +## How to fix the following code? +```go +type Distance int + +var ( + village Distance = 50 + city = 100 +) + +fmt.Print(village + city) +``` +1. `int(village + city)` +2. `village + int(city)` +3. `village(int) + city` +4. `village + Distance(city)` *CORRECT* + +> 1-3. There's a type mismatch in this code. But, this won't fix it. +> 4. That's right. Now, the `city`'s type is Distance in the expression. + + +## For the following program which types you might want to declare? +```go +package main +import "fmt" + +func main() { + celsius := 35. + fahrenheit := (9*celsius + 160) / 5 + fmt.Printf("%g ºC is %g ºF\n", celsius, fahrenheit) +} +``` + +1. Celsius and Fahrenheit using int64 +2. Celsius and Fahrenheit using float64 *CORRECT* +3. Temperature using int64 +4. Temperature using uint32 + +> 1. But a degree value has a floating part. So, using an integer may not the best way. +> 2. float64 can represent a degree value. +> 3-4. But a degree value has a floating part. So, using an integer may not the best way. Also, there are two different temperature units here: Celsius and Fahrenheit. Isn't it better to create two distinct types? + + +## What's the underlying type of the Millennium type? +```go +type ( + Duration int64 + Century Duration + Millennium Century +) +``` +1. `int64` *CORRECT* +2. `Duration` +3. `Century` +4. Another type + +> 1. That's right. Go's type system is flat. So, the defined type's underlying type is a type with a real structure. int64 is not just a name, it has its own structure, it's a predeclared type. +> 2. Duration is just a new type name. It doesn't its own structure. +> 3. Century is just a new type name. It doesn't its own structure. + + +## Which types do not need to be converted between each other? +**HINT:** Aliased types do not require type conversion. + +1. byte and uint8 *CORRECT* +2. byte and rune +3. rune and uint8 +4. byte and uint32 + +> 1. byte data type is an alias to uint8 data type. So, they don't need conversion between each other. They're the same types. + + +## Which types do not need to be converted between each other? +**HINT:** Aliased types do not require type conversion. + +1. byte and uint32 +2. byte and rune +3. rune and int32 *CORRECT* +4. byte and int8 + +> 3. rune data type is an alias to int32 data type. So, they don't need conversion between each other. They're the same types. \ No newline at end of file diff --git a/10-constants/01-declarations/01-syntax/01-magic-numbers/main.go b/10-constants/01-declarations/01-syntax/01-magic-numbers/main.go new file mode 100644 index 0000000..6cc0382 --- /dev/null +++ b/10-constants/01-declarations/01-syntax/01-magic-numbers/main.go @@ -0,0 +1,22 @@ +// 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" + +// This program uses magic values + +func main() { + cm := 100 + m := cm / 100 // 100 is a magic value + fmt.Printf("%dcm is %dm\n", cm, m) + + cm = 200 + m = cm / 100 // 100 is a magic value + fmt.Printf("%dcm is %dm\n", cm, m) +} diff --git a/10-constants/01-declarations/01-syntax/02-constants/main.go b/10-constants/01-declarations/01-syntax/02-constants/main.go new file mode 100644 index 0000000..2dcbce3 --- /dev/null +++ b/10-constants/01-declarations/01-syntax/02-constants/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" + +// This program uses a named constant +// instead of a magic value + +func main() { + const meters int = 100 + + cm := 100 + m := cm / meters // using a named constant + fmt.Printf("%dcm is %dm\n", cm, m) + + cm = 200 + m = cm / meters // using a named constant + fmt.Printf("%dcm is %dm\n", cm, m) +} diff --git a/10-constants/01-declarations/01-syntax/03-safety/main.go b/10-constants/01-declarations/01-syntax/03-safety/main.go new file mode 100644 index 0000000..dcf412e --- /dev/null +++ b/10-constants/01-declarations/01-syntax/03-safety/main.go @@ -0,0 +1,26 @@ +// 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() { + // Go can't catch the same error at runtime + // When you run this, there will be an error: + // + // panic: runtime error: integer divide by zero + n, m := 1, 0 + fmt.Println(n / m) + + // Go will detect the division by zero error + // at compile-time + // + // const n int = 1 + // const m int = 0 + // fmt.Println(n / m) +} diff --git a/10-constants/01-declarations/01-syntax/04-rules/01-immutability/main.go b/10-constants/01-declarations/01-syntax/04-rules/01-immutability/main.go new file mode 100644 index 0000000..6b1d2ed --- /dev/null +++ b/10-constants/01-declarations/01-syntax/04-rules/01-immutability/main.go @@ -0,0 +1,16 @@ +// 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 + +func main() { + const max int = 100 + + // ERROR: cannot assign to max + // constants are immutable (cannot change) + // max = 100 +} diff --git a/10-constants/01-declarations/01-syntax/04-rules/02-runtime-func/main.go b/10-constants/01-declarations/01-syntax/04-rules/02-runtime-func/main.go new file mode 100644 index 0000000..487baa3 --- /dev/null +++ b/10-constants/01-declarations/01-syntax/04-rules/02-runtime-func/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" +) + +func main() { + // math.Pow calculates the power of the given number + fmt.Println(math.Pow10(2)) // 100 + fmt.Println(math.Pow10(3)) // 1000 + fmt.Println(math.Pow10(4)) // 10000 + + // ERROR: math.Pow is not a constant + // constants cannot use runtime constructs + + // const max int = math.Pow10(2) +} diff --git a/10-constants/01-declarations/01-syntax/04-rules/03-runtime-var/main.go b/10-constants/01-declarations/01-syntax/04-rules/03-runtime-var/main.go new file mode 100644 index 0000000..ce727f7 --- /dev/null +++ b/10-constants/01-declarations/01-syntax/04-rules/03-runtime-var/main.go @@ -0,0 +1,17 @@ +// 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 + +func main() { + n := 2 + + // ERROR: n is not a constant + // const max int = n + + _ = n +} diff --git a/10-constants/01-declarations/01-syntax/04-rules/04-len/main.go b/10-constants/01-declarations/01-syntax/04-rules/04-len/main.go new file mode 100644 index 0000000..57415fc --- /dev/null +++ b/10-constants/01-declarations/01-syntax/04-rules/04-len/main.go @@ -0,0 +1,21 @@ +// 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() { + // When argument to len is a constant, len can be used + // while initializing a constant + // + // Here, "Hello" is a constant. + + const max int = len("Hello") + + fmt.Println(max) +} diff --git a/10-constants/01-declarations/02-constant-types-and-expressions/01/main.go b/10-constants/01-declarations/02-constant-types-and-expressions/01/main.go new file mode 100644 index 0000000..ec1e756 --- /dev/null +++ b/10-constants/01-declarations/02-constant-types-and-expressions/01/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() { + const min int = 1 + const pi float64 = 3.14 + const version string = "2.0.1" + const debug bool = true + const A rune = 'A' + + fmt.Println(min, pi, version, debug, A) +} diff --git a/10-constants/01-declarations/02-constant-types-and-expressions/02/main.go b/10-constants/01-declarations/02-constant-types-and-expressions/02/main.go new file mode 100644 index 0000000..5bdb217 --- /dev/null +++ b/10-constants/01-declarations/02-constant-types-and-expressions/02/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() { + const min = 1 + const pi = 3.14 + const version = "2.0.1" + const debug = true + const A = 'A' + + fmt.Println(min, pi, version, debug, A) +} diff --git a/10-constants/01-declarations/02-constant-types-and-expressions/03/main.go b/10-constants/01-declarations/02-constant-types-and-expressions/03/main.go new file mode 100644 index 0000000..49e0d88 --- /dev/null +++ b/10-constants/01-declarations/02-constant-types-and-expressions/03/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() { + const min = 1 + 1 + const pi = 3.14 * min + const version = "2.0.1" + "-beta" + const debug = !true + const A rune = 'A' + 1 + + fmt.Println(min, pi, version, debug, A) +} diff --git a/10-constants/01-declarations/03-multiple-declaration/01/main.go b/10-constants/01-declarations/03-multiple-declaration/01/main.go new file mode 100644 index 0000000..84030d0 --- /dev/null +++ b/10-constants/01-declarations/03-multiple-declaration/01/main.go @@ -0,0 +1,26 @@ +// 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() { + // below declaration is the same as this one: + + // const ( + // min int = 1 + // max int = 1000 + // ) + + const min, max int = 1, 1000 + + fmt.Println(min, max) + + // print the types of min and max + fmt.Printf("%T %T\n", min, max) +} diff --git a/10-constants/01-declarations/03-multiple-declaration/02/main.go b/10-constants/01-declarations/03-multiple-declaration/02/main.go new file mode 100644 index 0000000..3aff615 --- /dev/null +++ b/10-constants/01-declarations/03-multiple-declaration/02/main.go @@ -0,0 +1,23 @@ +// 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() { + // constants repeat the previous type and expression + const ( + min int = 1 + max // int = 1 + ) + + fmt.Println(min, max) + + // print the types of min and max + fmt.Printf("%T %T\n", min, max) +} diff --git a/10-constants/01-declarations/03-multiple-declaration/03/main.go b/10-constants/01-declarations/03-multiple-declaration/03/main.go new file mode 100644 index 0000000..837caf9 --- /dev/null +++ b/10-constants/01-declarations/03-multiple-declaration/03/main.go @@ -0,0 +1,23 @@ +// 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() { + // constants repeat the previous type and expression + const ( + min int = 1 + 1 + max // int = 1 + 1 + ) + + fmt.Println(min, max) + + // print the types of min and max + fmt.Printf("%T %T\n", min, max) +} diff --git a/10-constants/02-typeless-constants/01-typeless-constants/main.go b/10-constants/02-typeless-constants/01-typeless-constants/main.go new file mode 100644 index 0000000..ca0c9be --- /dev/null +++ b/10-constants/02-typeless-constants/01-typeless-constants/main.go @@ -0,0 +1,18 @@ +// 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() { + // min and pi are typeless constants + const min = 1 + 1 + const pi = 3.14 * min + + fmt.Println(min, pi) +} diff --git a/10-constants/02-typeless-constants/02-typed-vs-typeless/01/main.go b/10-constants/02-typeless-constants/02-typed-vs-typeless/01/main.go new file mode 100644 index 0000000..25c7d9c --- /dev/null +++ b/10-constants/02-typeless-constants/02-typed-vs-typeless/01/main.go @@ -0,0 +1,19 @@ +// 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() { + const min int = 42 + + var i int + i = min // OK + + fmt.Println(i) +} diff --git a/10-constants/02-typeless-constants/02-typed-vs-typeless/02/main.go b/10-constants/02-typeless-constants/02-typed-vs-typeless/02/main.go new file mode 100644 index 0000000..65f27fe --- /dev/null +++ b/10-constants/02-typeless-constants/02-typed-vs-typeless/02/main.go @@ -0,0 +1,21 @@ +// 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() { + const min int = 42 + + var f float64 + + // ERROR: Type Mismatch + // f = min // NOT OK + + fmt.Println(f) +} diff --git a/10-constants/02-typeless-constants/02-typed-vs-typeless/03/main.go b/10-constants/02-typeless-constants/02-typed-vs-typeless/03/main.go new file mode 100644 index 0000000..4ebded2 --- /dev/null +++ b/10-constants/02-typeless-constants/02-typed-vs-typeless/03/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() { + const min = 42 + + var f float64 + + f = min // OK when min is typeless + + fmt.Println(f) +} diff --git a/10-constants/02-typeless-constants/02-typed-vs-typeless/04/main.go b/10-constants/02-typeless-constants/02-typed-vs-typeless/04/main.go new file mode 100644 index 0000000..eb5a2b0 --- /dev/null +++ b/10-constants/02-typeless-constants/02-typed-vs-typeless/04/main.go @@ -0,0 +1,31 @@ +// 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() { + const min = 42 + + // i've removed int from the below declaration + // since, min's default type is int (you'll learn) + var i = min + + var f float64 = min + var b byte = min + var j int32 = min + var r rune = min + + // behind the scenes: + // below statement equals to: + // + // var b byte = min + b = byte(min) + + fmt.Println(i, f, b, j, r) +} diff --git a/10-constants/02-typeless-constants/03-default-type/01/main.go b/10-constants/02-typeless-constants/03-default-type/01/main.go new file mode 100644 index 0000000..8d9a19e --- /dev/null +++ b/10-constants/02-typeless-constants/03-default-type/01/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() { + const min int32 = 1 + + max := 5 + min + // above line equals to this: + // max := int32(5) + min + + fmt.Printf("Type of max: %T\n", max) +} diff --git a/10-constants/02-typeless-constants/03-default-type/02/main.go b/10-constants/02-typeless-constants/03-default-type/02/main.go new file mode 100644 index 0000000..7f1399a --- /dev/null +++ b/10-constants/02-typeless-constants/03-default-type/02/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() { + const min = 1 + + max := 5 + min + // above line equals to this: + // max := int(5) + int(min) + + fmt.Printf("Type of max: %T\n", max) +} diff --git a/10-constants/02-typeless-constants/03-default-type/03/main.go b/10-constants/02-typeless-constants/03-default-type/03/main.go new file mode 100644 index 0000000..83885c6 --- /dev/null +++ b/10-constants/02-typeless-constants/03-default-type/03/main.go @@ -0,0 +1,26 @@ +// 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() { + i := 42 + f := 3.14 + b := true + s := "Hello" + r := 'A' + + fmt.Printf("i is %T\n", i) + fmt.Printf("f is %T\n", f) + fmt.Printf("b is %T\n", b) + fmt.Printf("s is %T\n", s) + + // int32 = rune (type alias) + fmt.Printf("r is %T\n", r) +} diff --git a/10-constants/02-typeless-constants/03-default-type/04/main.go b/10-constants/02-typeless-constants/03-default-type/04/main.go new file mode 100644 index 0000000..0550cca --- /dev/null +++ b/10-constants/02-typeless-constants/03-default-type/04/main.go @@ -0,0 +1,19 @@ +// 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() { + i := 42 + 3.14 // OK: 42 and 3.14 are typeless + + // above line equals to this: + // i := float64(42 + 3.14) + + fmt.Printf("i is %T\n", i) +} diff --git a/10-constants/02-typeless-constants/03-default-type/05/main.go b/10-constants/02-typeless-constants/03-default-type/05/main.go new file mode 100644 index 0000000..7bbb3e7 --- /dev/null +++ b/10-constants/02-typeless-constants/03-default-type/05/main.go @@ -0,0 +1,15 @@ +// 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 + +func main() { + // NOT OK + // "Hello" string literal cannot be converted + // into a bool value + // f := true + "Hello" +} diff --git a/10-constants/02-typeless-constants/04-demo/01/main.go b/10-constants/02-typeless-constants/04-demo/01/main.go new file mode 100644 index 0000000..2da6fb0 --- /dev/null +++ b/10-constants/02-typeless-constants/04-demo/01/main.go @@ -0,0 +1,45 @@ +// 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" + "time" +) + +func main() { + type Duration int64 + + const ( + Nanosecond Duration = 1 + Microsecond = 1000 * Nanosecond + Millisecond = 1000 * Microsecond + Second = 1000 * Millisecond + Minute = 60 * Second + Hour = 60 * Minute + ) + + // The types are main.Duration instead of just Duration + // It's because, Duration type now belongs to main func + fmt.Printf("Nanosecond is %T\n", Nanosecond) + fmt.Printf("Microsecond is %T\n", Microsecond) + fmt.Printf("Millisecond is %T\n", Millisecond) + fmt.Printf("Second is %T\n", Second) + fmt.Printf("Minute is %T\n", Minute) + fmt.Printf("Hour is %T\n", Hour) + + // Print the types of time.Duration constants directly + // This time, types will be time.Duration + // It's because, they're inside the time package + fmt.Printf("Nanosecond is %T\n", time.Nanosecond) + fmt.Printf("Microsecond is %T\n", time.Microsecond) + fmt.Printf("Millisecond is %T\n", time.Millisecond) + fmt.Printf("Second is %T\n", time.Second) + fmt.Printf("Minute is %T\n", time.Minute) + fmt.Printf("Hour is %T\n", time.Hour) +} diff --git a/10-constants/02-typeless-constants/04-demo/02/main.go b/10-constants/02-typeless-constants/04-demo/02/main.go new file mode 100644 index 0000000..048c0a0 --- /dev/null +++ b/10-constants/02-typeless-constants/04-demo/02/main.go @@ -0,0 +1,41 @@ +// 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" + "time" +) + +func main() { + // #1 + t := time.Second * 10 + + fmt.Println(t) + + // #2 + i := 10 + + // NOT OK + // time.Duration and int are different types + // t = time.Second * i + + // OK: i is int, Duration is int64 + // So, i is convertable to int64 + t = time.Second * time.Duration(i) + + fmt.Println(t) + + // #3 + const c = 10 + + // OK: Because, c is typeless + t = time.Second * c + + fmt.Println(t) +} diff --git a/10-constants/03-refactor-feet-to-meters/main.go b/10-constants/03-refactor-feet-to-meters/main.go new file mode 100644 index 0000000..b3cc42c --- /dev/null +++ b/10-constants/03-refactor-feet-to-meters/main.go @@ -0,0 +1,31 @@ +// 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" + "os" + "strconv" +) + +// EXERCISE +// +// This program uses magic numbers +// Refactor it and use named constants instead + +func main() { + arg := os.Args[1] + + feet, _ := strconv.ParseFloat(arg, 64) + + meters := feet * 0.3048 + yards := feet * 0.3333 + + fmt.Printf("%g feet is %g meters.\n", feet, meters) + fmt.Printf("%g feet is %g yards.\n", feet, yards) +} diff --git a/10-constants/03-refactor-feet-to-meters/solution/main.go b/10-constants/03-refactor-feet-to-meters/solution/main.go new file mode 100644 index 0000000..25c9aa0 --- /dev/null +++ b/10-constants/03-refactor-feet-to-meters/solution/main.go @@ -0,0 +1,52 @@ +// 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" +) + +// This program contains a lot of comments +// Non-commented version is in /without-comments/main.go + +func main() { + // You cannot use this syntax. + // + // feetInMeters is declared + // when the declaration completes, + // (at the end of the whole declaration) + // + // And, it's not easy to read + // + // const feetInMeters, feetInYards float64 = 0.3048, + // feetInMeters / 0.9144 + + const ( + feetInMeters = 0.3048 + feetInYards = feetInMeters / 0.9144 + + // cannot call runtime funcs + // feetInYards = math.Round(feetInYards) + ) + + // Immutable: You cannot assign new values + // feetInYards = 0.333333 + + arg := os.Args[1] + + feet, _ := strconv.ParseFloat(arg, 64) + + meters := feet * feetInMeters + yards := math.Round(feet * feetInYards) + + fmt.Printf("%g feet is %g meters.\n", feet, meters) + fmt.Printf("%g feet is %g yards.\n", feet, yards) +} diff --git a/10-constants/03-refactor-feet-to-meters/solution/without-comments/main.go b/10-constants/03-refactor-feet-to-meters/solution/without-comments/main.go new file mode 100644 index 0000000..44b4f9d --- /dev/null +++ b/10-constants/03-refactor-feet-to-meters/solution/without-comments/main.go @@ -0,0 +1,35 @@ +// 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" +) + +// I copied the same program here but without comments. +// So, you can easily read it. + +func main() { + const ( + feetInMeters = 0.3048 + feetInYards = feetInMeters / 0.9144 + ) + + arg := os.Args[1] + + feet, _ := strconv.ParseFloat(arg, 64) + + meters := feet * feetInMeters + yards := math.Round(feet * feetInYards) + + fmt.Printf("%g feet is %g meters.\n", feet, meters) + fmt.Printf("%g feet is %g yards.\n", feet, yards) +} diff --git a/10-constants/04-iota/01-manually/main.go b/10-constants/04-iota/01-manually/main.go new file mode 100644 index 0000000..614dd69 --- /dev/null +++ b/10-constants/04-iota/01-manually/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" + +func main() { + const ( + monday = 0 + tuesday = 1 + wednesday = 2 + thursday = 3 + friday = 4 + saturday = 5 + sunday = 6 + ) + + fmt.Println(monday, tuesday, wednesday, thursday, friday, + saturday, sunday) +} diff --git a/10-constants/04-iota/02-with-iota/main.go b/10-constants/04-iota/02-with-iota/main.go new file mode 100644 index 0000000..31b8c7b --- /dev/null +++ b/10-constants/04-iota/02-with-iota/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" + +func main() { + const ( + monday = iota + tuesday + wednesday + thursday + friday + saturday + sunday + ) + + fmt.Println(monday, tuesday, wednesday, thursday, friday, + saturday, sunday) +} diff --git a/10-constants/04-iota/03-expressions/main.go b/10-constants/04-iota/03-expressions/main.go new file mode 100644 index 0000000..66e08ff --- /dev/null +++ b/10-constants/04-iota/03-expressions/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" + +func main() { + const ( + monday = iota + 1 + tuesday + wednesday + thursday + friday + saturday + sunday + ) + + fmt.Println(monday, tuesday, wednesday, thursday, friday, + saturday, sunday) +} diff --git a/10-constants/04-iota/04-blank-identifier/01/main.go b/10-constants/04-iota/04-blank-identifier/01/main.go new file mode 100644 index 0000000..f7e8abd --- /dev/null +++ b/10-constants/04-iota/04-blank-identifier/01/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() { + const ( + EST = -5 + MST = -7 + PST = -8 + ) + + fmt.Println(EST, MST, PST) +} diff --git a/10-constants/04-iota/04-blank-identifier/02/main.go b/10-constants/04-iota/04-blank-identifier/02/main.go new file mode 100644 index 0000000..6c48f64 --- /dev/null +++ b/10-constants/04-iota/04-blank-identifier/02/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() { + const ( + EST = -(5 + iota) // CORRECT: -5 + MST // INCORRECT: -6 + PST // INCORRECT: -7 + ) + + fmt.Println(EST, MST, PST) +} diff --git a/10-constants/04-iota/04-blank-identifier/03/main.go b/10-constants/04-iota/04-blank-identifier/03/main.go new file mode 100644 index 0000000..f65b00f --- /dev/null +++ b/10-constants/04-iota/04-blank-identifier/03/main.go @@ -0,0 +1,21 @@ +// 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() { + const ( + EST = -(5 + iota) // CORRECT: -5 + _ + MST // CORRECT: -7 + PST // CORRECT: -8 + ) + + fmt.Println(EST, MST, PST) +} diff --git a/10-constants/exercises/01/main.go b/10-constants/exercises/01/main.go new file mode 100644 index 0000000..d804bde --- /dev/null +++ b/10-constants/exercises/01/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 + +// --------------------------------------------------------- +// EXERCISE +// Calculate how many minutes in two weeks. +// +// STEPS: +// 1. Declare `minsPerDay` constant and initialize it +// to the number of minutes in a day +// +// 2. Declare `weekDays` constant and initialize it +// to 7. +// +// 3. Use printf to print the total number of minutes +// in two weeks. +// +// EXPECTED OUTPUT +// There are 20160 minutes in 2 weeks. +// --------------------------------------------------------- + +func main() { +} diff --git a/10-constants/exercises/01/solution/main.go b/10-constants/exercises/01/solution/main.go new file mode 100644 index 0000000..384d589 --- /dev/null +++ b/10-constants/exercises/01/solution/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() { + const ( + minsPerDay = 60 * 24 + weekDays = 7 + ) + + fmt.Printf("There are %d minutes in %d weeks.\n", + minsPerDay*weekDays*2, 2) +} diff --git a/10-constants/exercises/02/main.go b/10-constants/exercises/02/main.go new file mode 100644 index 0000000..ee208f0 --- /dev/null +++ b/10-constants/exercises/02/main.go @@ -0,0 +1,30 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// Get rid of the magic numbers in the following code. +// +// RESTRICTIONS +// 1. You should declare 3 constants named: +// hoursInDay, daysInWeek, and hoursPerWeek +// +// 2. And, hoursPerWeek constant should be initialized +// using hoursInDay and daysInWeek constants. +// +// EXPECTED OUTPUT +// There are 840 hours in 5 weeks. +// --------------------------------------------------------- + +func main() { + fmt.Printf("There are %d hours in %d weeks.\n", + 24*7*5, 5) +} diff --git a/10-constants/exercises/02/solution/main.go b/10-constants/exercises/02/solution/main.go new file mode 100644 index 0000000..07a0e62 --- /dev/null +++ b/10-constants/exercises/02/solution/main.go @@ -0,0 +1,21 @@ +// 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() { + const ( + hoursInDay = 24 + daysInWeek = 7 + hoursPerWeek = hoursInDay * daysInWeek + ) + + fmt.Printf("There are %d hours in %d weeks.\n", + hoursPerWeek*5, 5) +} diff --git a/10-constants/exercises/03/main.go b/10-constants/exercises/03/main.go new file mode 100644 index 0000000..f3805d0 --- /dev/null +++ b/10-constants/exercises/03/main.go @@ -0,0 +1,34 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Calculate how many characters inside the `home` +// constant and print it. +// +// STEPS: +// 1. Declare a constant named `home` +// 2. Initialize it to "Milky Way Galaxy" string literal +// +// 3. Declare another constant named `length` +// 4. Initialize it by using the built-in function `len`. +// +// 5. Print the message below using the constants that +// you've declared. +// +// RESTRICTION: +// Use Printf. +// Print the `home` constant using the quoted-string verb. +// +// EXPECTED OUTPUT +// There are 16 characters inside "Milky Way Galaxy" +// --------------------------------------------------------- + +func main() { +} diff --git a/10-constants/exercises/03/solution/main.go b/10-constants/exercises/03/solution/main.go new file mode 100644 index 0000000..d2f61e3 --- /dev/null +++ b/10-constants/exercises/03/solution/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() { + const ( + home = "Milky Way Galaxy" + length = len(home) + ) + + fmt.Printf("There are %d characters inside %q\n", + length, home) +} diff --git a/10-constants/exercises/04/main.go b/10-constants/exercises/04/main.go new file mode 100644 index 0000000..3fc51df --- /dev/null +++ b/10-constants/exercises/04/main.go @@ -0,0 +1,26 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Fix the following program and print the TAU number. +// +// HINT +// You can use %g verb for printing tau. +// +// EXPECTED OUTPUT +// tau = 6.283185307179586 +// --------------------------------------------------------- + +func main() { + // What's the problem with this code? + // Why it doesn't work? + + // const pi, tau = 3.14159265358979323846264, pi * 2 +} diff --git a/10-constants/exercises/04/solution/main.go b/10-constants/exercises/04/solution/main.go new file mode 100644 index 0000000..a8e4b62 --- /dev/null +++ b/10-constants/exercises/04/solution/main.go @@ -0,0 +1,19 @@ +// 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() { + const ( + pi = 3.14159265358979323846264 + tau = pi * 2 + ) + + fmt.Printf("tau = %g\n", tau) +} diff --git a/10-constants/exercises/05/main.go b/10-constants/exercises/05/main.go new file mode 100644 index 0000000..ed7ec1f --- /dev/null +++ b/10-constants/exercises/05/main.go @@ -0,0 +1,28 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Fix the following program. +// +// RESTRICTION +// You should not use any variables. +// +// EXPECTED OUTPUT +// area = 1250 +// --------------------------------------------------------- + +func main() { + // const ( + // width int16 = 25 + // height int32 = width * 2 + // ) + + // fmt.Printf("area = %d\n", width*height) +} diff --git a/10-constants/exercises/05/solution/main.go b/10-constants/exercises/05/solution/main.go new file mode 100644 index 0000000..d5bd6c8 --- /dev/null +++ b/10-constants/exercises/05/solution/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() { + // Make them typeless + const ( + width = 25 + height = width * 2 + ) + + fmt.Printf("area = %d\n", width*height) +} diff --git a/10-constants/exercises/06/main.go b/10-constants/exercises/06/main.go new file mode 100644 index 0000000..9cdf4f8 --- /dev/null +++ b/10-constants/exercises/06/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 + +// --------------------------------------------------------- +// EXERCISE +// 1. Fix the program without doing any conversion. +// 2. Explain why it doesn't work. +// +// EXPECTED OUTPUT +// 10h0m0s later... +// --------------------------------------------------------- + +func main() { + // const later int = 10 + + // hours, _ := time.ParseDuration("1h") + + // fmt.Printf("%s later...\n", hours*later) +} diff --git a/10-constants/exercises/06/solution/main.go b/10-constants/exercises/06/solution/main.go new file mode 100644 index 0000000..9bbe736 --- /dev/null +++ b/10-constants/exercises/06/solution/main.go @@ -0,0 +1,28 @@ +// 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" + "time" +) + +func main() { + // hours's type is time.Duration + // but later's type was int + // now, `later` is typeless + // + // time.Duration's underlying type is int64 + // and, `later` is numeric typeless value + // so, they can be multiplied + const later = 10 + + hours, _ := time.ParseDuration("1h") + + fmt.Printf("%s later...\n", hours*later) +} diff --git a/10-constants/exercises/07/main.go b/10-constants/exercises/07/main.go new file mode 100644 index 0000000..e1a8b59 --- /dev/null +++ b/10-constants/exercises/07/main.go @@ -0,0 +1,34 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// 1. Initialize the constants using iota. +// 2. You should find the correct formula for iota. +// +// RESTRICTIONS +// 1. Remove the initializer values from all constants. +// 2. Then use iota once for initializing one of the +// constants. +// +// EXPECTED OUTPUT +// 9 10 11 +// --------------------------------------------------------- + +func main() { + const ( + Nov = 11 + Oct = 10 + Sep = 9 + ) + + fmt.Println(Sep, Oct, Nov) +} diff --git a/10-constants/exercises/07/solution/main.go b/10-constants/exercises/07/solution/main.go new file mode 100644 index 0000000..be6c520 --- /dev/null +++ b/10-constants/exercises/07/solution/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() { + const ( + Nov = 11 - iota // 11 - 0 = 11 + Oct // 11 - 1 = 10 + Sep // 11 - 2 = 9 + ) + + fmt.Println(Sep, Oct, Nov) +} diff --git a/10-constants/exercises/08/main.go b/10-constants/exercises/08/main.go new file mode 100644 index 0000000..3cc8bb3 --- /dev/null +++ b/10-constants/exercises/08/main.go @@ -0,0 +1,38 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// 1. Initialize multiple constants using iota. +// 2. Please follow the instructions inside the code. +// +// EXPECTED OUTPUT +// 1 2 3 +// --------------------------------------------------------- + +func main() { + // HINT: This is a valid constant declaration + // Blank-Identifier can be used in place of a name + const _ = iota + // ^- this is just a name + + // Now, use iota and initialize the following constants + // "automatically" to 1, 2, and 3 respectively. + const ( + Jan = iota + Feb + Mar + ) + + // This should print: 1 2 3 + // Not: 0 1 2 + fmt.Println(Jan, Feb, Mar) +} diff --git a/10-constants/exercises/08/solution/main.go b/10-constants/exercises/08/solution/main.go new file mode 100644 index 0000000..9ffc0c5 --- /dev/null +++ b/10-constants/exercises/08/solution/main.go @@ -0,0 +1,21 @@ +// 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() { + const ( + _ = iota // 0 + Jan // 1 + Feb // 2 + Mar // 3 + ) + + fmt.Println(Jan, Feb, Mar) +} diff --git a/10-constants/exercises/09/main.go b/10-constants/exercises/09/main.go new file mode 100644 index 0000000..5fc9f4a --- /dev/null +++ b/10-constants/exercises/09/main.go @@ -0,0 +1,34 @@ +// 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" + +// --------------------------------------------------------- +// EXERCISE +// Use iota to initialize month constants. +// +// HINT +// You can change the order of the constants. +// +// EXPECTED OUTPUT +// 12 3 6 9 +// --------------------------------------------------------- + +func main() { + // NOTE : You should remove all the initializers below + // first. Then use iota to fix it. + const ( + Winter = 12 + Spring = 3 + Summer = 6 + Fall = 9 + ) + + fmt.Println(Winter, Spring, Summer, Fall) +} diff --git a/10-constants/exercises/09/solution/main.go b/10-constants/exercises/09/solution/main.go new file mode 100644 index 0000000..e7135ab --- /dev/null +++ b/10-constants/exercises/09/solution/main.go @@ -0,0 +1,21 @@ +// 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() { + const ( + Spring = (iota + 1) * 3 + Summer + Fall + Winter + ) + + fmt.Println(Winter, Spring, Summer, Fall) +} diff --git a/10-constants/questions/questions.md b/10-constants/questions/questions.md new file mode 100644 index 0000000..4fb6efb --- /dev/null +++ b/10-constants/questions/questions.md @@ -0,0 +1,104 @@ +## What's a magic value? +1. A value which pops up from somewhere +2. Merlin the Wizard's spell +3. An unnamed constant value in the source code *CORRECT* +4. A named constant + + +## What's a named constant? +1. A constant with a cool name +2. A constant value declared with a name *CORRECT* +3. A literal value converted to a name + + +## How to declare a constant? +1. `Const version int = 3` +2. `const version int := 3` +2. `const version int = 3` *CORRECT* + +> 1. "C"onst should be just "c"onst. + + +## Which code below is correct? +1. `s := "pick me"; const length = len(s)` +2. `const message = "pick me!"; const length = len(message)` *CORRECT* +3. `const length = utf8.RuneCountInString("pick me")` + +> 1. `s` not a constant. +> 2. `len` function can be used as an initial value to a constant, when the argument to `len` is also a constant. +> 3. You cannot call functions while initializing a constant. + + +## Which explanation below is correct for the following code? +```go +const speed = 100 +porsche := speed * 3 +``` +1. speed is typeless and porsche's type is int *CORRECT* +2. speed's type is int and porsche's type is also int +3. speed and porsche are typeless + +> 2. speed has no type. +> 3. A variable cannot be typeless. + + +## How to fix the following code? +```go +const spell string +spell = "Abracadabra" +``` +1. `const spell = "Abracadabra"` *CORRECT* +2. `spell := "Abracadabra"` +3. `var spell = "Abracadabra"` + +> 1. A constant always have to be initialized to a value. And, sometimes the type declaration is not necessary. +> 2-3. That's a variable not a constant. + + +## How to fix the following code? +```go +const total int8 = 10 +x := 5 + +fmt.Print(total * x) +``` + +```go +// #1 - *CORRECT* +const total = 10 +x := 5 + +fmt.Print(total * x) + +// #2 +const total int64 = 10 +x := 5 + +fmt.Print(total * x) + +// #3 +const total int64 = 10 +x := 5 + +fmt.Print(int64(total) * x) +``` + +> 1. Now, the total constant is typeless, so it can be used with the x variable. +> 2. There's still a type mismatch. x is int not int64. +> 3. total is already int64. No need to convert it again. + + +## What are the values of the following constants? +```go +const ( + Yes = (iota * 5) + 2 + No + Both +) +``` +1. Yes=0 No=1 Both=2 +2. Yes=2 No=3 Both=4 +3. Yes=7 No=12 Both=17 +4. Yes=2 No=7 Both=12 *CORRECT* + +> 3. iota starts at 0, not 1. \ No newline at end of file diff --git a/11-if/01-boolean-operators/01-comparison-operators/main.go b/11-if/01-boolean-operators/01-comparison-operators/main.go new file mode 100644 index 0000000..cb0bff8 --- /dev/null +++ b/11-if/01-boolean-operators/01-comparison-operators/main.go @@ -0,0 +1,26 @@ +// 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() { + 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/11-if/01-boolean-operators/02-comparison-and-assignability/01/main.go b/11-if/01-boolean-operators/02-comparison-and-assignability/01/main.go new file mode 100644 index 0000000..dfe3a6e --- /dev/null +++ b/11-if/01-boolean-operators/02-comparison-and-assignability/01/main.go @@ -0,0 +1,21 @@ +// 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 + +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/11-if/01-boolean-operators/02-comparison-and-assignability/02/main.go b/11-if/01-boolean-operators/02-comparison-and-assignability/02/main.go new file mode 100644 index 0000000..14da7d5 --- /dev/null +++ b/11-if/01-boolean-operators/02-comparison-and-assignability/02/main.go @@ -0,0 +1,21 @@ +// 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 + +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/11-if/01-boolean-operators/02-comparison-and-assignability/03/main.go b/11-if/01-boolean-operators/02-comparison-and-assignability/03/main.go new file mode 100644 index 0000000..24054b2 --- /dev/null +++ b/11-if/01-boolean-operators/02-comparison-and-assignability/03/main.go @@ -0,0 +1,48 @@ +// 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" + "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/11-if/01-boolean-operators/03-logical-operators/01-and-operator/01/main.go b/11-if/01-boolean-operators/03-logical-operators/01-and-operator/01/main.go new file mode 100644 index 0000000..20d29aa --- /dev/null +++ b/11-if/01-boolean-operators/03-logical-operators/01-and-operator/01/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() { + // 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/11-if/01-boolean-operators/03-logical-operators/01-and-operator/02/main.go b/11-if/01-boolean-operators/03-logical-operators/01-and-operator/02/main.go new file mode 100644 index 0000000..11f3162 --- /dev/null +++ b/11-if/01-boolean-operators/03-logical-operators/01-and-operator/02/main.go @@ -0,0 +1,34 @@ +// 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() { + 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/11-if/01-boolean-operators/03-logical-operators/02-or-operator/01/main.go b/11-if/01-boolean-operators/03-logical-operators/02-or-operator/01/main.go new file mode 100644 index 0000000..3f97780 --- /dev/null +++ b/11-if/01-boolean-operators/03-logical-operators/02-or-operator/01/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() { + // 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/11-if/01-boolean-operators/03-logical-operators/02-or-operator/02/main.go b/11-if/01-boolean-operators/03-logical-operators/02-or-operator/02/main.go new file mode 100644 index 0000000..f799c8d --- /dev/null +++ b/11-if/01-boolean-operators/03-logical-operators/02-or-operator/02/main.go @@ -0,0 +1,31 @@ +// 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() { + 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/11-if/01-boolean-operators/03-logical-operators/03-not-operator/01/main.go b/11-if/01-boolean-operators/03-logical-operators/03-not-operator/01/main.go new file mode 100644 index 0000000..9cea079 --- /dev/null +++ b/11-if/01-boolean-operators/03-logical-operators/03-not-operator/01/main.go @@ -0,0 +1,18 @@ +// 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() { + 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/11-if/01-boolean-operators/03-logical-operators/03-not-operator/02/main.go b/11-if/01-boolean-operators/03-logical-operators/03-not-operator/02/main.go new file mode 100644 index 0000000..ee042fa --- /dev/null +++ b/11-if/01-boolean-operators/03-logical-operators/03-not-operator/02/main.go @@ -0,0 +1,22 @@ +// 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() { + var on bool + + on = !on + fmt.Println(on) + + on = !!on + fmt.Println(on) +} diff --git a/11-if/02-if-statement/01-if-branch/main.go b/11-if/02-if-statement/01-if-branch/main.go new file mode 100644 index 0000000..01c0711 --- /dev/null +++ b/11-if/02-if-statement/01-if-branch/main.go @@ -0,0 +1,18 @@ +// 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() { + score, valid := 5, true + + if score > 3 && valid { + fmt.Println("good") + } +} diff --git a/11-if/02-if-statement/02-else-branch/main.go b/11-if/02-if-statement/02-else-branch/main.go new file mode 100644 index 0000000..809108b --- /dev/null +++ b/11-if/02-if-statement/02-else-branch/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() { + score, valid := 3, true + + if score > 3 && valid { + fmt.Println("good") + } else { + fmt.Println("low") + } +} diff --git a/11-if/02-if-statement/03-else-if-branch/01/main.go b/11-if/02-if-statement/03-else-if-branch/01/main.go new file mode 100644 index 0000000..457aef1 --- /dev/null +++ b/11-if/02-if-statement/03-else-if-branch/01/main.go @@ -0,0 +1,22 @@ +// 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() { + score := 3 + + if score > 3 { + fmt.Println("good") + } else if score == 3 { + fmt.Println("on the edge") + } else { + fmt.Println("low") + } +} diff --git a/11-if/02-if-statement/03-else-if-branch/02/main.go b/11-if/02-if-statement/03-else-if-branch/02/main.go new file mode 100644 index 0000000..6b032b7 --- /dev/null +++ b/11-if/02-if-statement/03-else-if-branch/02/main.go @@ -0,0 +1,24 @@ +// 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() { + 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/11-if/02-if-statement/04-refactor-feet-to-meters/main.go b/11-if/02-if-statement/04-refactor-feet-to-meters/main.go new file mode 100644 index 0000000..f78f264 --- /dev/null +++ b/11-if/02-if-statement/04-refactor-feet-to-meters/main.go @@ -0,0 +1,42 @@ +// 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" + "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/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/01-challenge/main.go b/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/01-challenge/main.go new file mode 100644 index 0000000..ebf1574 --- /dev/null +++ b/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/01-challenge/main.go @@ -0,0 +1,36 @@ +// 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 + +// --------------------------------------------------------- +// 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/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/02-solution/main.go b/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/02-solution/main.go new file mode 100644 index 0000000..e3105fe --- /dev/null +++ b/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/02-solution/main.go @@ -0,0 +1,32 @@ +// 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" + "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/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/03-solution-refactor/main.go b/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/03-solution-refactor/main.go new file mode 100644 index 0000000..d029392 --- /dev/null +++ b/11-if/02-if-statement/05-challenge-userpass/01-1st-challenge/03-solution-refactor/main.go @@ -0,0 +1,41 @@ +// 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" + "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/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/01-challenge/main.go b/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/01-challenge/main.go new file mode 100644 index 0000000..d8b099b --- /dev/null +++ b/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/01-challenge/main.go @@ -0,0 +1,72 @@ +// 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" + "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/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/02-solution/main.go b/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/02-solution/main.go new file mode 100644 index 0000000..7b4ca7d --- /dev/null +++ b/11-if/02-if-statement/05-challenge-userpass/02-2nd-challenge/02-solution/main.go @@ -0,0 +1,43 @@ +// 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" + "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/11-if/03-error-handling/01-itoa/main.go b/11-if/03-error-handling/01-itoa/main.go new file mode 100644 index 0000000..d2884a0 --- /dev/null +++ b/11-if/03-error-handling/01-itoa/main.go @@ -0,0 +1,22 @@ +// 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() { + // 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/11-if/03-error-handling/02-atoi/main.go b/11-if/03-error-handling/02-atoi/main.go new file mode 100644 index 0000000..0ea4c7d --- /dev/null +++ b/11-if/03-error-handling/02-atoi/main.go @@ -0,0 +1,24 @@ +// 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" + "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/11-if/03-error-handling/03-atoi-error-handling/main.go b/11-if/03-error-handling/03-atoi-error-handling/main.go new file mode 100644 index 0000000..1ebf7c6 --- /dev/null +++ b/11-if/03-error-handling/03-atoi-error-handling/main.go @@ -0,0 +1,44 @@ +// 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" + "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/11-if/03-error-handling/04-challenge-feet-to-meters/01-challenge/main.go b/11-if/03-error-handling/04-challenge-feet-to-meters/01-challenge/main.go new file mode 100644 index 0000000..69a429b --- /dev/null +++ b/11-if/03-error-handling/04-challenge-feet-to-meters/01-challenge/main.go @@ -0,0 +1,52 @@ +// 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" + "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/11-if/03-error-handling/04-challenge-feet-to-meters/02-solution/main.go b/11-if/03-error-handling/04-challenge-feet-to-meters/02-solution/main.go new file mode 100644 index 0000000..9a648f8 --- /dev/null +++ b/11-if/03-error-handling/04-challenge-feet-to-meters/02-solution/main.go @@ -0,0 +1,42 @@ +// 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" + "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/11-if/04-short-if/01-without-short-if/main.go b/11-if/04-short-if/01-without-short-if/main.go new file mode 100644 index 0000000..c4a8a8e --- /dev/null +++ b/11-if/04-short-if/01-without-short-if/main.go @@ -0,0 +1,21 @@ +// 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() { + n, err := strconv.Atoi("42") + + if err == nil { + fmt.Println("There was no error, n is", n) + } +} diff --git a/11-if/04-short-if/02-with-short-if/main.go b/11-if/04-short-if/02-with-short-if/main.go new file mode 100644 index 0000000..49c1a35 --- /dev/null +++ b/11-if/04-short-if/02-with-short-if/main.go @@ -0,0 +1,23 @@ +// 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() { + 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/11-if/04-short-if/03-scope/main.go b/11-if/04-short-if/03-scope/main.go new file mode 100644 index 0000000..f06c506 --- /dev/null +++ b/11-if/04-short-if/03-scope/main.go @@ -0,0 +1,44 @@ +// 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" + "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/11-if/04-short-if/04-scope-shadowing/01-shadowing/main.go b/11-if/04-short-if/04-scope-shadowing/01-shadowing/main.go new file mode 100644 index 0000000..37d4d13 --- /dev/null +++ b/11-if/04-short-if/04-scope-shadowing/01-shadowing/main.go @@ -0,0 +1,37 @@ +// 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" + "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) +} diff --git a/11-if/04-short-if/04-scope-shadowing/02-shadowing-solution/main.go b/11-if/04-short-if/04-scope-shadowing/02-shadowing-solution/main.go new file mode 100644 index 0000000..4dce750 --- /dev/null +++ b/11-if/04-short-if/04-scope-shadowing/02-shadowing-solution/main.go @@ -0,0 +1,44 @@ +// 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" + "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/11-if/exercises/00/main.go b/11-if/exercises/00/main.go new file mode 100644 index 0000000..1bace58 --- /dev/null +++ b/11-if/exercises/00/main.go @@ -0,0 +1,22 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// ? +// +// NOTE +// ? +// +// EXPECTED OUTPUT +// ? +// --------------------------------------------------------- + +func main() { +} diff --git a/11-if/exercises/00/solution/main.go b/11-if/exercises/00/solution/main.go new file mode 100644 index 0000000..b5e729e --- /dev/null +++ b/11-if/exercises/00/solution/main.go @@ -0,0 +1,11 @@ +// 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 + +func main() { +} diff --git a/11-if/questions/questions.md b/11-if/questions/questions.md new file mode 100644 index 0000000..cac63ae --- /dev/null +++ b/11-if/questions/questions.md @@ -0,0 +1,3 @@ +## ? +* text *CORRECT* +* text diff --git a/12-switch/01-one-case/main.go b/12-switch/01-one-case/main.go new file mode 100644 index 0000000..efb0c15 --- /dev/null +++ b/12-switch/01-one-case/main.go @@ -0,0 +1,31 @@ +// 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() { + city := "Paris" + + switch city { + case "Paris": + fmt.Println("France") + } + + // SIMILAR TO IF + // ------------------------------------ + + // switch statement is converted to an if statement + // automatically behind the scenes + // + // above switch statement is similar to this if + + // if city == "Paris" { + // fmt.Println("France") + // } +} diff --git a/12-switch/02-multiple-cases/main.go b/12-switch/02-multiple-cases/main.go new file mode 100644 index 0000000..a3dc8ee --- /dev/null +++ b/12-switch/02-multiple-cases/main.go @@ -0,0 +1,36 @@ +// 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" + "os" +) + +func main() { + city := os.Args[1] + + switch city { + case "Paris": + fmt.Println("France") + // break // unnecessary in Go + + // vip := true + // fmt.Println("VIP trip?", vip) + + case "Tokyo": + fmt.Println("Japan") + // fmt.Println("VIP trip?", vip) + } + + // if city == "Paris" { + // fmt.Println("France") + // } else if city == "Tokyo" { + // fmt.Println("Japan") + // } +} diff --git a/12-switch/03-default-clause/main.go b/12-switch/03-default-clause/main.go new file mode 100644 index 0000000..97c3a1b --- /dev/null +++ b/12-switch/03-default-clause/main.go @@ -0,0 +1,37 @@ +// 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" + "os" +) + +func main() { + city := os.Args[1] + + switch city { + case "Paris": + fmt.Println("France") + case "Tokyo": + fmt.Println("Japan") + default: + fmt.Println("Where?") + // there can be only one default in a switch + // default: + // fmt.Println("Where?") + } + + // if city == "Paris" { + // fmt.Println("France") + // } else if city == "Tokyo" { + // fmt.Println("Japan") + // } else { + // fmt.Println("Where?") + // } +} diff --git a/12-switch/04-multiple-conditions/main.go b/12-switch/04-multiple-conditions/main.go new file mode 100644 index 0000000..0787ff2 --- /dev/null +++ b/12-switch/04-multiple-conditions/main.go @@ -0,0 +1,30 @@ +// 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" + "os" +) + +func main() { + city := os.Args[1] + + switch city { + case "Paris", "Lyon": + fmt.Println("France") + case "Tokyo": + fmt.Println("Japan") + } + + // if city == "Paris" || city == "Lyon" { + // fmt.Println("France") + // } else if city == "Tokyo" { + // fmt.Println("Japan") + // } +} diff --git a/12-switch/05-bool-expressions/main.go b/12-switch/05-bool-expressions/main.go new file mode 100644 index 0000000..2bb170f --- /dev/null +++ b/12-switch/05-bool-expressions/main.go @@ -0,0 +1,23 @@ +// 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() { + i := 10 + + switch { + case i > 0: + fmt.Println("positive") + case i < 0: + fmt.Println("negative") + default: + fmt.Println("zero") + } +} diff --git a/12-switch/06-fallthrough/01-without/main.go b/12-switch/06-fallthrough/01-without/main.go new file mode 100644 index 0000000..1741509 --- /dev/null +++ b/12-switch/06-fallthrough/01-without/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" + +func main() { + i := 142 + + switch { + case i > 100: + fmt.Print("big positive number") + case i > 0: + fmt.Print("positive number") + default: + fmt.Print("number") + } + + fmt.Println() +} diff --git a/12-switch/06-fallthrough/02-with/main.go b/12-switch/06-fallthrough/02-with/main.go new file mode 100644 index 0000000..b51f4f8 --- /dev/null +++ b/12-switch/06-fallthrough/02-with/main.go @@ -0,0 +1,27 @@ +// 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() { + i := 142 + + switch { + case i > 100: + fmt.Print("big ") + fallthrough + case i > 0: + fmt.Print("positive ") + fallthrough + default: + fmt.Print("number") + } + + fmt.Println() +} diff --git a/12-switch/07-short-switch/main.go b/12-switch/07-short-switch/main.go new file mode 100644 index 0000000..93b6235 --- /dev/null +++ b/12-switch/07-short-switch/main.go @@ -0,0 +1,26 @@ +// 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() { + // i := 10 + + // true is in a comment now + // you can delete that part if you want + // it's by default true anyway. + switch i := 10; /* true */ { + case i > 0: + fmt.Println("positive") + case i < 0: + fmt.Println("negative") + default: + fmt.Println("zero") + } +} diff --git a/12-switch/08-parts-of-the-day/main.go b/12-switch/08-parts-of-the-day/main.go new file mode 100644 index 0000000..d73bcf7 --- /dev/null +++ b/12-switch/08-parts-of-the-day/main.go @@ -0,0 +1,36 @@ +// 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" + "time" +) + +func main() { + // time.Now() gets the current time + // and in turn, .Hour() gets the current hour from it + + // h := time.Now().Hour() + // fmt.Println("Current hour is", h) + + switch h := time.Now().Hour(); { + case h >= 18: // 18 to 23 + fmt.Println("good evening") + case h >= 12: // 12 to 18 + fmt.Println("good afternoon") + case h >= 6: // 6 to 12 + fmt.Println("good morning") + default: // 0 to 5 + fmt.Println("good night") + } + + // h is not available here + // it's bound to switch statement's block + // fmt.Println(h) +} diff --git a/12-switch/09-when-to-use/main.go b/12-switch/09-when-to-use/main.go new file mode 100644 index 0000000..842a6e6 --- /dev/null +++ b/12-switch/09-when-to-use/main.go @@ -0,0 +1,47 @@ +// 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" + "os" +) + +func main() { + if len(os.Args) != 2 { + fmt.Println("Gimme a month name.") + return + } + + // m := os.Args[1] + + // if m == "Dec" || m == "Jan" || m == "Feb" { + // fmt.Println("Winter") + // } else if m == "Mar" || m == "Apr" || m == "May" { + // fmt.Println("Spring") + // } else if m == "Jun" || m == "Jul" || m == "Aug" { + // fmt.Println("Summer") + // } else if m == "Sep" || m == "Oct" || m == "Nov" { + // fmt.Println("Fall") + // } else { + // fmt.Println("ERROR: not a month.") + // } + + switch m := os.Args[1]; m { + case "Dec", "Jan", "Feb": + fmt.Println("Winter") + case "Mar", "Apr", "May": + fmt.Println("Spring") + case "Jun", "Jul", "Aug": + fmt.Println("Summer") + case "Sep", "Oct", "Nov": + fmt.Println("Fall") + default: + fmt.Printf("%q is not a month.\n", m) + } +} diff --git a/12-switch/exercises/00/main.go b/12-switch/exercises/00/main.go new file mode 100644 index 0000000..1bace58 --- /dev/null +++ b/12-switch/exercises/00/main.go @@ -0,0 +1,22 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// ? +// +// NOTE +// ? +// +// EXPECTED OUTPUT +// ? +// --------------------------------------------------------- + +func main() { +} diff --git a/12-switch/exercises/00/solution/main.go b/12-switch/exercises/00/solution/main.go new file mode 100644 index 0000000..b5e729e --- /dev/null +++ b/12-switch/exercises/00/solution/main.go @@ -0,0 +1,11 @@ +// 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 + +func main() { +} diff --git a/12-switch/exercises/_challenge/01-challenge/main.go b/12-switch/exercises/_challenge/01-challenge/main.go new file mode 100644 index 0000000..bf0a76b --- /dev/null +++ b/12-switch/exercises/_challenge/01-challenge/main.go @@ -0,0 +1,75 @@ +// 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" + "os" +) + +// --------------------------------------------------------- +// CHALLENGE +// Convert the if statement into a switch statement. +// +// 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, 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] + + // refactor this into switch + 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/12-switch/exercises/_challenge/02-solution/main.go b/12-switch/exercises/_challenge/02-solution/main.go new file mode 100644 index 0000000..c09048c --- /dev/null +++ b/12-switch/exercises/_challenge/02-solution/main.go @@ -0,0 +1,44 @@ +// 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" + "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] + + switch { + case u != user && u != user2: + fmt.Printf(errUser, u) + case u == user && p == pass: + fallthrough + case u == user2 && p == pass2: + fmt.Printf(accessOK, u) + default: + fmt.Printf(errPwd, u) + } +} diff --git a/12-switch/questions/questions.md b/12-switch/questions/questions.md new file mode 100644 index 0000000..cac63ae --- /dev/null +++ b/12-switch/questions/questions.md @@ -0,0 +1,3 @@ +## ? +* text *CORRECT* +* text diff --git a/13-loops/01-loops/01-basics/main.go b/13-loops/01-loops/01-basics/main.go new file mode 100644 index 0000000..da5f716 --- /dev/null +++ b/13-loops/01-loops/01-basics/main.go @@ -0,0 +1,28 @@ +// 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() { + var sum int + + // sum += 1 + // sum += 2 + // sum += 3 + // sum += 4 + // sum += 5 + + for i := 1; i <= 1000; i++ { + sum += i + } + + fmt.Println(sum) +} diff --git a/13-loops/01-loops/02-break/main.go b/13-loops/01-loops/02-break/main.go new file mode 100644 index 0000000..bbf804c --- /dev/null +++ b/13-loops/01-loops/02-break/main.go @@ -0,0 +1,33 @@ +// 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() { + var ( + sum int + i = 1 + ) + + for { + if i > 5 { + break + } + + sum += i + + fmt.Println(i, "-->", sum) + + i++ + } + + fmt.Println(sum) +} diff --git a/13-loops/01-loops/03-continue/01-a-before/main.go b/13-loops/01-loops/03-continue/01-a-before/main.go new file mode 100644 index 0000000..2cde9d5 --- /dev/null +++ b/13-loops/01-loops/03-continue/01-a-before/main.go @@ -0,0 +1,39 @@ +// 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() { + var ( + sum int + i = 1 + ) + + for { + if i > 10 { + break + } + + if i%2 != 0 { + // this continue creates an endless loop + // since the code never increases the i + continue + } + + sum += i + + fmt.Println(i, "-->", sum) + + i++ + } + + fmt.Println(sum) +} diff --git a/13-loops/01-loops/03-continue/01-b-after/main.go b/13-loops/01-loops/03-continue/01-b-after/main.go new file mode 100644 index 0000000..bfc8124 --- /dev/null +++ b/13-loops/01-loops/03-continue/01-b-after/main.go @@ -0,0 +1,39 @@ +// 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() { + var ( + sum int + i = 1 + ) + + for { + if i > 10 { + break + } + + if i%2 != 0 { + // just by putting this here we solve the problem + i++ + continue + } + + sum += i + + fmt.Println(i, "-->", sum) + + i++ + } + + fmt.Println(sum) +} diff --git a/13-loops/01-loops/04-nested-loops/main.go b/13-loops/01-loops/04-nested-loops/main.go new file mode 100644 index 0000000..d3cb78f --- /dev/null +++ b/13-loops/01-loops/04-nested-loops/main.go @@ -0,0 +1,37 @@ +// 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" +) + +// EXERCISE: Get the `max` from the user +// And create the table according to that. + +const max = 5 + +func main() { + // print the header + fmt.Printf("%5s", "X") + for i := 0; i <= max; i++ { + fmt.Printf("%5d", i) + } + fmt.Println() + + for i := 0; i <= max; i++ { + // print the vertical header + fmt.Printf("%5d", i) + + // print the cells + for j := 0; j <= max; j++ { + fmt.Printf("%5d", i*j) + } + fmt.Println() + } +} diff --git a/13-loops/01-loops/05-for-range/01-loop-over-slices/main.go b/13-loops/01-loops/05-for-range/01-loop-over-slices/main.go new file mode 100644 index 0000000..f70d642 --- /dev/null +++ b/13-loops/01-loops/05-for-range/01-loop-over-slices/main.go @@ -0,0 +1,54 @@ +// 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" + "os" +) + +func main() { + // i := 1 + // fmt.Printf("%q\n", os.Args[1]) + // fmt.Printf("%q\n", os.Args[i]) + + // i++ + // fmt.Printf("%q\n", os.Args[2]) + // fmt.Printf("%q\n", os.Args[i]) + + // i++ + // fmt.Printf("%q\n", os.Args[3]) + // fmt.Printf("%q\n", os.Args[i]) + + // -------------------------------- + // #1st way: + // -------------------------------- + + // for i := 1; i < len(os.Args); i++ { + // fmt.Printf("%q\n", os.Args[i]) + // } + + // -------------------------------- + // #2nd way: + // -------------------------------- + + // for i, v := range os.Args { + // if i == 0 { + // continue + // } + // fmt.Printf("%q\n", v) + // } + + // -------------------------------- + // #3rd way (best): + // -------------------------------- + + for _, v := range os.Args[1:] { + fmt.Printf("%q\n", v) + } +} diff --git a/13-loops/01-loops/05-for-range/02-loop-over-words/main.go b/13-loops/01-loops/05-for-range/02-loop-over-words/main.go new file mode 100644 index 0000000..155f497 --- /dev/null +++ b/13-loops/01-loops/05-for-range/02-loop-over-words/main.go @@ -0,0 +1,51 @@ +// 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" + "strings" +) + +func main() { + // this will split the following string by spaces + // and then it will put it inside words variable + // as a slice of strings + words := strings.Fields( + "lazy cat jumps again and again and again", + ) + // 1 2 3 4 5 6 7 8 + + // -------------------------------- + // #1st way: + // -------------------------------- + // for j := 0; j < len(words); j++ { + // fmt.Printf("#%-2d: %q\n", j+1, words[j]) + // } + + // -------------------------------- + // #2nd way (best): + // -------------------------------- + for i, v := range words { + fmt.Printf("#%-2d: %q\n", i+1, v) + } + + // -------------------------------- + // #3rd way (reuse mechanism): + // -------------------------------- + // var ( + // i int + // v string + // ) + + // for i, v = range words { + // fmt.Printf("#%-2d: %q\n", i+1, v) + // } + + // fmt.Printf("Last value of i and v %d %q\n", i, v) +} diff --git a/13-loops/02-project-lucky-number-game/01-randomization/main.go b/13-loops/02-project-lucky-number-game/01-randomization/main.go new file mode 100644 index 0000000..2b39d3f --- /dev/null +++ b/13-loops/02-project-lucky-number-game/01-randomization/main.go @@ -0,0 +1,34 @@ +// 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/rand" + "time" +) + +func main() { + // rand.Seed(10) + // rand.Seed(100) + + // t := time.Now() + // rand.Seed(t.UnixNano()) + + // ^-- same: + + rand.Seed(time.Now().UnixNano()) + + guess := 10 + + for n := 0; n != guess; { + n = rand.Intn(guess + 1) + fmt.Printf("%d ", n) + } + fmt.Println() +} diff --git a/13-loops/02-project-lucky-number-game/02-game/main.go b/13-loops/02-project-lucky-number-game/02-game/main.go new file mode 100644 index 0000000..023f5ef --- /dev/null +++ b/13-loops/02-project-lucky-number-game/02-game/main.go @@ -0,0 +1,63 @@ +// 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/rand" + "os" + "strconv" + "time" +) + +const ( + maxTurns = 5 // less is more difficult + usage = `Welcome to the Lucky Number Game! + +The program will pick %d random numbers. +Your mission is to guess one of those numbers. + +The greater your number is, harder it gets. + +Wanna play? +` +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + args := os.Args[1:] + + if len(args) != 1 { + // fmt.Println("Pick a number.") + fmt.Printf(usage, maxTurns) + return + } + + guess, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Println("Not a number.") + return + } + + if guess < 0 { + fmt.Println("Please pick a positive number.") + return + } + + for turn := 1; turn <= maxTurns; turn++ { + n := rand.Intn(guess + 1) + + if n == guess { + fmt.Println("🎉 YOU WIN!") + return + } + } + + fmt.Println("☠️ YOU LOST... Try again?") +} diff --git a/13-loops/03-project-word-finder/main.go b/13-loops/03-project-word-finder/main.go new file mode 100644 index 0000000..1149ce4 --- /dev/null +++ b/13-loops/03-project-word-finder/main.go @@ -0,0 +1,38 @@ +// 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" + "os" + "strings" +) + +const corpus = "lazy cat jumps again and again and again" + +func main() { + words := strings.Fields(corpus) + query := os.Args[1:] + + // after the inner loop breaks + // this parent loop will look for the next queried word + for _, q := range query { + + // "break" will terminate this loop + for i, w := range words { + if q == w { + fmt.Printf("#%-2d: %q\n", i+1, w) + + // find the first word then break + // the nested loop + break + } + } + + } +} diff --git a/13-loops/04-labeled-statements/01-word-finder-break/main.go b/13-loops/04-labeled-statements/01-word-finder-break/main.go new file mode 100644 index 0000000..57a8b2d --- /dev/null +++ b/13-loops/04-labeled-statements/01-word-finder-break/main.go @@ -0,0 +1,40 @@ +// 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" + "os" + "strings" +) + +const corpus = "lazy cat jumps again and again and again" + +func main() { + words := strings.Fields(corpus) + query := os.Args[1:] + + // labels and other names do not share the same scope + // var queries string + // _ = queries + + // this label labels the parent loop below + // label's scope is the whole function body + // not only it's block +queries: + for _, q := range query { + for i, w := range words { + if q == w { + fmt.Printf("#%-2d: %q\n", i+1, w) + + // find the first word then quit + break queries + } + } + } +} diff --git a/13-loops/04-labeled-statements/02-word-finder-continue/main.go b/13-loops/04-labeled-statements/02-word-finder-continue/main.go new file mode 100644 index 0000000..73f8c88 --- /dev/null +++ b/13-loops/04-labeled-statements/02-word-finder-continue/main.go @@ -0,0 +1,33 @@ +// 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" + "os" + "strings" +) + +const corpus = "lazy cat jumps again and again and again" + +func main() { + words := strings.Fields(corpus) + query := os.Args[1:] + +queries: + for _, q := range query { + for i, w := range words { + if q == w { + fmt.Printf("#%-2d: %q\n", i+1, w) + + // skip duplicate words + continue queries + } + } + } +} diff --git a/13-loops/04-labeled-statements/03-word-finder-switch/main.go b/13-loops/04-labeled-statements/03-word-finder-switch/main.go new file mode 100644 index 0000000..19b2bcd --- /dev/null +++ b/13-loops/04-labeled-statements/03-word-finder-switch/main.go @@ -0,0 +1,48 @@ +// 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" + "os" + "strings" +) + +const corpus = "lazy cat jumps again and again and again" + +func main() { + words := strings.Fields(corpus) + query := os.Args[1:] + + // labels and other names do not share the same scope + // this will work even though `queries` label exists + // + // var queries string + // _ = queries + + // this label labels the parent loop below. + // label's scope is the whole func main() +queries: + for _, q := range query { + + search: + for i, w := range words { + switch q { + case "and", "or", "the": + break search + } + + if q == w { + fmt.Printf("#%-2d: %q\n", i+1, w) + + // find the first word then quit + continue queries + } + } + } +} diff --git a/13-loops/04-labeled-statements/04-goto/main.go b/13-loops/04-labeled-statements/04-goto/main.go new file mode 100644 index 0000000..77e0640 --- /dev/null +++ b/13-loops/04-labeled-statements/04-goto/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" +) + +func main() { + // cannot step over any variable declarations + // ERROR: "i" variable is declared after the jump + // + // goto loop + + var i int + +loop: + if i < 3 { + fmt.Println("looping") + i++ + goto loop + } + fmt.Println("done") +} diff --git a/13-loops/exercises/01-loops/01-sum-the-numbers/main.go b/13-loops/exercises/01-loops/01-sum-the-numbers/main.go new file mode 100644 index 0000000..82b0848 --- /dev/null +++ b/13-loops/exercises/01-loops/01-sum-the-numbers/main.go @@ -0,0 +1,21 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Sum the numbers +// +// 1. By using a loop, sum the numbers between 1 and 10. +// 2. Print the sum. +// +// EXPECTED OUTPUT +// Sum: 55 +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/01-loops/01-sum-the-numbers/solution/main.go b/13-loops/exercises/01-loops/01-sum-the-numbers/solution/main.go new file mode 100644 index 0000000..7861183 --- /dev/null +++ b/13-loops/exercises/01-loops/01-sum-the-numbers/solution/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() { + var sum int + for i := 1; i <= 10; i++ { + sum += i + } + fmt.Println("Sum:", sum) +} diff --git a/13-loops/exercises/01-loops/02-sum-the-numbers-verbose/main.go b/13-loops/exercises/01-loops/02-sum-the-numbers-verbose/main.go new file mode 100644 index 0000000..9df3100 --- /dev/null +++ b/13-loops/exercises/01-loops/02-sum-the-numbers-verbose/main.go @@ -0,0 +1,28 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Sum the numbers verbose edition +// +// By using a loop, sum the numbers between 1 and 10. +// +// HINT +// 1. For printing it as in the expected output, use Print +// and Printf functions. They don't print a newline +// automatically (unlike a Println). +// +// 2. Also, you need to use an if statement to prevent +// printing the last plus sign. +// +// EXPECTED OUTPUT +// 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55 +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/01-loops/02-sum-the-numbers-verbose/solution/main.go b/13-loops/exercises/01-loops/02-sum-the-numbers-verbose/solution/main.go new file mode 100644 index 0000000..487f458 --- /dev/null +++ b/13-loops/exercises/01-loops/02-sum-the-numbers-verbose/solution/main.go @@ -0,0 +1,27 @@ +// 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() { + const min, max = 1, 10 + + var sum int + for i := min; i <= max; i++ { + sum += i + + fmt.Print(i) + if i != max { + fmt.Print(" + ") + } + } + fmt.Printf(" = %d\n", sum) +} diff --git a/13-loops/exercises/01-loops/03-sum-up-to-n/main.go b/13-loops/exercises/01-loops/03-sum-up-to-n/main.go new file mode 100644 index 0000000..94eb21c --- /dev/null +++ b/13-loops/exercises/01-loops/03-sum-up-to-n/main.go @@ -0,0 +1,38 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Sum up to N +// +// 1. Get two numbers from the command-line: min and max +// 2. Convert them to integers (using Atoi) +// 3. By using a loop, sum the numbers between min and max +// +// RESTRICTIONS +// 1. Be sure to handle the errors. So, if a user doesn't +// pass enough arguments or she passes non-numerics, +// then warn the user and exit from the program. +// +// 2. Also, check that, min < max. +// +// HINT +// For converting the numbers, you can use `strconv.Atoi`. +// +// EXPECTED OUTPUT +// Let's suppose that the user runs it like this: +// +// go run main.go 1 10 +// +// Then it should print: +// +// 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55 +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/01-loops/03-sum-up-to-n/solution/main.go b/13-loops/exercises/01-loops/03-sum-up-to-n/solution/main.go new file mode 100644 index 0000000..187cc0a --- /dev/null +++ b/13-loops/exercises/01-loops/03-sum-up-to-n/solution/main.go @@ -0,0 +1,39 @@ +// 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" + "os" + "strconv" +) + +func main() { + if len(os.Args) < 3 { + fmt.Println("gimme two numbers") + return + } + + min, err1 := strconv.Atoi(os.Args[1]) + max, err2 := strconv.Atoi(os.Args[2]) + if err1 != nil || err2 != nil || min >= max { + fmt.Println("wrong numbers") + return + } + + var sum int + for i := min; i <= max; i++ { + sum += i + + fmt.Print(i) + if i != max { + fmt.Print(" + ") + } + } + fmt.Printf(" = %d\n", sum) +} diff --git a/13-loops/exercises/01-loops/04-only-evens/main.go b/13-loops/exercises/01-loops/04-only-evens/main.go new file mode 100644 index 0000000..88871e5 --- /dev/null +++ b/13-loops/exercises/01-loops/04-only-evens/main.go @@ -0,0 +1,30 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Only evens +// +// 1. Extend the "Sum up to N" exercise +// 2. Sum only the even numbers +// +// RESTRICTIONS +// Skip odd numbers using the `continue` statement +// +// EXPECTED OUTPUT +// Let's suppose that the user runs it like this: +// +// go run main.go 1 10 +// +// Then it should print: +// +// 2 + 4 + 6 + 8 + 10 = 30 +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/01-loops/04-only-evens/solution/main.go b/13-loops/exercises/01-loops/04-only-evens/solution/main.go new file mode 100644 index 0000000..eede18a --- /dev/null +++ b/13-loops/exercises/01-loops/04-only-evens/solution/main.go @@ -0,0 +1,42 @@ +// 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" + "os" + "strconv" +) + +func main() { + if len(os.Args) < 3 { + fmt.Println("gimme two numbers") + return + } + + min, err1 := strconv.Atoi(os.Args[1]) + max, err2 := strconv.Atoi(os.Args[2]) + if err1 != nil || err2 != nil || min >= max { + fmt.Println("wrong numbers") + return + } + + var sum int + for i := min; i <= max; i++ { + if i%2 != 0 { + continue + } + sum += i + + fmt.Print(i) + if i != max { + fmt.Print(" + ") + } + } + fmt.Printf(" = %d\n", sum) +} diff --git a/13-loops/exercises/01-loops/05-break-up/main.go b/13-loops/exercises/01-loops/05-break-up/main.go new file mode 100644 index 0000000..e3a9b06 --- /dev/null +++ b/13-loops/exercises/01-loops/05-break-up/main.go @@ -0,0 +1,30 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Break up +// +// 1. Extend the "Only Evens" exercise +// 2. This time, use an infinite loop. +// 3. Break the loop when it reaches to the `max`. +// +// RESTRICTIONS +// You should use the `break` statement once. +// +// HINT +// Do not forget incrementing the `i` before the `continue` +// statement and at the end of the loop. +// +// EXPECTED OUTPUT +// go run main.go 1 10 +// 2 + 4 + 6 + 8 + 10 = 30 +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/01-loops/05-break-up/solution/main.go b/13-loops/exercises/01-loops/05-break-up/solution/main.go new file mode 100644 index 0000000..61e656c --- /dev/null +++ b/13-loops/exercises/01-loops/05-break-up/solution/main.go @@ -0,0 +1,50 @@ +// 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" + "os" + "strconv" +) + +func main() { + if len(os.Args) < 3 { + fmt.Println("gimme two numbers") + return + } + + min, err1 := strconv.Atoi(os.Args[1]) + max, err2 := strconv.Atoi(os.Args[2]) + if err1 != nil || err2 != nil || min >= max { + fmt.Println("wrong numbers") + return + } + + var ( + i = min + sum int + ) + + for { + if i%2 != 0 { + i++ + continue + } + sum += i + + fmt.Print(i) + if i != max { + fmt.Print(" + ") + } else { + break + } + i++ + } + fmt.Printf(" = %d\n", sum) +} diff --git a/13-loops/exercises/01-loops/06-infinite-kill/main.go b/13-loops/exercises/01-loops/06-infinite-kill/main.go new file mode 100644 index 0000000..f65d6c8 --- /dev/null +++ b/13-loops/exercises/01-loops/06-infinite-kill/main.go @@ -0,0 +1,42 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Infinite Kill +// +// 1. Create an infinite loop +// 2. On each step of the loop print a random character. +// 3. And, sleep for 250 milliseconds. +// 4. Run the program and wait a couple of seconds +// then kill it using CTRL+C keys +// +// RESTRICTIONS +// 1. Print one of those characters randomly: \ / | +// 2. Before printing a character print also this +// escape sequence: \r +// +// Like this: "\r/", or this: "\r|", and so on... +// +// HINTS +// 1. Use `time.Sleep` to sleep. +// 2. You should pass a `time.Duration` value to it. +// 3. Check out the Go online documentation for the +// `Millisecond` constant. +// 4. When printing, do not use a newline! Or a Println! +// Use Print or Printf instead. +// +// NOTE +// If this exercise is hard for you now, wait until the +// lucky number lecture. Even then so, then just skip it. +// +// You can return back to it afterwards. +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/01-loops/06-infinite-kill/solution/main.go b/13-loops/exercises/01-loops/06-infinite-kill/solution/main.go new file mode 100644 index 0000000..8a48502 --- /dev/null +++ b/13-loops/exercises/01-loops/06-infinite-kill/solution/main.go @@ -0,0 +1,31 @@ +// 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/rand" + "time" +) + +func main() { + for { + var c string + + switch rand.Intn(3) { + case 0: + c = "\\" + case 1: + c = "/" + case 2: + c = "|" + } + fmt.Printf("\r%s", c) + time.Sleep(time.Millisecond * 150) + } +} diff --git a/13-loops/exercises/01-loops/07-crunch-the-primes/main.go b/13-loops/exercises/01-loops/07-crunch-the-primes/main.go new file mode 100644 index 0000000..03f1cda --- /dev/null +++ b/13-loops/exercises/01-loops/07-crunch-the-primes/main.go @@ -0,0 +1,33 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Crunch the primes +// +// 1. Get numbers from the command-line. +// 2. `for range` over them. +// 4. Convert each one to an int. +// 5. If one of the numbers isn't an int, just skip it. +// 6. Print the ones that are only the prime numbers. +// +// RESTRICTION +// The user can run the program with any number of +// arguments. +// +// HINT +// Find whether a number is prime using this algorithm: +// https://stackoverflow.com/a/1801446/115363 +// +// EXPECTED OUTPUT +// go run main.go 2 4 6 7 a 9 c 11 x 12 13 +// 2 7 11 13 +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/01-loops/07-crunch-the-primes/solution/main.go b/13-loops/exercises/01-loops/07-crunch-the-primes/solution/main.go new file mode 100644 index 0000000..ffa7292 --- /dev/null +++ b/13-loops/exercises/01-loops/07-crunch-the-primes/solution/main.go @@ -0,0 +1,54 @@ +// 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" + "os" + "strconv" +) + +// +func main() { + // remember [1:] skips the first argument + +main: + for _, arg := range os.Args[1:] { + n, err := strconv.Atoi(arg) + if err != nil { + // skip non-numerics + continue + } + + switch { + // not a prime + case n <= 0 || n%2 == 0 || n%3 == 0: + continue + + // prime + case n == 2 || n == 3: + fmt.Print(n, " ") + continue + } + + for i, w := 5, 2; i*i <= n; { + // not a prime + if n%i == 0 { + continue main + } + + i += w + w = 6 - w + } + + // all checks ok: it's a prime + fmt.Print(n, " ") + } + + fmt.Println() +} diff --git a/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/main.go b/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/main.go new file mode 100644 index 0000000..ee6a1f4 --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/main.go @@ -0,0 +1,28 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: First Turn Winner +// +// If the player wins on the first turn, then display +// a special bonus message. +// +// RESTRICTION +// The first picked random number by the computer should +// match the player's guess. +// +// EXAMPLE SCENARIO +// 1. The player guesses 6 +// 2. The computer picks a random number and it happens +// to be 6 +// 3. Terminate the game and print the bonus message +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/solution/main.go new file mode 100644 index 0000000..1e582ee --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/01-first-turn-winner/solution/main.go @@ -0,0 +1,66 @@ +// 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/rand" + "os" + "strconv" + "time" +) + +const ( + maxTurns = 5 // less is more difficult + usage = `Welcome to the Lucky Number Game! + +The program will pick %d random numbers. +Your mission is to guess one of those numbers. + +The greater your number is, harder it gets. + +Wanna play? +` +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + args := os.Args[1:] + + if len(args) != 1 { + fmt.Printf(usage, maxTurns) + return + } + + guess, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Println("Not a number.") + return + } + + if guess < 0 { + fmt.Println("Please pick a positive number.") + return + } + + for turn := 1; turn <= maxTurns; turn++ { + n := rand.Intn(guess + 1) + + if n == guess { + if turn == 1 { + fmt.Println("🥇 FIRST TIME WINNER!!!") + } else { + fmt.Println("🎉 YOU WON!") + } + return + } + } + + fmt.Println("☠️ YOU LOST... Try again?") +} diff --git a/13-loops/exercises/02-lucky-number-exercises/02-random-messages/main.go b/13-loops/exercises/02-lucky-number-exercises/02-random-messages/main.go new file mode 100644 index 0000000..c888e48 --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/02-random-messages/main.go @@ -0,0 +1,37 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Random Messages +// +// Display a few different won and lost messages "randomly". +// +// HINTS +// 1. You can use a switch statement to do that. +// 2. Set its condition to the random number generator. +// 3. I would use a short switch. +// +// EXAMPLES +// Player wins: then randomly printone of these: +// +// go run main.go 5 +// YOU WON +// go run main.go 5 +// YOU'RE AWESOME +// +// Player loses: then randomly printone of these: +// +// go run main.go 5 +// LOSER! +// go run main.go 5 +// YOU LOST. TRY AGAIN? +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/02-lucky-number-exercises/02-random-messages/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/02-random-messages/solution/main.go new file mode 100644 index 0000000..1d5f6bc --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/02-random-messages/solution/main.go @@ -0,0 +1,77 @@ +// 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/rand" + "os" + "strconv" + "time" +) + +const ( + maxTurns = 5 // less is more difficult + usage = `Welcome to the Lucky Number Game! + +The program will pick %d random numbers. +Your mission is to guess one of those numbers. + +The greater your number is, harder it gets. + +Wanna play? +` +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + args := os.Args[1:] + + if len(args) != 1 { + fmt.Printf(usage, maxTurns) + return + } + + guess, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Println("Not a number.") + return + } + + if guess < 0 { + fmt.Println("Please pick a positive number.") + return + } + + for turn := 1; turn <= maxTurns; turn++ { + n := rand.Intn(guess + 1) + + if n == guess { + switch rand.Intn(3) { + case 0: + fmt.Println("🎉 YOU WIN!") + case 1: + fmt.Println("🎉 YOU'RE AWESOME!") + case 2: + fmt.Println("🎉 PERFECT!") + } + return + } + } + + msg := "%s Try again?\n" + + switch rand.Intn(2) { + case 0: + fmt.Printf(msg, "☠️ YOU LOST...") + case 1: + fmt.Printf(msg, "☠️ JUST A BAD LUCK...") + } + +} diff --git a/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/main.go b/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/main.go new file mode 100644 index 0000000..9966a0c --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/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 + +// --------------------------------------------------------- +// EXERCISE: Double Guesses +// +// Let the player guess two numbers instead of one. +// +// HINT: +// Generate random numbers using the greatest number +// among the guessed numbers. +// +// EXAMPLES +// go run main.go 5 6 +// Player wins if the random number is either 5 or 6. +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/solution/main.go new file mode 100644 index 0000000..6d1e4a5 --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/03-double-guesses/solution/main.go @@ -0,0 +1,76 @@ +// 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/rand" + "os" + "strconv" + "time" +) + +const ( + maxTurns = 5 // less is more difficult + usage = `Welcome to the Lucky Number Game! + +The program will pick %d random numbers. +Your mission is to guess one of those numbers. + +The greater your number is, harder it gets. + +Wanna play? +` +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + args := os.Args[1:] + + if len(args) < 1 { + fmt.Printf(usage, maxTurns) + return + } + + guess, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Println("Not a number.") + return + } + + var guess2 int + if len(args) == 2 { + guess2, err = strconv.Atoi(args[1]) + if err != nil { + fmt.Println("Not a number.") + return + } + } + + if guess < 0 || guess2 < 0 { + fmt.Println("Please pick positive numbers.") + return + } + + min := guess + if guess < guess2 { + min = guess2 + } + + for turn := 1; turn <= maxTurns; turn++ { + n := rand.Intn(min + 1) + + if n == guess || n == guess2 { + fmt.Println("🎉 YOU WIN!") + return + } + } + + fmt.Println("☠️ YOU LOST... Try again?") +} diff --git a/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/main.go b/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/main.go new file mode 100644 index 0000000..6f06f72 --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/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 + +// --------------------------------------------------------- +// EXERCISE: Verbose Mode +// +// When the player runs the game like this: +// +// go run main.go -v 4 +// +// Display each generated random number: + +// 1 3 4 🎉 YOU WIN! +// +// In this example, computer picks 1, 3, and 4. And the +// player wins. +// +// HINT +// You need to get and interpret the command-line arguments. +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/solution/main.go new file mode 100644 index 0000000..0931330 --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/04-verbose-mode/solution/main.go @@ -0,0 +1,78 @@ +// 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/rand" + "os" + "strconv" + "time" +) + +const ( + maxTurns = 5 // less is more difficult + usage = `Welcome to the Lucky Number Game! + +The program will pick %d random numbers. +Your mission is to guess one of those numbers. + +The greater your number is, harder it gets. + +Wanna play? + +(Provide -v flag to see the picked numbers.) +` +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + args := os.Args[1:] + + if len(args) < 1 { + fmt.Printf(usage, maxTurns) + return + } + + var verbose bool + + if args[0] == "-v" { + verbose = true + } + + guess, err := strconv.Atoi(args[len(args)-1]) + if err != nil { + fmt.Println("Not a number.") + return + } + + if guess < 0 { + fmt.Println("Please pick a positive number.") + return + } + + for turn := 1; turn <= maxTurns; turn++ { + n := rand.Intn(guess + 1) + + if verbose { + fmt.Printf("%d ", n) + } + + if n == guess { + if turn == 1 { + fmt.Println("🥇 FIRST TIME WINNER!!!") + } else { + fmt.Println("🎉 YOU WON!") + } + return + } + } + + fmt.Println("☠️ YOU LOST... Try again?") +} diff --git a/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/main.go b/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/main.go new file mode 100644 index 0000000..45431c2 --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/main.go @@ -0,0 +1,48 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Enough Picks +// +// If the player's guess number is below 10; +// then make sure that the computer generates a random +// number between 0 and 10. +// +// However, if the guess number is above 10; then the +// computer should generate the numbers +// between 0 and the guess number. +// +// WHY? +// This way the game will be harder. +// +// Because, in the current version of the game, if +// the player's guess number is for example 3; then the +// computer picks a random number between 0 and 3. +// +// So, currently a player can easily win the game. +// +// EXAMPLE +// Suppose that the player runs the game like this: +// go run main.go 9 +// +// Or like this: +// go run main.go 2 +// +// Then the computer should pick a random number +// between 0-10. +// +// Or, if the player runs it like this: +// go run main.go 15 +// +// Then the computer should pick a random number +// between 0-15. +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/solution/main.go new file mode 100644 index 0000000..047e3fb --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/05-enough-picks/solution/main.go @@ -0,0 +1,68 @@ +// 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/rand" + "os" + "strconv" + "time" +) + +const ( + maxTurns = 5 // less is more difficult + usage = `Welcome to the Lucky Number Game! + +The program will pick %d random numbers. +Your mission is to guess one of those numbers. + +The greater your number is, harder it gets. + +Wanna play? +` +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + args := os.Args[1:] + + if len(args) < 1 { + fmt.Printf(usage, maxTurns) + return + } + + guess, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Println("Not a number.") + return + } + + if guess < 0 { + fmt.Println("Please pick a positive number.") + return + } + + min := 10 + if guess > min { + min = guess + } + + for turn := 1; turn <= maxTurns; turn++ { + n := rand.Intn(min + 1) + fmt.Println(n) + + if n == guess { + fmt.Println("🎉 YOU WIN!") + return + } + } + + fmt.Println("☠️ YOU LOST... Try again?") +} diff --git a/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/main.go b/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/main.go new file mode 100644 index 0000000..6cc71f4 --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/main.go @@ -0,0 +1,51 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Dynamic Difficulty +// +// Current game picks only 5 numbers (5 turns). +// +// Make sure that the game adjust its own difficulty +// depending on the guess number. +// +// RESTRICTION +// Do not make the game to easy. Only adjust the +// difficulty if the guess is above 10. +// +// EXPECTED OUTPUT +// Suppose that the player runs the game like this: +// go run main.go 5 +// +// Then the computer should pick 5 random numbers. +// +// Or, if the player runs it like this: +// go run main.go 25 +// +// Then the computer may pick 11 random numbers +// instead. +// +// Or, if the player runs it like this: +// go run main.go 100 +// +// Then the computer may pick 30 random numbers +// instead. +// +// As you can see, greater guess number causes the +// game to increase the game turns, which in turn +// adjust the game's difficulty dynamically. +// +// Because, greater guess number makes it harder to win. +// But, this way, game's difficulty will be dynamic. +// It will adjust its own difficulty depending on the +// guess number. +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/solution/main.go b/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/solution/main.go new file mode 100644 index 0000000..0838b18 --- /dev/null +++ b/13-loops/exercises/02-lucky-number-exercises/06-dynamic-difficulty/solution/main.go @@ -0,0 +1,62 @@ +// 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/rand" + "os" + "strconv" + "time" +) + +const ( + maxTurns = 5 // less is more difficult + usage = `Welcome to the Lucky Number Game! + +The program will pick %d random numbers. +Your mission is to guess one of those numbers. + +The greater your number is, harder it gets. + +Wanna play? +` +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + args := os.Args[1:] + + if len(args) != 1 { + fmt.Printf(usage, maxTurns) + return + } + + guess, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Println("Not a number.") + return + } + + if guess < 0 { + fmt.Println("Please pick a positive number.") + return + } + + for turn := (maxTurns + guess/4); turn > 0; turn-- { + n := rand.Intn(guess + 1) + + if n == guess { + fmt.Println("🎉 YOU WIN!") + return + } + } + + fmt.Println("☠️ YOU LOST... Try again?") +} diff --git a/13-loops/exercises/03-word-finder-exercises/01-case-insensitive/main.go b/13-loops/exercises/03-word-finder-exercises/01-case-insensitive/main.go new file mode 100644 index 0000000..f1beff2 --- /dev/null +++ b/13-loops/exercises/03-word-finder-exercises/01-case-insensitive/main.go @@ -0,0 +1,27 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Case Insentive Search +// +// Allow for case-insensitive searching +// +// EXAMPLE +// Let's say that the user runs the program like this: +// go run main.go LAZY +// +// Or like this: go run main.go lAzY +// Or like this: go run main.go lazy +// +// For all cases above, the program should find +// the "lazy" keyword. +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/03-word-finder-exercises/01-case-insensitive/solution/main.go b/13-loops/exercises/03-word-finder-exercises/01-case-insensitive/solution/main.go new file mode 100644 index 0000000..3567152 --- /dev/null +++ b/13-loops/exercises/03-word-finder-exercises/01-case-insensitive/solution/main.go @@ -0,0 +1,40 @@ +// 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" + "os" + "strings" +) + +const corpus = "lazy cat jumps again and again and again" + +func main() { + words := strings.Fields(corpus) + query := os.Args[1:] + +queries: + for _, q := range query { + // case insensitive search + q = strings.ToLower(q) + + search: + for i, w := range words { + switch q { + case "and", "or", "the": + break search + } + + if q == w { + fmt.Printf("#%-2d: %q\n", i+1, w) + continue queries + } + } + } +} diff --git a/13-loops/exercises/03-word-finder-exercises/02-path-searcher/main.go b/13-loops/exercises/03-word-finder-exercises/02-path-searcher/main.go new file mode 100644 index 0000000..9ff4e1f --- /dev/null +++ b/13-loops/exercises/03-word-finder-exercises/02-path-searcher/main.go @@ -0,0 +1,70 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE: Path Searcher +// +// Your program should search inside the path environment +// variable. +// +// Remove the corpus constant then get the corpus from the +// environment variable "Path" or "PATH" which +// constains paths to the executable programs on your +// operating system. +// +// HINTS +// 1. Search the web for what is an environment +// variable and how to use it, if you don't know +// what it is. +// +// 2. Look up for the necessary function for getting +// an environment variable. It's in the "os" package. +// +// Search for it on the Go online documentation. +// +// 3. Look up for the necessary function for splitting +// the path variable into directories. It's in +// the "strings" package. +// +// EXAMPLE +// For example, on my Mac, my PATH environment variable +// looks like this: +// +// "/usr/local/bin:/sbin:/Users/inanc/go/bin" +// +// So, if the user runs the program like this: +// +// go run main.go /sbin +// +// It should print this: +// +// #2 : "/sbin" +// --------------------------------------------------------- + +// --------------------------------------------------------- +// BONUS EXERCISE +// Make your program cross platform. So, it can search +// the path environment variable when you run it on +// a Windows or on a Mac (OS X) or on a Linux. +// +// HINT +// 1. What you're looking for is the runtime.GOOS constant. +// 2. Get the operating system name using GOOS. +// 3. Adjust the path environment variable name and +// the directory separator accordingly. +// +// FOR EXAMPLE: On OS X, path environment variable's name +// is PATH and the separator is a colon `:`. +// +// Or, on Windows, its name is Path and the separator is +// a semicolon `;`. +// --------------------------------------------------------- + +func main() { +} diff --git a/13-loops/exercises/03-word-finder-exercises/02-path-searcher/solution/main.go b/13-loops/exercises/03-word-finder-exercises/02-path-searcher/solution/main.go new file mode 100644 index 0000000..00a34bf --- /dev/null +++ b/13-loops/exercises/03-word-finder-exercises/02-path-searcher/solution/main.go @@ -0,0 +1,36 @@ +// 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" + "os" + "strings" +) + +func main() { + // get and split the PATH environment variable + // this only works for the unix-based systems + words := strings.Split(os.Getenv("PATH"), ":") + query := os.Args[1:] +queries: + for _, q := range query { + search: + for i, w := range words { + switch q { + case "and", "or", "the": + break search + } + + if q == w { + fmt.Printf("#%-2d: %q\n", i+1, w) + continue queries + } + } + } +} diff --git a/13-loops/exercises/more-exercises.md b/13-loops/exercises/more-exercises.md new file mode 100644 index 0000000..f2ba408 --- /dev/null +++ b/13-loops/exercises/more-exercises.md @@ -0,0 +1,2 @@ +**You can find more exercises here:** +* https://www.rosettacode.org/wiki/Category:Iteration \ No newline at end of file diff --git a/13-loops/questions/questions.md b/13-loops/questions/questions.md new file mode 100644 index 0000000..cac63ae --- /dev/null +++ b/13-loops/questions/questions.md @@ -0,0 +1,3 @@ +## ? +* text *CORRECT* +* text diff --git a/14-arrays/01-what/01-without-arrays/main.go b/14-arrays/01-what/01-without-arrays/main.go new file mode 100644 index 0000000..971b653 --- /dev/null +++ b/14-arrays/01-what/01-without-arrays/main.go @@ -0,0 +1,28 @@ +// 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() { + jake := "jake" + joe := "joe" + lee := "lee" + lina := "lina" + + fmt.Println(jake) + fmt.Println(joe) + fmt.Println(lee) + fmt.Println(lina) + + // for each name + // you need to declare a variable + // and then you need to print it + // + // but by using an array, you don't need to do that +} diff --git a/14-arrays/01-what/02-with-arrays/main.go b/14-arrays/01-what/02-with-arrays/main.go new file mode 100644 index 0000000..cc79c94 --- /dev/null +++ b/14-arrays/01-what/02-with-arrays/main.go @@ -0,0 +1,27 @@ +// 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() { + // instead of storing the names in variables, + // you can use an array to store all of the names + // in a single variable + + // names := [3]string{"jake", "joe", "lee"} + names := [4]string{"jake", "joe", "lee", "lina"} + + // doing so allows you to loop over the names + // and print each one of them + // + // you can't do this without arrays + for _, name := range names { + fmt.Println(name) + } +} diff --git a/14-arrays/02-array-literals/01/main.go b/14-arrays/02-array-literals/01/main.go new file mode 100644 index 0000000..e613271 --- /dev/null +++ b/14-arrays/02-array-literals/01/main.go @@ -0,0 +1,18 @@ +// 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() { + ages := [3]int{15, 30, 25} + + for _, age := range ages { + fmt.Println(age) + } +} diff --git a/14-arrays/02-array-literals/02-fixed-size/main.go b/14-arrays/02-array-literals/02-fixed-size/main.go new file mode 100644 index 0000000..6187189 --- /dev/null +++ b/14-arrays/02-array-literals/02-fixed-size/main.go @@ -0,0 +1,18 @@ +// 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 + +func main() { + // uncomment the code below to observe the error + + // ages := [3]int{15, 30, 25, 44} + + // for _, age := range ages { + // fmt.Println(age) + // } +} diff --git a/14-arrays/02-array-literals/03-constant-expressions/main.go b/14-arrays/02-array-literals/03-constant-expressions/main.go new file mode 100644 index 0000000..520de38 --- /dev/null +++ b/14-arrays/02-array-literals/03-constant-expressions/main.go @@ -0,0 +1,39 @@ +// 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() { + // you cannot use variables + // when setting the length of an array + + // length := 3 + + // but, you can use constants and constant expressions + const length = 3 * 2 + + ages := [3 * 2]int{15, 30, 25} + + for _, age := range ages { + fmt.Println(age) + } +} + +// EXERCISE: +// Try to put the `length` constant +// in place of `3 * 2` above. + +// EXERCISE: +// Try to put the `length` variable +// in place of `3 * 2` above. +// +// And observe the error. +// +// To do that, you need comment-out +// the length constant first. diff --git a/14-arrays/02-array-literals/04-element-type/main.go b/14-arrays/02-array-literals/04-element-type/main.go new file mode 100644 index 0000000..3b008ba --- /dev/null +++ b/14-arrays/02-array-literals/04-element-type/main.go @@ -0,0 +1,51 @@ +// 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 + +func main() { + // ----- + // you cannot use incompatible values + // than the array's element type + // + // uncomment the code parts below one by one + // then observe the errors + // ----- + + // for example you can't use a string + // ages := [3]int{15, 30, "hi"} + + // ----- + + // or a float64, etc... + // ages := [3]int{15, 30, 5.5} + + // ----- + + // of course this is valid, since it's untyped + // 5. becomes 5 (int) + // ages := [3]int{15, 30, 5.} + + // ----- + + // for _, age := range ages { + // fmt.Println(age) + // } +} + +// EXERCISE: +// Try to put the `length` constant +// in place of `3 * 2` above. + +// EXERCISE: +// Try to put the `length` variable +// in place of `3 * 2` above. +// +// And observe the error. +// +// To do that, you need comment-out +// the length constant first. diff --git a/14-arrays/02-array-literals/05-ellipsis/main.go b/14-arrays/02-array-literals/05-ellipsis/main.go new file mode 100644 index 0000000..c12af56 --- /dev/null +++ b/14-arrays/02-array-literals/05-ellipsis/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() { + ages := [...]int{15, 30, 25} + // equals to: + // ages := [3]int{15, 30, 25} + + for _, age := range ages { + fmt.Println(age) + } +} diff --git a/14-arrays/03-array-type/main.go b/14-arrays/03-array-type/main.go new file mode 100644 index 0000000..06ec9ff --- /dev/null +++ b/14-arrays/03-array-type/main.go @@ -0,0 +1,22 @@ +// 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() { + ages := [3]int{15, 30, 25} + + fmt.Printf("%T\n", ages) // [3]int + fmt.Printf("%T\n", [...]int{15, 30, 25}) // [3]int + + fmt.Printf("%T\n", [2]int{15, 30}) // [2]int + + fmt.Printf("%T\n", [1]string{"hi"}) // [1]string + fmt.Printf("%T\n", [...]float64{3.14, 6.28}) // [2]float64 +} diff --git a/14-arrays/04-zero-values/01-uninitialized-array/main.go b/14-arrays/04-zero-values/01-uninitialized-array/main.go new file mode 100644 index 0000000..6d3fabb --- /dev/null +++ b/14-arrays/04-zero-values/01-uninitialized-array/main.go @@ -0,0 +1,16 @@ +// 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() { + var ages [3]int + + fmt.Println(ages) +} diff --git a/14-arrays/04-zero-values/02-partially-initialized-array/main.go b/14-arrays/04-zero-values/02-partially-initialized-array/main.go new file mode 100644 index 0000000..e35abbe --- /dev/null +++ b/14-arrays/04-zero-values/02-partially-initialized-array/main.go @@ -0,0 +1,16 @@ +// 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() { + ages := [3]int{1} + + fmt.Println(ages) +} diff --git a/14-arrays/04-zero-values/03-ellipsis/main.go b/14-arrays/04-zero-values/03-ellipsis/main.go new file mode 100644 index 0000000..1a1a500 --- /dev/null +++ b/14-arrays/04-zero-values/03-ellipsis/main.go @@ -0,0 +1,18 @@ +// 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() { + names := [...]string{ + "lina", "bob", "jane", + } + + fmt.Printf("%#v\n", names) +} diff --git a/14-arrays/04-zero-values/04-without-ellipsis/main.go b/14-arrays/04-zero-values/04-without-ellipsis/main.go new file mode 100644 index 0000000..2b62719 --- /dev/null +++ b/14-arrays/04-zero-values/04-without-ellipsis/main.go @@ -0,0 +1,18 @@ +// 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() { + names := [5]string{ + "lina", "bob", "jane", + } + + fmt.Printf("%#v\n", names) +} diff --git a/14-arrays/04-zero-values/05-more-examples/main.go b/14-arrays/04-zero-values/05-more-examples/main.go new file mode 100644 index 0000000..11a0ce3 --- /dev/null +++ b/14-arrays/04-zero-values/05-more-examples/main.go @@ -0,0 +1,21 @@ +// 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() { + names := [5]string{ + "lina", "bob", "jane", + } + + fmt.Printf("%#v\n", names) + + temperatures := [10]float64{1.5, 2.5} + fmt.Printf("%#v\n", temperatures) +} diff --git a/14-arrays/05-array-operations/01-getting-length/main.go b/14-arrays/05-array-operations/01-getting-length/main.go new file mode 100644 index 0000000..94e87ce --- /dev/null +++ b/14-arrays/05-array-operations/01-getting-length/main.go @@ -0,0 +1,35 @@ +// 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() { + ages := [2]int{15, 20} + + // i can directly use `len` + fmt.Println(len(ages)) // 2 + + // i can also assign its result to a variable + l := len(ages) + fmt.Println(l) // 2 + + // let's print the length of a few arrays + l = len([1]bool{true}) // 1 + fmt.Println(l) + + l = len([3]string{"lina", "james", "joe"}) // 3 + fmt.Println(l) + + // this array doesn't initialize any elements + // but its length is still 5 + // whether the elements are initialized or not + l = len([5]int{}) + fmt.Println(l) // 5 + fmt.Println([5]int{}) +} diff --git a/14-arrays/05-array-operations/02-getting-elements/main.go b/14-arrays/05-array-operations/02-getting-elements/main.go new file mode 100644 index 0000000..84673b2 --- /dev/null +++ b/14-arrays/05-array-operations/02-getting-elements/main.go @@ -0,0 +1,32 @@ +// 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() { + ages := [2]int{15, 20} + + // WRONG (try it): + // fmt.Println(ages[-1]) + + fmt.Println("1st item:", ages[0]) + fmt.Println("2nd item:", ages[1]) + + // fmt.Println("2nd item:", ages[2]) + + // fmt.Println(ages[len(ages)]) + // here, `len(ages) - 1` equals to 1 + fmt.Println("last item:", ages[len(ages)-1]) + + // let's display the indexes and the items + // of the array + for i, v := range ages { + fmt.Printf("index: %d, value: %d\n", i, v) + } +} diff --git a/14-arrays/05-array-operations/03-getting-elements/main.go b/14-arrays/05-array-operations/03-getting-elements/main.go new file mode 100644 index 0000000..f3a69c1 --- /dev/null +++ b/14-arrays/05-array-operations/03-getting-elements/main.go @@ -0,0 +1,49 @@ +// 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() { + ages := [2]int{15, 20} + + ages[0] = 6 + ages[1] *= 3 + + fmt.Println(ages) + + // increase the last element by 1 + ages[1]++ + ages[len(ages)-1]++ + + fmt.Println(ages) + + // v is a copy + for _, v := range ages { + // it's like: + // v = ages[0] + // v++ + + // and: + // v = ages[1] + // v++ + + v++ + } + + fmt.Println(ages) + + // you don't need to use blank-identifier for the value + // for i, _ := range ages { + + for i := range ages { + ages[i]++ + } + + fmt.Println(ages) +} diff --git a/14-arrays/06-comparing-arrays/01-equal-arrays/main.go b/14-arrays/06-comparing-arrays/01-equal-arrays/main.go new file mode 100644 index 0000000..440d2fe --- /dev/null +++ b/14-arrays/06-comparing-arrays/01-equal-arrays/main.go @@ -0,0 +1,30 @@ +// 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() { + a := [3]int{6, 9, 3} + b := [3]int{6, 9, 3} + + if a == b { + fmt.Println("they're equal!") + + // they're comparable + // since their types are identical + fmt.Printf("1st array's type is %T\n", a) + fmt.Printf("2nd array's type is %T\n", b) + + // go compares arrays like this + // it compares the elements one by one + fmt.Println(a[0] == b[0]) + fmt.Println(a[1] == b[1]) + fmt.Println(a[2] == b[2]) + } +} diff --git a/14-arrays/06-comparing-arrays/02-not-equal-arrays/main.go b/14-arrays/06-comparing-arrays/02-not-equal-arrays/main.go new file mode 100644 index 0000000..d88a685 --- /dev/null +++ b/14-arrays/06-comparing-arrays/02-not-equal-arrays/main.go @@ -0,0 +1,39 @@ +// 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() { + a := [3]int{6, 9, 3} + b := [3]int{3, 9, 6} + + if a == b { + fmt.Println("they're equal!") + } else { + fmt.Println("they're not equal! a==b?", a == b) + + // but, they're comparable + // since their types are identical + fmt.Printf("1st array's type is %T\n", a) + fmt.Printf("2nd array's type is %T\n", b) + + // go compares arrays like this + // it compares the elements one by one + fmt.Println(a[0] == b[0]) // false + fmt.Println(a[1] == b[1]) // true + fmt.Println(a[2] == b[2]) // false + + // actually Go will quit comparing these arrays + // once it sees unequal items + // + // so, it will stop the comparison (short-circuit) + // when it sees the first item is false + // and it will return false + } +} diff --git a/14-arrays/06-comparing-arrays/03-comparability/main.go b/14-arrays/06-comparing-arrays/03-comparability/main.go new file mode 100644 index 0000000..b243337 --- /dev/null +++ b/14-arrays/06-comparing-arrays/03-comparability/main.go @@ -0,0 +1,24 @@ +// 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 + +func main() { + // these two arrays not comparable + // their types are different + + // uncomment all the code below and observe the error + + // a := [3]int{6, 9, 3} + // b := [2]int{6, 9} + + // you can't ask this question + + // if a == b { + // fmt.Println("they're equal!") + // } +} diff --git a/14-arrays/06-comparing-arrays/04-example/main.go b/14-arrays/06-comparing-arrays/04-example/main.go new file mode 100644 index 0000000..dd197a9 --- /dev/null +++ b/14-arrays/06-comparing-arrays/04-example/main.go @@ -0,0 +1,26 @@ +// 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() { + a := [2]string{"jane", "lina"} + b := [2]string{"jane", "lina"} + c := [2]string{"mike", "joe"} + + fmt.Println("a==b?", a == b) // equal + fmt.Println("a==c?", a == c) // not equal + + // cannot compare + d := [3]string{"jane", "lina"} + // fmt.Println("c==d?", c == d) + + fmt.Printf("type of c is %T\n", c) + fmt.Printf("type of d is %T\n", d) +} diff --git a/14-arrays/07-unnamed-vs-named-types/main.go b/14-arrays/07-unnamed-vs-named-types/main.go new file mode 100644 index 0000000..a030fe1 --- /dev/null +++ b/14-arrays/07-unnamed-vs-named-types/main.go @@ -0,0 +1,38 @@ +// 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() { + type ( + A [3]int + B [3]int + ) + + x := A{1, 2, 3} + y := B{1, 2, 3} + z := [3]int{1, 2, 3} + + fmt.Printf("x's type: %T\n", x) // named type : main.A + fmt.Printf("y's type: %T\n", y) // named type : main.B + fmt.Printf("z's type: %T\n", z) // unnamed type: [3]int + + // cannot compare different named types + // fmt.Println("x==y?", x == y) + + // can convert between identical types + fmt.Println("x==y?", x == A(y)) + fmt.Println("x==y?", B(x) == y) + + fmt.Printf("x's type : %T\n", x) + fmt.Printf("A(y)'s type: %T\n", A(y)) + + // can compare to unnamed types + fmt.Println("x==z?", x == z) +} diff --git a/14-arrays/08-assigning-arrays/01-assigning-arrays/main.go b/14-arrays/08-assigning-arrays/01-assigning-arrays/main.go new file mode 100644 index 0000000..b28c83b --- /dev/null +++ b/14-arrays/08-assigning-arrays/01-assigning-arrays/main.go @@ -0,0 +1,24 @@ +// 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() { + a := [3]int{5, 4, 7} + + // this assignment will create a new array value + // and then it will be stored in the b variable + b := a + + fmt.Println("a =>", a) + fmt.Println("b =>", b) + + // they're equal, they're clones + fmt.Println("a==b?", a == b) +} diff --git a/14-arrays/08-assigning-arrays/02-arrays-are-value-types/main.go b/14-arrays/08-assigning-arrays/02-arrays-are-value-types/main.go new file mode 100644 index 0000000..aa36f54 --- /dev/null +++ b/14-arrays/08-assigning-arrays/02-arrays-are-value-types/main.go @@ -0,0 +1,27 @@ +// 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() { + a := [3]int{5, 4, 7} + b := a + + // this will only change the 'a' array + a[0] = 10 + + fmt.Println("a =>", a) + fmt.Println("b =>", b) + + // this will only change the 'b' array + b[1] = 20 + + fmt.Println("a =>", a) + fmt.Println("b =>", b) +} diff --git a/14-arrays/08-assigning-arrays/03-assigning-arrays2/main.go b/14-arrays/08-assigning-arrays/03-assigning-arrays2/main.go new file mode 100644 index 0000000..e558aa7 --- /dev/null +++ b/14-arrays/08-assigning-arrays/03-assigning-arrays2/main.go @@ -0,0 +1,23 @@ +// 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() { + a := [3]int{5, 4, 7} + b := [3]int{6, 9, 3} + + b = a + + fmt.Println("b =>", b) + + b[0] = 100 + fmt.Println("a =>", a) + fmt.Println("b =>", b) +} diff --git a/14-arrays/08-assigning-arrays/04-cannot-assign-arrays/main.go b/14-arrays/08-assigning-arrays/04-cannot-assign-arrays/main.go new file mode 100644 index 0000000..20c3a03 --- /dev/null +++ b/14-arrays/08-assigning-arrays/04-cannot-assign-arrays/main.go @@ -0,0 +1,23 @@ +// 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 + +func main() { + // you can't assign the array 'a' to the array 'b' + // a's type is [2]int whereas b's type is [3]int + + // when two values are not comparable, + // then they're not assignable either + + // uncomment and observe the error + + // a := [2]int{5, 4} + // b := [3]int{6, 9, 3} + + // b = a +} diff --git a/14-arrays/08-assigning-arrays/05-passing-to-funcs/main.go b/14-arrays/08-assigning-arrays/05-passing-to-funcs/main.go new file mode 100644 index 0000000..7ed609e --- /dev/null +++ b/14-arrays/08-assigning-arrays/05-passing-to-funcs/main.go @@ -0,0 +1,31 @@ +// 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() { + a := [3]int{10, 4, 7} + + // like the assignment passing an array to a function + // creates a copy of the array + fmt.Println("a (before) =>", a) + incr(a) + fmt.Println("a (after) =>", a) +} + +// inside the function +// the passed array 'a' is a new copy (a clone) +// it's not the original one +func incr(a [3]int) { + // this only changes the copy of the passed array + a[1]++ + + // this prints the copied array not the original one + fmt.Println("a (inside) =>", a) +} diff --git a/14-arrays/09-multi-dimensional-arrays/main.go b/14-arrays/09-multi-dimensional-arrays/main.go new file mode 100644 index 0000000..108dc0f --- /dev/null +++ b/14-arrays/09-multi-dimensional-arrays/main.go @@ -0,0 +1,49 @@ +// 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() { + // [LENGTH]TYPE { + // ELEMENTS + // } + + // LENGTH=2 and TYPE=[2]int + + // nums := [2][2]int{ + // [2]int{2, 4}, + // [2]int{1, 3}, + // } + + // code below is the same as the code above + nums := [2][2]int{ + {2, 4}, + {1, 3}, + } + + fmt.Println("nums =", nums) + fmt.Println("nums[0] =", nums[0]) + fmt.Println("nums[1] =", nums[1]) + + fmt.Println("nums[0][0] =", nums[0][0]) + fmt.Println("nums[0][1] =", nums[0][1]) + fmt.Println("nums[1][0] =", nums[1][0]) + fmt.Println("nums[1][1] =", nums[1][1]) + + fmt.Println("len(nums) =", len(nums)) + fmt.Println("len(nums[0]) =", len(nums[0])) + fmt.Println("len(nums[1]) =", len(nums[1])) + + for i, array := range nums { + for j, n := range array { + // nums[i][j] = number + fmt.Printf("nums[%d][%d] = %d\n", i, j, n) + } + } +} diff --git a/14-arrays/10-keyed-elements/01-unkeyed/main.go b/14-arrays/10-keyed-elements/01-unkeyed/main.go new file mode 100644 index 0000000..f04c477 --- /dev/null +++ b/14-arrays/10-keyed-elements/01-unkeyed/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() { + rates := [3]float64{ + 0.5, + 2.5, + 1.5, + } + + fmt.Println(rates) +} diff --git a/14-arrays/10-keyed-elements/02-keyed/main.go b/14-arrays/10-keyed-elements/02-keyed/main.go new file mode 100644 index 0000000..e74f912 --- /dev/null +++ b/14-arrays/10-keyed-elements/02-keyed/main.go @@ -0,0 +1,28 @@ +// 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() { + rates := [3]float64{ + 0: 0.5, // index: 0 + 1: 2.5, // index: 1 + 2: 1.5, // index: 2 + } + + fmt.Println(rates) + + // above array literal equals to this: + // + // rates := [3]float64{ + // 0.5, + // 2.5, + // 1.5, + // } +} diff --git a/14-arrays/10-keyed-elements/03-keyed-order/main.go b/14-arrays/10-keyed-elements/03-keyed-order/main.go new file mode 100644 index 0000000..122d389 --- /dev/null +++ b/14-arrays/10-keyed-elements/03-keyed-order/main.go @@ -0,0 +1,28 @@ +// 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() { + rates := [3]float64{ + 1: 2.5, // index: 1 + 0: 0.5, // index: 0 + 2: 1.5, // index: 2 + } + + fmt.Println(rates) + + // above array literal equals to this: + // + // rates := [3]float64{ + // 0.5, + // 2.5, + // 1.5, + // } +} diff --git a/14-arrays/10-keyed-elements/04-keyed-auto-initialize/main.go b/14-arrays/10-keyed-elements/04-keyed-auto-initialize/main.go new file mode 100644 index 0000000..5fa9b54 --- /dev/null +++ b/14-arrays/10-keyed-elements/04-keyed-auto-initialize/main.go @@ -0,0 +1,28 @@ +// 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() { + rates := [3]float64{ + // index 0 empty + // index 1 empty + 2: 1.5, // index: 2 + } + + fmt.Println(rates) + + // above array literal equals to this: + // + // rates := [3]float64{ + // 0., + // 0., + // 1.5, + // } +} diff --git a/14-arrays/10-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go b/14-arrays/10-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go new file mode 100644 index 0000000..a51b1fc --- /dev/null +++ b/14-arrays/10-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go @@ -0,0 +1,36 @@ +// 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() { + // ellipsis (...) below calculates the length of the + // array automatically + rates := [...]float64{ + // index 0 empty + // index 1 empty + // index 2 empty + // index 3 empty + // index 4 empty + 5: 1.5, // index: 5 + } + + fmt.Println(rates) + + // above array literal equals to this: + // + // rates := [6]float64{ + // 0., + // 0., + // 0., + // 0., + // 0., + // 1.5, + // } +} diff --git a/14-arrays/10-keyed-elements/06-keyed-and-unkeyed/main.go b/14-arrays/10-keyed-elements/06-keyed-and-unkeyed/main.go new file mode 100644 index 0000000..76839a7 --- /dev/null +++ b/14-arrays/10-keyed-elements/06-keyed-and-unkeyed/main.go @@ -0,0 +1,34 @@ +// 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() { + rates := [...]float64{ + // index 1 to 4 empty + + 5: 1.5, // index: 5 + 2.5, // index: 6 + 0: 0.5, // index: 0 + } + + fmt.Println(rates) + + // above array literal equals to this: + // + // rates := [7]float64{ + // 0.5, + // 0., + // 0., + // 0., + // 0., + // 1.5, + // 2.5, + // } +} diff --git a/14-arrays/11-project-xratio/01-without-keys/main.go b/14-arrays/11-project-xratio/01-without-keys/main.go new file mode 100644 index 0000000..3b5865a --- /dev/null +++ b/14-arrays/11-project-xratio/01-without-keys/main.go @@ -0,0 +1,21 @@ +// 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() { + rates := [...]float64{ + 25.5, // ethereum + 120.5, // wanchain + } + + // uses magic values - not good + fmt.Printf("1 BTC is %g ETH\n", rates[0]) + fmt.Printf("1 BTC is %g WAN\n", rates[1]) +} diff --git a/14-arrays/11-project-xratio/02-with-keys/main.go b/14-arrays/11-project-xratio/02-with-keys/main.go new file mode 100644 index 0000000..91759e3 --- /dev/null +++ b/14-arrays/11-project-xratio/02-with-keys/main.go @@ -0,0 +1,30 @@ +// 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" + +// REFACTORED VERSION +// It uses well-defined names instead of magic numbers. +// Thanks to keyed elements and constants. + +func main() { + const ( + ETH = iota + WAN + ) + + rates := [...]float64{ + ETH: 25.5, + WAN: 120.5, + } + + // uses well-defined names (ETH, WAN, ...) - good + fmt.Printf("1 BTC is %g ETH\n", rates[ETH]) + fmt.Printf("1 BTC is %g WAN\n", rates[WAN]) +} diff --git a/14-arrays/12-random-message/01-1st-version/main.go b/14-arrays/12-random-message/01-1st-version/main.go new file mode 100644 index 0000000..c66cf4d --- /dev/null +++ b/14-arrays/12-random-message/01-1st-version/main.go @@ -0,0 +1,38 @@ +// 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/rand" + "os" + "time" +) + +func main() { + args := os.Args[1:] + if len(args) != 1 { + fmt.Println("[your name]") + return + } + + name := args[0] + + moods := [...]string{ + "happy 😀", "good 👍", "awesome 😎", + "sad 😞", "bad 👎", "terrible 😩", + } + + rand.Seed(time.Now().UnixNano()) + n := rand.Intn(len(moods)) + + fmt.Printf("%s feels %s\n", name, moods[n]) +} + +// inspired from: +// https://github.com/moby/moby/blob/1fd7e4c28d3a4a21c3540f03a045f96a4190b527/pkg/namesgenerator/names-generator.go diff --git a/14-arrays/12-random-message/02-2nd-version/main.go b/14-arrays/12-random-message/02-2nd-version/main.go new file mode 100644 index 0000000..76b6544 --- /dev/null +++ b/14-arrays/12-random-message/02-2nd-version/main.go @@ -0,0 +1,39 @@ +// 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/rand" + "os" + "time" +) + +func main() { + args := os.Args[1:] + if len(args) != 2 { + fmt.Println("[your name] [positive|negative]") + return + } + + name, mood := args[0], args[1] + + moods := [...][3]string{ + {"happy 😀", "good 👍", "awesome 😎"}, + {"sad 😞", "bad 👎", "terrible 😩"}, + } + + rand.Seed(time.Now().UnixNano()) + n := rand.Intn(len(moods[0])) + + var mi int + if mood != "positive" { + mi = 1 + } + fmt.Printf("%s feels %s\n", name, moods[mi][n]) +} diff --git a/14-arrays/13-refactor-lucky-number-game/main.go b/14-arrays/13-refactor-lucky-number-game/main.go new file mode 100644 index 0000000..d92d041 --- /dev/null +++ b/14-arrays/13-refactor-lucky-number-game/main.go @@ -0,0 +1,72 @@ +// 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/rand" + "os" + "strconv" + "time" +) + +const ( + maxTurns = 5 // less is more difficult + usage = `Welcome to the Lucky Number Game! + +The program will pick %d random numbers. +Your mission is to guess one of those numbers. + +The greater your number is, harder it gets. + +Wanna play? +` +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + args := os.Args[1:] + + if len(args) != 1 { + fmt.Printf(usage, maxTurns) + return + } + + guess, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Println("Not a number.") + return + } + + if guess < 0 { + fmt.Println("Please pick a positive number.") + return + } + + // This array will store the generated random numbers + var pickeds [maxTurns]int + + for turn := 0; turn < maxTurns; turn++ { + n := rand.Intn(guess + 1) + + pickeds[turn] = n + + if n == guess { + fmt.Println("🎉 YOU WIN!") + goto pickeds + } + } + + fmt.Println("☠️ YOU LOST... Try again?") + +pickeds: + fmt.Println("Computer has picked these:", pickeds) + + // after this line the program automatically exits +} diff --git a/14-arrays/exercises/00/main.go b/14-arrays/exercises/00/main.go new file mode 100644 index 0000000..1bace58 --- /dev/null +++ b/14-arrays/exercises/00/main.go @@ -0,0 +1,22 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// ? +// +// NOTE +// ? +// +// EXPECTED OUTPUT +// ? +// --------------------------------------------------------- + +func main() { +} diff --git a/14-arrays/exercises/00/solution/main.go b/14-arrays/exercises/00/solution/main.go new file mode 100644 index 0000000..b5e729e --- /dev/null +++ b/14-arrays/exercises/00/solution/main.go @@ -0,0 +1,11 @@ +// 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 + +func main() { +} diff --git a/14-arrays/exercises/xxx-lucky-number-unique-picker/main.go b/14-arrays/exercises/xxx-lucky-number-unique-picker/main.go new file mode 100644 index 0000000..44beb17 --- /dev/null +++ b/14-arrays/exercises/xxx-lucky-number-unique-picker/main.go @@ -0,0 +1,82 @@ +// 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/rand" + "os" + "strconv" + "time" +) + +const ( + maxTurns = 5 // less is more difficult + usage = `Welcome to the Lucky Number Game! + +The program will pick %d random numbers. +Your mission is to guess one of those numbers. + +The greater your number is, harder it gets. + +Wanna play? +` +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + args := os.Args[1:] + + if len(args) != 1 { + fmt.Printf(usage, maxTurns) + return + } + + guess, err := strconv.Atoi(args[0]) + if err != nil { + fmt.Println("Not a number.") + return + } + + if guess < 0 { + fmt.Println("Please pick a positive number.") + return + } + + // This array will store the generated random numbers + var pickeds [maxTurns]int + + for turn := 0; turn < maxTurns; turn++ { + var n int + pick: + for { + n = rand.Intn(guess + 1) + for _, v := range pickeds { + if n == v { + continue pick + } + } + break + } + + pickeds[turn] = n + + if n == guess { + fmt.Println("🎉 YOU WIN!") + goto pickeds + } + } + + fmt.Println("☠️ YOU LOST... Try again?") + +pickeds: + fmt.Println("Computer has picked these:", pickeds) + + // after this line the program automatically exits +} diff --git a/14-arrays/questions/questions.md b/14-arrays/questions/questions.md new file mode 100644 index 0000000..cac63ae --- /dev/null +++ b/14-arrays/questions/questions.md @@ -0,0 +1,3 @@ +## ? +* text *CORRECT* +* text diff --git a/15-slices/01-intro/01-theory/main.go b/15-slices/01-intro/01-theory/main.go new file mode 100644 index 0000000..e41c332 --- /dev/null +++ b/15-slices/01-intro/01-theory/main.go @@ -0,0 +1,82 @@ +// 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() { + // ----------------------------------------------------- + // COMPLEX LITERALS: + // ----------------------------------------------------- + // array literal : [4]int{1, 2, 3} + // slice literal : []int{1, 2, 3} + // + // You didn't learn about these yet: + // + // map literal : map[int]int{1: 2} + // struct literal: struct{ x, y int }{ 1, 2 } + + // ----------------------------------------------------- + // SLICE LITERAL: + // ----------------------------------------------------- + r := []rune{'h', 'e', 'y'} // 3 items + i := []int{'h', 'e', 'y'} // 3 items + b := []byte{'h', 'e', 'y'} // 3 items + s := []string{"hey"} // 1 item + + fmt.Printf("[]rune type: %T\n", r) + fmt.Printf("[]int type: %T\n", i) + fmt.Printf("[]byte type: %T\n", b) + fmt.Printf("[]string type: %T\n", s) + + // ----------------------------------------------------- + // multi-dimensional slice + // ----------------------------------------------------- + multi := [][]rune{ + {1, 2, 3}, + // each item can have a different length + {4, 5, 6, 7, 8}, + } + fmt.Printf("multi's type : %T\n", multi) + fmt.Printf("multi's length : %d\n", len(multi)) + fmt.Printf("multi[0]'s length: %d\n", len(multi[0])) + fmt.Printf("multi[1]'s length: %d\n", len(multi[1])) + + // ----------------------------------------------------- + // getting and setting elements + // ----------------------------------------------------- + hello := []rune{'h', 'e', 'y'} + fmt.Println(hello[0], hello[1], hello[2]) + + hello[0] = 'y' + hello[1] = 'o' + hello[2] = '!' + fmt.Println(hello[0], hello[1], hello[2]) + + // ----------------------------------------------------- + // empty slice + // ----------------------------------------------------- + e := []rune{} + fmt.Printf("empty slice's length: %d\n", len(e)) + + // ----------------------------------------------------- + // nil slice + // slice's zero value is nil + // ----------------------------------------------------- + var n []rune + fmt.Println("Really nil?", n == nil) + + // you can't get/set elements of a nil slice + // if you run the code below, it will error at runtime + // + // fmt.Println(n[0]) + // fmt.Println(n[1]) + + // but you can get its length + fmt.Printf("nil slice's length: %d\n", len(n)) +} diff --git a/15-slices/01-intro/02-examples/main.go b/15-slices/01-intro/02-examples/main.go new file mode 100644 index 0000000..450fb28 --- /dev/null +++ b/15-slices/01-intro/02-examples/main.go @@ -0,0 +1,61 @@ +// 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" + + s "github.com/inancgumus/prettyslice" +) + +func main() { + basket := []string{"banana", "apple", "coffee"} + s.Show("basket (before)", basket) + + // #1 example: type + fmt.Printf("[]string type: %T\n", basket) + + // #2 example: getting and setting elements + fmt.Println(basket[0], basket[1], basket[2]) + + basket[0] = "pepper" + basket[1] = "water" + basket[2] = "tea" + s.Show("basket (after)", basket) + + // #3 example: empty slice + emptyBasket := []rune{} + s.Show(`emptyBasket := []rune{}`, emptyBasket) + + // #4 example: nil slice + // a slice's zero value is nil + var nilButHappy []rune + + s.Show(`var nilButHappy []rune`, nilButHappy) + + // #5 example: comparing to nil + fmt.Println("nilButHappy == nil", nilButHappy == nil) + fmt.Println("emptyBasket == nil", emptyBasket == nil) + + // you can't compare slices other than nil + // nilButHappy == emptyBasket + + // #6 example: comparing slices + newBasket := []string{"pepper", "water", "tea"} + + equal := true + for i := range basket { + if basket[i] != newBasket[i] { + equal = false + break + } + } + + s.Show("equal?", basket, newBasket) + fmt.Println(equal) +} diff --git a/15-slices/02-append/01-theory/main.go b/15-slices/02-append/01-theory/main.go new file mode 100644 index 0000000..a504d9b --- /dev/null +++ b/15-slices/02-append/01-theory/main.go @@ -0,0 +1,46 @@ +// 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 ( + s "github.com/inancgumus/prettyslice" +) + +func main() { + nums := []int{1, 2, 3} + s.Show("nums := []int{1, 2, 3}", nums) + + // ----------------------------------------------------- + nums = append(nums, 4) + s.Show("append(nums, 4)", nums) + + // ----------------------------------------------------- + nums = append(nums, 9) + s.Show("append(nums, 9)", nums) + + // ----------------------------------------------------- + // let's reset nums + // and let's add multiple elements + nums = []int{1, 2, 3} + s.Show("nums = []int{1, 2, 3}", nums) + + nums = append(nums, 4, 9) + s.Show("append(nums, 4, 9)", nums) + + // ----------------------------------------------------- + // let's reset nums again + // let's add multiple elements using the ellipsis + nums = []int{1, 2, 3} + tens := []int{12, 13} + + s.Show("nums = []int{1, 2, 3}", nums) + s.Show("tens := []int{12, 13}", tens) + + nums = append(nums, tens...) + s.Show("append(nums, tens...)", nums) +} diff --git a/15-slices/02-append/02-examples/main.go b/15-slices/02-append/02-examples/main.go new file mode 100644 index 0000000..9bb0d65 --- /dev/null +++ b/15-slices/02-append/02-examples/main.go @@ -0,0 +1,26 @@ +// 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 ( + s "github.com/inancgumus/prettyslice" +) + +func main() { + var basket []string + + basket = append(basket, "banana") + basket = append(basket, "milk", "apple") + + todo := []string{"tea", "coffee", "salt"} + basket = append(basket, todo...) + + _ = append(basket, "pepper") + + s.Show("my basket", basket) +} diff --git a/15-slices/03-slices-vs-arrays/01-with-arrays/main.go b/15-slices/03-slices-vs-arrays/01-with-arrays/main.go new file mode 100644 index 0000000..a11c321 --- /dev/null +++ b/15-slices/03-slices-vs-arrays/01-with-arrays/main.go @@ -0,0 +1,40 @@ +// 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 ( + "math/rand" + "time" + + s "github.com/inancgumus/prettyslice" +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + const max = 10 + // max, _ := strconv.Atoi(os.Args[1]) + + var uniques [max]int + +loop: + for found := 0; found < max; { + n := rand.Intn(max + 1) + + for _, u := range uniques { + if u == n { + continue loop + } + } + + uniques[found] = n + found++ + } + + s.Show("Uniques", uniques) +} diff --git a/15-slices/03-slices-vs-arrays/02-with-slices/main.go b/15-slices/03-slices-vs-arrays/02-with-slices/main.go new file mode 100644 index 0000000..1d443b1 --- /dev/null +++ b/15-slices/03-slices-vs-arrays/02-with-slices/main.go @@ -0,0 +1,43 @@ +// 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 ( + "math/rand" + "os" + "strconv" + "time" + + s "github.com/inancgumus/prettyslice" +) + +func main() { + rand.Seed(time.Now().UnixNano()) + + max, _ := strconv.Atoi(os.Args[1]) + + // declare an uninitialized nil slice + var uniques []int + +loop: + // you can still use the len function on a nil slice + for len(uniques) < max { + n := rand.Intn(max + 1) + + for _, u := range uniques { + if u == n { + continue loop + } + } + + // let's grow the slice by appending + uniques = append(uniques, n) + } + + s.Show("Uniques", uniques) +} diff --git a/15-slices/04-copy/01-usage/main.go b/15-slices/04-copy/01-usage/main.go new file mode 100644 index 0000000..48455cb --- /dev/null +++ b/15-slices/04-copy/01-usage/main.go @@ -0,0 +1,28 @@ +// 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" + + s "github.com/inancgumus/prettyslice" +) + +func main() { + me := []byte{'m', 'e'} + yo := []byte{'y', 'o', '!'} + + s.Show("me [before]", me) + s.Show("yo [before]", yo) + + N := copy(me, yo) + fmt.Printf("%d element(s) copied.\n", N) + + s.Show("me [after]", me) + s.Show("yo [after]", yo) +} diff --git a/15-slices/04-copy/02-hacker-incident/main.go b/15-slices/04-copy/02-hacker-incident/main.go new file mode 100644 index 0000000..73af75e --- /dev/null +++ b/15-slices/04-copy/02-hacker-incident/main.go @@ -0,0 +1,44 @@ +// 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" + + s "github.com/inancgumus/prettyslice" +) + +func main() { + // we've received the raining probabilities + data := []float64{10, 25, 30, 50} + + s.Show("Probabilities", data) + + fmt.Printf("Is it gonna rain? %.f%% chance.\n", + (data[0]+data[1]+data[2]+data[3])/ + float64(len(data))) + + // ----------------------------------------------------- + // but it turns out that the first two items of the data + // has been corrupted by a hacker. + // + // this time, we've received clean data. + // let's overwrite the invalid data by copying + + copy(data, []float64{80, 90}) + + // why copy? why not just assign and overwrite? + // because: it overwrites the whole slice + // data = []float64{80, 90} + + s.Show("Probabilities", data) + + fmt.Printf("Is it gonna rain? %.f%% chance.\n", + (data[0]+data[1]+data[2]+data[3])/ + float64(len(data))) +} diff --git a/15-slices/05-whats-a-slice-in-real/01-theory/main.go b/15-slices/05-whats-a-slice-in-real/01-theory/main.go new file mode 100644 index 0000000..f6153b0 --- /dev/null +++ b/15-slices/05-whats-a-slice-in-real/01-theory/main.go @@ -0,0 +1,78 @@ +// 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" + + s "github.com/inancgumus/prettyslice" +) + +/* +If you find this code hard to understand, please comment +some parts of it and run it again. +*/ + +func main() { + // example #1 + slice := []int{5, 6, 7, 8, 9} + s.Show("slice", slice) + + // fmt.Println("slice[0]:", slice[0]) + // fmt.Println("slice[1]:", slice[1]) + // fmt.Println("slice[2]:", slice[2]) + // fmt.Println("slice[3]:", slice[3]) + // fmt.Println("slice[4]:", slice[4]) + + // example #2 + sliced := slice[1:4] + s.Show("slice[1:4]", sliced) + + // fmt.Println("sliced[0]:", sliced[0]) + // fmt.Println("sliced[1]:", sliced[1]) + // fmt.Println("sliced[2]:", sliced[2]) + // fmt.Println("sliced[3]:", sliced[3]) // -> you can't + + // example #3 + // the new slice will also be effected from this change + sliced = append(sliced, 15) + slice[1] = 200 + s.Show("append(sliced, 15)", sliced) + + // example #3b + // the new slice won't be effected anymore + // because, go has created a new array for the `s` + sliced = append(sliced, 3) + slice[1] = 0 + s.Show("slice[1] = 0", slice) + s.Show("sliced", sliced) + + // example #4 + // its pointer will stay the same until 8 elements + sliced = append(sliced, 10, 11, 12) + s.Show("append(sliced, 10, 11, 12)", sliced) + + // now it will change: 13 the wicked number! + sliced = append(sliced, 13) + s.Show("append(sliced, 13)", sliced) + + // example #5 + var ( + // just declaring it will make it nil + nilButHappy []int + + // without any elements will make empty + empty = []int{} + ) + + s.Show("Empty Slice", empty) + s.Show("Nil Slice", nilButHappy) + + fmt.Println("empty == nil?", empty == nil) + fmt.Println("nilButHappy == nil?", nilButHappy == nil) +} diff --git a/15-slices/05-whats-a-slice-in-real/02-example/main.go b/15-slices/05-whats-a-slice-in-real/02-example/main.go new file mode 100644 index 0000000..a3f8a79 --- /dev/null +++ b/15-slices/05-whats-a-slice-in-real/02-example/main.go @@ -0,0 +1,36 @@ +// 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 ( + s "github.com/inancgumus/prettyslice" +) + +// Please uncomment the examples below to see the results + +func main() { + colors := []string{"black", "white"} + + // 1st example + vivid := colors + vivid[0] = "orange" + + // 2nd example + // vivid = nil + + // 3th example + vivid = append(colors, "yellow") + vivid[0] = "gray" + + // 4th example + colors = append(colors, "yellow") + colors[0] = "gray" + + s.Show("colors slice header", colors) + s.Show("vivid slice header", vivid) +} diff --git a/15-slices/06-slice-expressions/01-theory/main.go b/15-slices/06-slice-expressions/01-theory/main.go new file mode 100644 index 0000000..c995721 --- /dev/null +++ b/15-slices/06-slice-expressions/01-theory/main.go @@ -0,0 +1,18 @@ +// 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 s "github.com/inancgumus/prettyslice" + +func main() { + slicable := []byte{'c', 'o', 'l', 'o', 'r'} + s.Show("slicable[0:5]", slicable[0:5]) + s.Show("slicable[0:2]", slicable[0:2]) + s.Show("slicable[3:5]", slicable[3:5]) + s.Show("slicable[1:4]", slicable[1:4]) +} diff --git a/15-slices/06-slice-expressions/02-examples/main.go b/15-slices/06-slice-expressions/02-examples/main.go new file mode 100644 index 0000000..6173596 --- /dev/null +++ b/15-slices/06-slice-expressions/02-examples/main.go @@ -0,0 +1,56 @@ +// 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" + + s "github.com/inancgumus/prettyslice" +) + +func main() { + // 1. slicing creates a new slice + nums := []int{1, 3, 5, 2, 4, 8} + odds := nums[:3] + evens := nums[3:] + + // 2. backing array is shared + nums[1], nums[3] = 9, 6 + s.Show("nums", nums) + s.Show("odds : nums[:3]", odds) + s.Show("evens: nums[3:]", evens) + + // 3. you can create a new slice from an array + heyArr := [3]byte{'h', 'e', 'y'} + hey, he := heyArr[:], heyArr[:2] + + // 4. sliced array will be shared among the slices + heyArr[0] = 'm' + s.Show("hey := heyArr[:]", hey) + s.Show("he := heyArr[:2]", he) + + // 5. index expression returns a value + // while a slice expression returns a slice + s.Show("nums[0]", nums[0]) + s.Show("nums[0:1]", nums[0:1]) + fmt.Printf("nums[0] : %T\n", nums[0]) // just int + fmt.Printf("nums[0:1]: %T\n", nums[0:1]) // []int slice + + // 6. extending a slice up to its capacity + first := nums[0:1] + s.Show("first := nums[0:1]", first) + s.Show("first[0:2]", first[0:2]) + s.Show("first[0:3]", first[0:3]) + s.Show("first[0:4]", first[0:4]) + s.Show("first[0:5]", first[0:5]) + s.Show("first[0:6]", first[0:6]) + // s.Show("first[0:7]", first[0:7]) // <- you can't + + first = append(first[0:6], 9) + s.Show("first: with a new backing array", first) +} diff --git a/15-slices/07-full-slice-expressions/main.go b/15-slices/07-full-slice-expressions/main.go new file mode 100644 index 0000000..c24f63f --- /dev/null +++ b/15-slices/07-full-slice-expressions/main.go @@ -0,0 +1,23 @@ +// 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 s "github.com/inancgumus/prettyslice" + +func main() { + ships := []string{ + "Normandy", "Verrikan", "Nexus", + } + + // frigates := ships[:2] + frigates := ships[:2:2] + frigates = append(frigates, "Warsaw") + + s.Show("Ships", ships) + s.Show("Frigates", frigates) +} diff --git a/15-slices/08-make/main.go b/15-slices/08-make/main.go new file mode 100644 index 0000000..35616c0 --- /dev/null +++ b/15-slices/08-make/main.go @@ -0,0 +1,31 @@ +// 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 s "github.com/inancgumus/prettyslice" + +func main() { + // #1: make preallocates an array + nums := make([]int, 0, 10) + nums = append(nums, 1, 2, 3) + nums = append(nums, 4, 5) + s.Show("nums", nums) + + nums = append(nums, 6) + s.Show("nums: doesn't allocate", nums) + + // #2: prevent overwriting with make + doubles := make([]int, len(nums) /*, 12 */) + + for i := range nums { + doubles[i] = nums[i] * 2 + } + + s.Show("nums: after doubling", nums) + s.Show("doubles", doubles) +} diff --git a/15-slices/09-bouncing-ball-challenge/01-challenge/main.go b/15-slices/09-bouncing-ball-challenge/01-challenge/main.go new file mode 100644 index 0000000..4a78421 --- /dev/null +++ b/15-slices/09-bouncing-ball-challenge/01-challenge/main.go @@ -0,0 +1,181 @@ +// 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 + +// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ +// Post your solution to twitter with this hashtag: +// #learngoprogramming +// +// Notify me on twitter by adding my account to your tweet: +// @inancgumus +// +// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + +// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ +// This document contains what you need to do to create +// the bouncing ball animation. +// +// You're going to learn a great deal of knowledge about +// slices and you'll earn a good experience while doing this. +// +// However, refer to this document only when you get stuck. +// Do not follow it step by step. +// Try to solve the challenge on your own. +// +// This document organized into steps/sections. +// So you can jump to that step/section directly. +// +// Good luck! +// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + +// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ +// #1 Declare constants here. +// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ +// The width and the height of the board. +// You're going to draw your board using these. + +func main() { + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // -> Declare ball positions: X and Y + // Initialize them to 0s. + + // -> Declare ball's velocity: xVelocity and yVelocity + // + // Velocity means: Speed and Direction + // X velocity = 1 // balls moves to the right + // X velocity = -1 // balls moves to the left + // Y velocity = 1 // balls moves down + // Y velocity = -1 // balls moves up + // + // -> On each step, add velocities to ball's position. + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 🎾 CREATE THE BOARD + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // + // -> Use the make function to initialize your board. + // Remember: You also need to initialize each sub-slice. + // (in a for loop) + // + // -> You can use [][]bool for your board. + // + // Because, when you set one of the items to true, + // then you know that the ball is in that position. + // + // EXAMPLE: + // false false false false + // false true -+ false false + // false false | false false + // v + // the ball is here + // board[1][1] is true + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 🎾 DRAWING LOOP + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // { + // + // Create a loop. + // + // On each step of the loop, you're going to: + // -> Clear the board + // -> Calculate the next ball position + // -> Draw the board with the balls + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 🎾 CLEAR THE BOARD + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // -> Set all the board elements to false. + // (I mean the sub-slices' elements) + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 🎾 CALCULATE THE NEXT BALL POSITION + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // -> Add the velocities to the ball's current position: + // + // X += xVelocity + // Y += yVelocity + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 👉 When ball hits the borders change its direction. + // + // -> Multiply the velocity by -1 to change its X direction. + // -> Do the same for the Y velocity as well. + + // 👉 Set the ball's position in the board. + // + // -> You will use this information when drawing the board. + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 🎾 DRAW THE BOARD + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // -> Make a large enough []rune `buffer`. + // + // HINT: width * height will give you a large enough buffer. + // TIP : You could also use string but it would be inefficient. + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 👉 FILL YOUR BUFFER: + // + // + It's better to use buffers for these kind of things. + // + It's worst to call the Print functions all the time. + // + // 1. Loop for the height of the board. + // 2. Then in a nested loop, loop for the width of the board. + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 👉 NESTEP LOOP (WIDTH LOOP): + // + // In each step of the nested loop, do this: + // + // 1. Check whether the ball is in the x, y positions. + // You need to check for it using your board slice. + // + // 2. If so, append this tennis ball '🎾' to the buf slice. + // 3. If not, append this pool ball '🎱' to the buf slice. + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 👉 HEIGHT LOOP: + // + // After the nested loop (but in the height loop): + // + // 1. Append the newline character to the buf: '\n' + // This will allow you to print the next row to the + // next line. + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 🎾 PRINT THE BOARD + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // After the loop print this to clear the screen. + // + // fmt.Print("\033[2J") + // + // Note : This will only work in Linux and Mac. + // For Windows: Just install Ubuntu bash on Windows, it's easy now! + // It isn't a virtual machine. + // https://docs.microsoft.com/en-us/windows/wsl/install-win10 + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 👉 PRINT YOUR BOARD (USING THE BUFFER): + // + // -> Do not forget converting it to string. + // Because your buffer is []rune. + // + // fmt.Print(string(buf)) + // + // You'll learn the details about rune and strings later. + + // ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + // 👉 SLOW DOWN THE SPEED + // And lastly, call the time.Sleep function to slow down + // the speed of the loop, so you can see the ball :) + // + // time.Sleep(time.Millisecond * 60) + + // } DRAWING LOOP ENDS HERE 👈 +} diff --git a/15-slices/09-bouncing-ball-challenge/02-solution-draw-the-board/main.go b/15-slices/09-bouncing-ball-challenge/02-solution-draw-the-board/main.go new file mode 100644 index 0000000..cc5dd65 --- /dev/null +++ b/15-slices/09-bouncing-ball-challenge/02-solution-draw-the-board/main.go @@ -0,0 +1,47 @@ +// 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" +) + +const ( + width = 25 + height = 8 +) + +func main() { + // // ------------------------------------------------- + // // CREATE THE BOARD + // // ------------------------------------------------- + board := make([][]bool, width) + for row := range board { + board[row] = make([]bool, height) + } + + // ------------------------------------------------- + // DRAW THE BOARD + // ------------------------------------------------- + var ( + buf = make([]rune, 0, width*height) + ball rune + ) + + for y := range board[0] { + for x := range board { + ball = '🎱' + if board[x][y] { + ball = '🎾' + } + buf = append(buf, ball, ' ') + } + buf = append(buf, '\n') + } + fmt.Print(string(buf)) +} diff --git a/15-slices/09-bouncing-ball-challenge/03-solution-drawing-loop/main.go b/15-slices/09-bouncing-ball-challenge/03-solution-drawing-loop/main.go new file mode 100644 index 0000000..acac370 --- /dev/null +++ b/15-slices/09-bouncing-ball-challenge/03-solution-drawing-loop/main.go @@ -0,0 +1,64 @@ +// 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" + "time" +) + +const ( + width = 25 + height = 8 + + maxFrames = 1200 +) + +func main() { + // // ------------------------------------------------- + // // CREATE THE BOARD + // // ------------------------------------------------- + board := make([][]bool, width) + for row := range board { + board[row] = make([]bool, height) + } + + var X, Y int // ball positions + + for i := 0; i < maxFrames; i++ { + // draw after a while: slows down the animation + time.Sleep(time.Second / 20) + + // ------------------------------------------------- + // PUT THE BALL + // ------------------------------------------------- + board[X][Y] = true + + // ------------------------------------------------- + // DRAW THE BOARD + // ------------------------------------------------- + var ( + buf = make([]rune, 0, width*height) + ball rune + ) + + for y := range board[0] { + for x := range board { + ball = '🎱' + if board[x][y] { + ball = '🎾' + } + buf = append(buf, ball, ' ') + } + buf = append(buf, '\n') + } + + // clear the screen and draw the board + fmt.Print("\033[2J", string(buf)) + } +} diff --git a/15-slices/09-bouncing-ball-challenge/04-solution-final/main.go b/15-slices/09-bouncing-ball-challenge/04-solution-final/main.go new file mode 100644 index 0000000..ad7d37b --- /dev/null +++ b/15-slices/09-bouncing-ball-challenge/04-solution-final/main.go @@ -0,0 +1,87 @@ +// 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" + "time" +) + +const ( + width = 25 + height = 8 + + maxFrames = 1200 +) + +func main() { + // // ------------------------------------------------- + // // CREATE THE BOARD + // // ------------------------------------------------- + board := make([][]bool, width) + for row := range board { + board[row] = make([]bool, height) + } + + var ( + X, Y int // ball positions + xVel, yVel = 1, 1 // velocities + ) + + for i := 0; i < maxFrames; i++ { + // draw after a while: slows down the animation + time.Sleep(time.Second / 20) + + // ------------------------------------------------- + // CALCULATE THE NEXT BALL POSITION + // ------------------------------------------------- + X += xVel + Y += yVel + + // when the ball hits the borders change its direction + // by changing its velocity + if X <= 0 || X >= width-1 { + xVel *= -1 + } + if Y <= 0 || Y >= height-1 { + yVel *= -1 + } + + // ------------------------------------------------- + // CLEAR THE BOARD AND PUT THE BALL + // ------------------------------------------------- + for y := range board[0] { + for x := range board { + board[x][y] = false + } + } + board[X][Y] = true + + // ------------------------------------------------- + // DRAW THE BOARD + // ------------------------------------------------- + var ( + buf = make([]rune, 0, width*height) + ball rune + ) + + for y := range board[0] { + for x := range board { + ball = '🎱' + if board[x][y] { + ball = '🎾' + } + buf = append(buf, ball, ' ') + } + buf = append(buf, '\n') + } + + // clear the screen and draw the board + fmt.Print("\x0c", string(buf)) + } +} diff --git a/15-slices/README-WARNING.md b/15-slices/README-WARNING.md new file mode 100644 index 0000000..58abcdc --- /dev/null +++ b/15-slices/README-WARNING.md @@ -0,0 +1,8 @@ +# WARNING + +For the code in this section, you should install my prettyslice library. + +## STEPS +1. Open your command-line +2. Type: `go get github.com/inancgumus/prettyslice` +3. That's all. \ No newline at end of file diff --git a/16-strings-revisited/01-byte/main.go b/16-strings-revisited/01-byte/main.go new file mode 100644 index 0000000..4f1232b --- /dev/null +++ b/16-strings-revisited/01-byte/main.go @@ -0,0 +1,45 @@ +// 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() { + var g, o rune + + g, o = 'g', 'o' + g, o = 103, 111 + g, o = 0x67, 0x6f + g, o = '\U00000067', '\U0000006f' + g, o = '\u0067', '\u006f' + g, o = '\x67', '\x6f' + + fmt.Println("codepoints") + fmt.Printf(" dec : %d %d\n", g, o) + fmt.Printf(" hex : %x %x\n", g, o) + fmt.Printf(" unicode : %U %U\n", g, o) + fmt.Printf(" chars : %c %c\n", g, o) + + // g++ + // o -= 6 + + g -= 'a' - 'A' + o -= 'a' - 'A' + + fmt.Println("codepoints") + fmt.Printf(" dec : %d %d\n", g, o) + fmt.Printf(" hex : %x %x\n", g, o) + fmt.Printf(" unicode : %U %U\n", g, o) + fmt.Printf(" chars : %c %c\n", g, o) + + // string representations + // fmt.Print("string() : ", string(g), string(o), "\n") + // fmt.Print("hex 1 byte : \x67\x6f \n") + // fmt.Print("hex 2 bytes : \u0067\u006f \n") + // fmt.Print("hex 4 bytes : \U00000067\U0000006f \n") +} diff --git a/16-strings-revisited/02-string-byte-slice/main.go b/16-strings-revisited/02-string-byte-slice/main.go new file mode 100644 index 0000000..198762c --- /dev/null +++ b/16-strings-revisited/02-string-byte-slice/main.go @@ -0,0 +1,28 @@ +// 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() { + msg := "WONDERFUL!" + bytes := []byte(msg) + + fmt.Println("msg :", msg) + fmt.Println("bytes :", bytes) + fmt.Println("string(bytes) :", string(bytes)) + fmt.Println("string(87) :", string(87)) + + fmt.Println() + + for i, v := range msg { + fmt.Printf( + "msg[%d] : %d = %[2]q\n", + i, v) + } +} diff --git a/16-strings-revisited/03-string-indexing-slicing/main.go b/16-strings-revisited/03-string-indexing-slicing/main.go new file mode 100644 index 0000000..a47df4b --- /dev/null +++ b/16-strings-revisited/03-string-indexing-slicing/main.go @@ -0,0 +1,57 @@ +// 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() { + // GOALS: + // 1- String value is immutable + // 2- Indexing vs Slicing + // 3- Using bytes for manipulating strings + + mood := "wonder" + + // 1- a string value is immutable (read-only) + // mood[1] = 'a' + + // 2- Indexing vs Slicing + + // "wonder" + // ^ ^^^^ + // | |||| + // "wandering" + + // "w" + "a" + "nder" + "ing" + + // wandering := mood[0] + "a" + mood[2:] + "ing" + // fmt.Printf("mood[0] : %T - %[1]v\n", mood[0]) // byte + // fmt.Printf("mood[0:1] : %T - %[1]v\n", mood[0:1]) // string + + // wandering := mood[:1] + "a" + mood[2:] + "ing" + fmt.Println(mood) + // fmt.Println(wandering) + + // 3- converting creates a new byte slice (allocation) + b := []byte(mood) + b[1] = 'a' + + // b = append(b, 'i', 'n', 'g') + // b = append(b, []byte{'i', 'n', 'g'}) + b = append(b, "ing"...) + + // starts copying from the first element + copy(b, "listen") + + // starts copying from the "7th" element + copy(b[6:], "ed.") + + fmt.Println(string(b)) +} diff --git a/16-strings-revisited/04-masker-challenge/main.go b/16-strings-revisited/04-masker-challenge/main.go new file mode 100644 index 0000000..14ca092 --- /dev/null +++ b/16-strings-revisited/04-masker-challenge/main.go @@ -0,0 +1,77 @@ +// 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/ +// + +/* +------------------------------------------------------------ +RULES +------------------------------------------------------------ + +* You shouldn't use a standard library function. + +* You should only solve the challenge by manipulating the bytes directly. + +* Manipulate the bytes of a string using indexing, slicing, appending etc. + +* Be efficient: Do not use string concat (+ operator). + + * Instead, create a new byte slice as a buffer from the given string argument. + + * Then, manipulate it during your program. + + * And, for once, print that buffer. + +------------------------------------------------------------ +STEPS +------------------------------------------------------------ + +* Mask only links starting with http:// + +* Don't check for uppercase/lowercase letters + * The goal is to learn manipulating bytes in strings + * It's not about creating a perfect masker + + * For example: A spammer can prevent the masker like this (for now this is OK): + + "Here's my spammy page: hTTp://youth-elixir.com" + +* But, you should catch this: + + "Here's my spammy page: http://hehefouls.netHAHAHA see you." + "Here's my spammy page: http://******************* see you." +*/ +package main + +func main() { + // --------------------------------------------------------------- + // #1 + // --------------------------------------------------------------- + // Check whether there's a command line argument or not + // If not, quit from the program with a message + + // --------------------------------------------------------------- + // #2 + // --------------------------------------------------------------- + // Create a byte buffer as big as the argument + + // --------------------------------------------------------------- + // #3 + // --------------------------------------------------------------- + // 1- Loop and detect the http:// patterns + // 2- Copy the input character by character to the buffer + // 3- If you detect http:// pattern, copy the http:// first, + // then copy the *s instead of the original link until + // you see a whitespace character. + // + // Here: http://www.mylink.com Click! + // -> Here: http://************** Click! + // + + // --------------------------------------------------------------- + // #4 + // --------------------------------------------------------------- + // Print the buffer as a string +} diff --git a/16-strings-revisited/04-masker-challenge/spam.txt b/16-strings-revisited/04-masker-challenge/spam.txt new file mode 100644 index 0000000..6ad043d --- /dev/null +++ b/16-strings-revisited/04-masker-challenge/spam.txt @@ -0,0 +1,7 @@ +Hi guys, + +Here is my new spammy webpage http://www.mysuperpage.com <-- This is my website! + +Please click on the link now!!! + +When you click, I will be rich, thanks! \ No newline at end of file diff --git a/16-strings-revisited/05-masker-solution/main.go b/16-strings-revisited/05-masker-solution/main.go new file mode 100644 index 0000000..e3399fd --- /dev/null +++ b/16-strings-revisited/05-masker-solution/main.go @@ -0,0 +1,88 @@ +// 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/ +// + +/* +#1- Get and check the input +#2- Create a buffer with a sufficient size +#3- Write input to the buffer as it is and print it +#4- Detect the link +#5- Mask the link +#6- Detect white spaces and disable the masking +#7- Write http:// to the buffer, just before the link +*/ + +package main + +import ( + "fmt" + "os" +) + +const ( + link = "http://" + mask = '*' +) + +func main() { + args := os.Args[1:] + if len(args) != 1 { + fmt.Println("gimme somethin' to censor!") + return + } + + var ( + text = args[0] + size = len(text) + + // create a sufficient buffer for the output + // + // and adjust its slice pointer to the first element + // of the backing array! -> make(..., 0, ...) + buf = make([]byte, 0, size) + + in bool + ) + + for i := 0; i < size; i++ { + nlink := len(link) + + // slice the input and look for the link pattern + // do not slice it when it goes beyond the input text's capacity + if len(text[i:]) >= nlink && text[i:i+nlink] == link { + // jump to the next character after "http://" + i += nlink + + // set the flag: we're in a link! -> "http://....." + in = true + + // add the "http://" manually + buf = append(buf, link...) + } + + // get the current byte from the input + c := text[i] + + // disable the link detection flag + // this will prevent masking the rest of the bytes + switch c { + case ' ', '\t', '\n': // try -> unicode.IsSpace + in = false + } + + // if we're in the link detection mode (inside the link bytes) + // then, mask the current character + if in { + c = mask + } + + // add the current character to the buffer + buf = append(buf, c) + } + + // print out the buffer as text (string) + fmt.Println(string(buf)) +} diff --git a/16-strings-revisited/05-masker-solution/spam.txt b/16-strings-revisited/05-masker-solution/spam.txt new file mode 100644 index 0000000..6ad043d --- /dev/null +++ b/16-strings-revisited/05-masker-solution/spam.txt @@ -0,0 +1,7 @@ +Hi guys, + +Here is my new spammy webpage http://www.mysuperpage.com <-- This is my website! + +Please click on the link now!!! + +When you click, I will be rich, thanks! \ No newline at end of file diff --git a/16-strings-revisited/06-encoding/main.go b/16-strings-revisited/06-encoding/main.go new file mode 100644 index 0000000..3a6bb64 --- /dev/null +++ b/16-strings-revisited/06-encoding/main.go @@ -0,0 +1,144 @@ +// 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" + "unicode/utf8" + "unsafe" +) + +// Please run this code and experiment with it +// Observe the results + +// USELESS-NOTE : "Öykü" means "Story" in Turkish! + +func main() { + fmt.Println("-----------------------------------") + fmt.Println("ASCII Codepoints") + fmt.Println("-----------------------------------") + + var ( + a, z byte = 'a', 'z' + A, Z byte = 'A', 'Z' + d0, d9 byte = '0', '9' + ) + + for _, c := range []byte{a, z, A, Z, d0, d9} { + fmt.Printf("%c - 1 byte - %[1]U - %[1]d\n", c) + } + + fmt.Println("\n-----------------------------------") + fmt.Println("Unicode Codepoints") + fmt.Println("-----------------------------------") + + var ( + Ö = 'Ö' + 栗 = '栗' + monkey = '🙉' + ) + for _, c := range []rune{rune(A), Ö, 栗, monkey} { + fmt.Printf("%c - %d bytes - %[1]U - %[1]d\n", c, cptb(c)) + } + + fmt.Println("\n-----------------------------------") + fmt.Println("UTF-8 Encoded") + fmt.Println("-----------------------------------") + + // utf8.RuneLen finds the number of bytes necessary for + // encoding a codepoint to utf8 + for _, c := range []rune{rune(A), Ö, 栗, monkey} { + fmt.Printf("%c - %d bytes - %[1]U - %[1]d\n", c, + utf8.RuneLen(c)) + } + + fmt.Println("\n-----------------------------------") + fmt.Println("Example: Unicode Codepoints") + fmt.Println("-----------------------------------") + + var ( + ö = 'ö' + y = 'y' + k = 'k' + ü = 'ü' + ) + + var ( + oykuRunes = []rune{ö, y, k, ü} + total int + ) + + for _, c := range oykuRunes { + fmt.Printf("%c - %d bytes - %[1]U - %[1]d\n", c, cptb(c)) + + // unsafe.Sizeof finds the memory size of simple values + // don't use it in production-level code -> it's unsafe! + total += int(unsafe.Sizeof(c)) + } + fmt.Printf("TOTAL: %d bytes.\n", total) + + fmt.Println("\n-----------------------------------") + fmt.Println("Example: Indexing") + fmt.Println("-----------------------------------") + + fmt.Printf("%c%c%c%c\n", + oykuRunes[0], oykuRunes[1], oykuRunes[2], + oykuRunes[len(oykuRunes)-1]) + + // string to []rune + oykuRunes = []rune("öykü") + fmt.Printf("%c%c%c%c\n", + oykuRunes[0], oykuRunes[1], oykuRunes[2], + oykuRunes[len(oykuRunes)-1]) + + fmt.Println("\n-----------------------------------") + fmt.Println("Example: UTF-8 Encoding") + fmt.Println("-----------------------------------") + + // this is also ok + // oykuString := string(oykuRunes) + + oykuString := "öykü" + + fmt.Printf("TOTAL bytes in oykuRunes : %d\n", total) + fmt.Printf("TOTAL bytes in oykuString: %d\n", len(oykuString)) + fmt.Printf("TOTAL runes in oykuString: %d\n", + utf8.RuneCountInString(oykuString)) + + fmt.Printf("Runes of oykuString : %s\n", oykuString) + fmt.Printf("Bytes of oykuString : % x\n", oykuString) + + fmt.Println() + for i := 0; i < len(oykuString); i++ { + fmt.Printf("oykuString[%d]: %c\n", i, oykuString[i]) + } + + // slicing returns a slice with the type of the sliced value + // so, the sliced value is a string, then a string is returned + // + // example: + // oykuString[0:2] is a string + fmt.Println() + fmt.Printf("oykuString[0:2]: %q\n", oykuString[0:2]) + fmt.Printf("oykuString[4:6]: %q\n", oykuString[4:6]) +} + +// ------------------------------------------------------------------- +// cptb finds how many bytes are necessary to represent a codepoint +// cptb means codepoint to bytes +func cptb(r rune) int { + switch { + case r <= 0xFF: // 255 + return 1 + case r <= 0xFFFF: // 65,535 + return 2 + case r <= 0xFFFFF: // 16,777,215 + return 3 + } + return 4 +} diff --git a/16-strings-revisited/07-encoding-examples/main.go b/16-strings-revisited/07-encoding-examples/main.go new file mode 100644 index 0000000..a61ab66 --- /dev/null +++ b/16-strings-revisited/07-encoding-examples/main.go @@ -0,0 +1,60 @@ +// 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" + "unicode/utf8" + "unsafe" +) + +func main() { + // try yourself: try other runes! + // you can find more here: https://unicode-table.com + // r := '🙉' + + // r := '\u011e' + r := 'Ğ' + + // only codepoint (can't be printed) + fmt.Printf("before encoding: %d\n", r) + fmt.Printf(" bits : %016b\n", r) + fmt.Printf(" bytes: % x\n", r) + + // utf-8 encoded string + encoded := string(r) + encodedBytes := []byte(encoded) + + fmt.Println() + fmt.Printf("after encoding: %q\n", encoded) + fmt.Printf(" bits : %8b\n", encodedBytes) + fmt.Printf(" bytes: % x\n", encodedBytes) + + // utf-8 string efficient to store and transmit + // but, it's harder to use. + // + // rune slice is inefficient. + // but, it's easy to use. + fmt.Println() + fmt.Println("string (utf-8) vs []rune (unicode)") + + s := "hava çok güzel 😳" + fmt.Printf("%q\n", s) + fmt.Printf(" size : %d bytes\n", len(s)) + fmt.Printf(" len : %d chars\n", utf8.RuneCountInString(s)) + fmt.Printf(" s[5] : %q\n", s[5]) + fmt.Printf(" s[5:7] : %q\n", s[5:7]) + + runes := []rune(s) + size := int(unsafe.Sizeof(runes[0])) * len(runes) + + fmt.Printf("\n%q\n", runes) + fmt.Printf(" size : %d bytes\n", size) + fmt.Printf(" len : %d chars\n", len(runes)) + fmt.Printf(" runes[5] : %q\n", runes[5]) +} diff --git a/16-strings-revisited/08-wrapper-example/main.go b/16-strings-revisited/08-wrapper-example/main.go new file mode 100644 index 0000000..d79344b --- /dev/null +++ b/16-strings-revisited/08-wrapper-example/main.go @@ -0,0 +1,66 @@ +// 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" + "unicode" + "unicode/utf8" +) + +const lineWidth = 40 + +func main() { + text := `Galaksinin Batı Sarmal Kolu'nun bir ucunda, haritası bile çıkarılmamış ücra bir köşede, gözlerden uzak, küçük ve sarı bir güneş vardır. + +Bu güneşin yörüngesinde, kabaca yüz kırksekiz milyon kilometre uzağında, tamamıyla önemsiz ve mavi-yeşil renkli, küçük bir gezegen döner. + +Gezegenin maymun soyundan gelen canlıları öyle ilkeldir ki dijital kol saatinin hâlâ çok etkileyici bir buluş olduğunu düşünürler.` + + var lw int // line width + + for _, r := range text { + fmt.Printf("%c", r) + + switch lw++; { + case lw > lineWidth && r != '\n' && unicode.IsSpace(r): + fmt.Println() + fallthrough + case r == '\n': + lw = 0 + } + } + fmt.Println() +} + +// call it like: runeHandler(text) +func runeHandler(text string) { + for i := 0; i < len(text); { + r := rune(text[i]) + + size := 1 + if r > utf8.RuneSelf { + r, size = utf8.DecodeRuneInString(text[i:]) + // check out the other functions as well, play with them! + // + // for example (type these into the command-line): + // go doc utf8 + // go doc utf8 EncodeRune + } + i += size + + fmt.Printf("%c", r) + } +} + +// call it like: byteHandler(text) +func byteHandler(text string) { + for i := 0; i < len(text); i++ { + fmt.Printf("%c", text[i]) + } +} diff --git a/16-strings-revisited/09-internals/main.go b/16-strings-revisited/09-internals/main.go new file mode 100644 index 0000000..e5cfce7 --- /dev/null +++ b/16-strings-revisited/09-internals/main.go @@ -0,0 +1,45 @@ +// 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" + "unsafe" +) + +func main() { + // empty := "" + // dump(empty) + + hello := "hello" + dump(hello) + dump("hello") + dump("hello!") + + for i := range hello { + dump(hello[i : i+1]) + } + + dump(string([]byte(hello))) + dump(string([]byte(hello))) + dump(string([]rune(hello))) +} + +// StringHeader is used by a string value +// In practice, you should use: reflect.Header +type StringHeader struct { + // points to a backing array's item + pointer uintptr // where it starts + length int // where it ends +} + +// dump prints the string header of a string value +func dump(s string) { + ptr := *(*StringHeader)(unsafe.Pointer(&s)) + fmt.Printf("%q: %+v\n", s, ptr) +} diff --git a/16-strings-revisited/charset-table/main.go b/16-strings-revisited/charset-table/main.go new file mode 100644 index 0000000..eead3fb --- /dev/null +++ b/16-strings-revisited/charset-table/main.go @@ -0,0 +1,83 @@ +// 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" + "os" + "strconv" + "strings" +) + +const ( + asciiStart = '\u0001' // 1 + asciiStop = '\u007f' // 127 + + cols = 1 +) + +func main() { + // DETERMINE START - STOP POSITIONS + var start, stop int + + if args := os.Args[1:]; len(args) == 2 { + start, _ = strconv.Atoi(args[0]) + stop, _ = strconv.Atoi(args[1]) + } + + if start == 0 || stop == 0 { + start, stop = asciiStart, asciiStop + } + + // PRINT HEADER + for i := 0; i < cols; i++ { + fmt.Printf("| %-6s %-8s %-8s %-14s", "rune", "dec", "code", "bytes") + } + fmt.Print("\n", strings.Repeat("-", 40*cols), "\n") + + // PRINT TABLE + for n, l := start, 0; n <= stop; n++ { + // draw the line + fmt.Printf("| %-6q %-8d %-8U % -14x", n, n, n, string(n)) + + // go to next line if columns are consumed + if l++; l%cols == 0 { + fmt.Println() + continue + } + } + fmt.Println() +} + +/* +EXAMPLE BLOCKS + +1 byte +------------------------------------------------------------ +asciiStart = '\u0001' -> 32 +asciiStop = '\u007f' -> 127 + +2 bytes +------------------------------------------------------------ +latin1Start = '\u0080' -> 128 +latin1Stop = '\u00ff' -> 255 + +3 bytes +------------------------------------------------------------ +dingbatStart = '\u2700' -> 9984 +dingbatStop = '\u27bf' -> 10175 + +4 bytes +------------------------------------------------------------ +emojiStart = '\U0001f600' -> 128512 +emojiStop = '\U0001f64f' -> 128591 +transportStart = '\U0001F680' -> 128640 +transportStop = '\U0001f6ff' -> 128767 + +BIG THANK YOU! -> https://unicode-table.com/ +*/ diff --git a/16-strings-revisited/notes.md b/16-strings-revisited/notes.md new file mode 100644 index 0000000..ebc6419 --- /dev/null +++ b/16-strings-revisited/notes.md @@ -0,0 +1,233 @@ +# Strings Revisited + +## Bytes +* ASCII +* Immutable + +## Runes +* Unicode +* vs ASCII +* UTF-8 +* Made up of bytes + +## Slicing +* String: Read-Only Byte Slice +* Slicing -> String +* Index -> Byte + +--- + +## Read-only byte slice +* A string is a read-only slice +* You can't change its data +* Indexable: Returns you a byte +* Slicable: Returns you a string + +## Slicing +* Strings can be sliced just like a slice +* After slicing Go returns you a new string slice +* WARNING: Indexing expression returns you a byte + * s := "hey" + * s[0] + s[1] + s[2] != "hey" + * s[0:3] == "hey + +## Underlying array +* Underlying array is a string array +* There's no capacity this time: Only length and pointer. + +* Sliced string will refer to that array +* String slicing is cheap — They share the same array + +## Unicode +* At the beginning there was only ASCII code standard + * It was using 7-bits to represents 128 characters + * Only English characters + * Each code corresponding to a character + +* After Internet nothing couldn't stay the same +* There was a need to introduce more languages +* 127 characters aren't enough for the entire world + +* So: Unicode is born +* It collects all of the characters in world's languages +* Unicode can represent every character in every imaginable language system. + +* Assigns each character to a codepoint or a rune (in Go) +* Unicode assigns each character a unique number, or code point. + +* Codepoint is a numeric number which represents a character in general +* U+2700 -> hex +* Unicode defines codepoints for 1m+ characters +* It includes the ASCII codes too + + A chinese character: 汉 + Its unicode value: U+6C49 + convert 6C49 to binary: 01101100 01001001 + embed 6C49 as UTF-8: 11100110 10110001 10001001 + + +## Unicode and Runes +* Rune is a 4-bytes type for storing unicode codepoints + +* Rune data type and rune codepoints are different things! + +* There's UTF-32 standard which assigns 4 bytes to each codepoint +* But, that's inefficient, so, instead Go uses a variable encoding standard called UTF-8. It assigns different number of bytes to codepoints. +* UTF-8 has been invented by Rob Pike and Ken Thompson (two of the creators of Go) + +* So, a rune is 1-4 bytes. Uses 1 byte for ASCII (english). +* 2-3 bytes for most of the characters. + +* A string can contain runes +* Each rune can span to multiple bytes +* WARNING: Getting one byte of a string may give you corrupt data + * If you're getting one part of a rune inside the string! + +* In a string with runes, you can't easily index the characters + * You need to use unicode and utf8 packages + * Or you need to convert the string into a rune slice + * unicode: letters vs nums, to uppercase, ... + * utf8 : working w/bytes and runes + +* RuneCountInString(s) == len([]rune(s)) +* DecodeRuneInString(s) returns the first rune + +## Ranging over strings +* You can range over a string like a slice +* It will jump over the runes inside the string +* The index variable will be the starting position of each rune + * And the value will be the rune itself + +## Representing bytes +* Unicode characters can be hard to type in code +* So, we can use \x and \u in a string to represent bytes and runes + +* A string literal is always utf-8 but a string value is not + +## Convenience +* It's easy to work with runes in code: []rune +* However, it will consume more memory: Each char is 4 bytes + +* "inanç"[4] = gibberish + +* r := []rune("inanç") -> five elements rune slice + * r[4] = 'ç' + * string(r) + * // inanç: automatically concatenates the runes to form a string + +* string(105) // i -> interprets 105 as a rune value; 'i' not 105 +* string(351) // ş -> "" + +* printf: %q -> 'ç' %c -> ç %d -> 231 + +## Bytes +* major libs: + * strings, bytes (have corresponding funcs) + * strconv, unicode + * bytes.Buffer + +* []byte can be modified whereas string is immutable + * if you do a lot of string manipulations you can use []byte + +* []byte <-> string convertable + * but, each conversion copies the data + * compiler optimizes it mostly + + * however, do not blindly convert; use bytes pkg + * it's like the string pkg + + * s := "inanc" + * b := []byte(s) + * s := string(b) + +## Sprintf +* Just like printf but instead of printing it returns a string + +## Builders +* bytes.Buffer +* strings.Builder + +* Use WriteRune when adding rune + + +## Terminology: +Summary: Unicode is a large table mapping characters to numbers and the different UTF encodings specify how these numbers are encoded as bits. + +* **ASCII** First character set that maps characters to codepoints or character codes. In terms of alphabets, it only supports basic latin alphabet: English. 2^7=127 + + * The center of the computer industry was in the USA at that time. As a consequence, they didn't need to support accents or other marks such as á, ü, ç, ñ, etc. + + * Once upon a time, computer memory and storage was very expensive. And all of the computers in the world (for practical purposes) were in the hands of English-speaking countries. + + * Single byte encoding only using the bottom 7 bits. Basic Latin. (Unicode code points 0-127.) No accents etc. + +* **Unicode** is a coded character set. A set of characters and a mapping between the characters and integer code points representing them. Unicode is a superset of ASCII. + + * You cannot save text to your hard drive as "Unicode". Unicode is an abstract representation of the text. You need to "encode" this abstract representation. That's where an encoding comes into play. + + * Unicode first and foremost defines a table of code points for characters. That's a fancy way of saying "65 stands for A, 66 stands for B and 9,731 stands for ☃" (seriously, it does). How these code points are actually encoded into bits is a different topic. + +* **UTF-8** is a character encoding - a way of converting from sequences of bytes to sequences of characters and vice versa. It covers the whole of the Unicode character set. + + * UTF-8 uses the ASCII set for the first 128 characters. That's handy because it means ASCII text is also valid in UTF-8. + +* **Character Set:** A character set is a list of characters with unique numbers (these numbers are sometimes referred to as “code points”). For example, in the Unicode character set, the number for A is 41. + +* **Codepoint:** Characters are referred to by their "Unicode code point". + + * Written in hexadecimal (to keep the numbers shorter). + + * Preceded by a "U+" (that's just what they do, it has no other meaning than "this is a Unicode code point"). + + * Unicode itself is a mapping, it defines codepoints and a codepoint is a number, associated with usually a character. + + * Code: a system of words, letters, figures, or other symbols substituted for other words, letters, etc. + +* **Encoding:** Converting data into a coded form. An encoding on the other hand, is an algorithm that translates a list of numbers to binary so it can be stored on disk. For example UTF-8 would translate the number sequence 1, 2, 3, 4 like this: `00000001 00000010 00000011 00000100`. Our data is now translated into binary and can now be saved to disk. + + * To encode means to use something to represent something else. An encoding is the set of rules with which to convert something from one representation to another. + + * To represent 1,114,112 different values, two bytes aren't enough. Three bytes are, but three bytes are often awkward to work with, so four bytes would be the comfortable minimum. But, unless you're actually using Chinese or some of the other characters with big numbers that take a lot of bits to encode, you're never going to use a huge chunk of those four bytes. + + * If the letter "A" was always encoded to 00000000 00000000 00000000 01000001, "B" always to 00000000 00000000 00000000 01000010 and so on, any document would bloat to four times the necessary size. + + * To optimize this, there are several ways to encode Unicode code points into bits. UTF-8 is one of them. + + character encoding bits + A UTF-8 01000001 + A UTF-16 00000000 01000001 + A UTF-32 00000000 00000000 00000000 01000001 + + U+0000 to U+007F are (correctly) encoded with one byte + U+0080 to U+07FF are encoded with 2 bytes + U+0800 to U+FFFF are encoded with 3 bytes + U+010000 to U+10FFFF are encoded with 4 bytes + + * There is NO string or text, without an accompanying encoding standard. + +## REFS: +https://unicode-table.com/en/ + +What's the difference between ASCII and Unicode? +https://stackoverflow.com/a/41198513/115363 + +https://stackoverflow.com/questions/643694/what-is-the-difference-between-utf-8-and-unicode + +https://stackoverflow.com/questions/3951722/whats-the-difference-between-unicode-and-utf-8 + +https://stackoverflow.com/questions/1543613/how-does-utf-8-variable-width-encoding-work + +http://kunststube.net/encoding/ +(detailed and simple) + +http://www.joelonsoftware.com/articles/Unicode.html + +Unicode codepoint to UTF-8 encoding answer: https://stackoverflow.com/a/27939161/115363 + +http://www.polylab.dk/utf8-vs-unicode.html + +Characters, Symbols and the Unicode Miracle - Computerphile +https://www.youtube.com/watch?v=MijmeoH9LT4 + +The history of UTF-8 as told by Rob Pike +http://doc.cat-v.org/bell_labs/utf-8_history \ No newline at end of file diff --git a/17-maps/README.md b/17-maps/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/17-maps/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/18-structs/README.md b/18-structs/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/18-structs/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/19-functions/README.md b/19-functions/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/19-functions/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/20-pointers/README.md b/20-pointers/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/20-pointers/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/21-closures/README.md b/21-closures/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/21-closures/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/22-deferred-funcs/README.md b/22-deferred-funcs/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/22-deferred-funcs/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/23-variadic-funcs/README.md b/23-variadic-funcs/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/23-variadic-funcs/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/24-methods/README.md b/24-methods/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/24-methods/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/25-method-values/README.md b/25-method-values/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/25-method-values/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/26-interfaces/README.md b/26-interfaces/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/26-interfaces/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/27-embedding/README.md b/27-embedding/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/27-embedding/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/28-concurrency/README.md b/28-concurrency/README.md new file mode 100644 index 0000000..58094c8 --- /dev/null +++ b/28-concurrency/README.md @@ -0,0 +1 @@ +This section is in progress. I'm working hard to update the course all the time. Hold on! \ No newline at end of file diff --git a/29-tba/README.md b/29-tba/README.md new file mode 100644 index 0000000..0f138a1 --- /dev/null +++ b/29-tba/README.md @@ -0,0 +1 @@ +I'm going to add more sections after I complete the previous ones. Exciting! \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9a80a9b --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# Learn Go Programming: Complete Bootcamp Course (Golang) +This repository provides the downloadable example programs +for the course, [Learn Go Programming: Complete Bootcamp (Golang)](https://udemy.com/TBA). + +--- + +# Stay in touch + +[Follow me on twitter](https://twitter.com/inancgumus) + +[![@inancgumus](https://img.shields.io/twitter/follow/inancgumus.svg?style=social&label=@inancgumus)](https://twitter.com/inancgumus) + +[Learn Go Programming Blog](http://blog.learngoprogramming.com) + + _(Followed by 3K+ developers)_ + +[Subscribe to my Go emailing list](http://eepurl.com/c4DMNX) + + _(5K+ developers already subscribed!)_ + +--- + +## License +Whole materials are licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
+Creative Commons License diff --git a/Warming Up Exercises.md b/Warming Up Exercises.md new file mode 100644 index 0000000..88cb1ae --- /dev/null +++ b/Warming Up Exercises.md @@ -0,0 +1,39 @@ +1. **Run your own program? Say hello to yourself.** + + 1. Build your program using `go build` + + 2. And, send it to your friend + (s/he should use be using the same operating system) + (if you're using windows, then hers/his should be + windows too) + + 3. And then send your program to a friend with a different + operating system. + + (So, you should compile your program for her operating system). + + **For OSX, type:** + GOOS=darwin GOARCH=386 go build + + **For Windows:** + GOOS=windows GOARCH=386 go build + + **For Linux:** + GOOS=linux GOARCH=arm GOARM=7 go build + + **You can find the full list in here:** + https://golang.org/doc/install/source#environment + +2. **Call Print instead of Println** to see what happens. + +3. **Call Println or Print with multiple values** by separating them using commas. + +4. **Remove double quotes from string literals** and see what happens. + +5. **Move the package and import statement** to the bottom of the file and see what happens. + +6. **Read Go online documentation**. Take a quick look at the packages and read what they do. Look at their source-code by clicking on their titles. + +You don't have to understand anything, just do it. This will warm you up for the upcoming lectures. https://golang.org/pkg + +7. Also, **take a tour**: https://tour.golang.org/ See the language features. We're going to talk all about them soon. \ No newline at end of file diff --git a/first/README.md b/first/README.md new file mode 100644 index 0000000..8377c32 --- /dev/null +++ b/first/README.md @@ -0,0 +1,19 @@ +**The course videos follow this directory.** + +So, you can find the lecture files in the same place as they are explained in the course videos. But then I've moved them into their own directories. This directory is here just for your convenience. + +--- + +# ☢️ USE THE FOLLOWING DIRECTORIES INSTEAD! ☢️ + +**For the first part of the course; only the following directories contains exercises and quizzes.** + +01-get-started/ + +02-write-your-first-program/ + +03-packages-and-scopes/ + +04-statements-expressions-comments/ + +05-write-your-first-library-package/ diff --git a/first/explain/comments/main.go b/first/explain/comments/main.go new file mode 100644 index 0000000..6758349 --- /dev/null +++ b/first/explain/comments/main.go @@ -0,0 +1,21 @@ +// 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 makes this package an executable program +package main + +import "fmt" + +/* +main function +Go executes this program using this function. +There should be only one main file in a main package. +Executable programs are also called as "commands". +*/ +func main() { + fmt.Println("Hello Gopher!") +} diff --git a/first/explain/expressions/01-operator/main.go b/first/explain/expressions/01-operator/main.go new file mode 100644 index 0000000..891740b --- /dev/null +++ b/first/explain/expressions/01-operator/main.go @@ -0,0 +1,16 @@ +// 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() { + fmt.Println("Hello!" + "!") +} diff --git a/first/explain/expressions/02-call-expression/main.go b/first/explain/expressions/02-call-expression/main.go new file mode 100644 index 0000000..98b454d --- /dev/null +++ b/first/explain/expressions/02-call-expression/main.go @@ -0,0 +1,18 @@ +// 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" + "runtime" +) + +func main() { + // runtime.NumCPU() is a call expression + fmt.Println(runtime.NumCPU() + 1) +} diff --git a/first/explain/expressions/README b/first/explain/expressions/README new file mode 100644 index 0000000..6ee4882 --- /dev/null +++ b/first/explain/expressions/README @@ -0,0 +1,3 @@ +I've moved the main.go file for the expressions coding lecture into: + +02-call-expression/ \ No newline at end of file diff --git a/first/explain/importing/01-file-scope/main.go b/first/explain/importing/01-file-scope/main.go new file mode 100644 index 0000000..a480589 --- /dev/null +++ b/first/explain/importing/01-file-scope/main.go @@ -0,0 +1,14 @@ +// 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() { + fmt.Println("Hello!") +} diff --git a/first/explain/importing/02-renaming/main.go b/first/explain/importing/02-renaming/main.go new file mode 100644 index 0000000..d5af349 --- /dev/null +++ b/first/explain/importing/02-renaming/main.go @@ -0,0 +1,16 @@ +// 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" +import f "fmt" + +func main() { + fmt.Println("Hello!") + f.Println("There!") +} diff --git a/first/explain/packages/scopes/bye.go b/first/explain/packages/scopes/bye.go new file mode 100644 index 0000000..4705383 --- /dev/null +++ b/first/explain/packages/scopes/bye.go @@ -0,0 +1,14 @@ +// 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 bye() { + fmt.Println("Bye!") +} diff --git a/first/explain/packages/scopes/hey.go b/first/explain/packages/scopes/hey.go new file mode 100644 index 0000000..9f2b3d9 --- /dev/null +++ b/first/explain/packages/scopes/hey.go @@ -0,0 +1,14 @@ +// 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 hey() { + fmt.Println("Hey!") +} diff --git a/first/explain/packages/scopes/main.go b/first/explain/packages/scopes/main.go new file mode 100644 index 0000000..fc8ede8 --- /dev/null +++ b/first/explain/packages/scopes/main.go @@ -0,0 +1,38 @@ +// 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() { + fmt.Println("Hello!") + + // two files belong to the same package + // calling `bye()` of bye.go here + bye() +} + +// EXERCISE: Remove the comments from the below function +// And analyze the error message + +// func bye() { +// fmt.Println("Bye!") +// } + +// ***** EXPLANATION ***** +// +// ERROR: "bye" function "redeclared" +// in "this block" +// +// "this block" means = "main package" +// +// "redeclared" means = you're using the same name +// in the same scope again +// +// main package's scope is: +// all source-code files which are in the same main package diff --git a/first/explain/packages/what/bye.go b/first/explain/packages/what/bye.go new file mode 100644 index 0000000..4705383 --- /dev/null +++ b/first/explain/packages/what/bye.go @@ -0,0 +1,14 @@ +// 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 bye() { + fmt.Println("Bye!") +} diff --git a/first/explain/packages/what/hey.go b/first/explain/packages/what/hey.go new file mode 100644 index 0000000..9f2b3d9 --- /dev/null +++ b/first/explain/packages/what/hey.go @@ -0,0 +1,14 @@ +// 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 hey() { + fmt.Println("Hey!") +} diff --git a/first/explain/packages/what/main.go b/first/explain/packages/what/main.go new file mode 100644 index 0000000..b2f5ac1 --- /dev/null +++ b/first/explain/packages/what/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" + +func main() { + fmt.Println("Hello!") + + // You can access functions from other files + // which are in the same package + + // Here, `main()` can access `bye()` and `hey()` + + // It's because: bye.go, hey.go and main.go + // are in the main package. + + bye() + hey() +} diff --git a/first/explain/scopes/01-scopes/main.go b/first/explain/scopes/01-scopes/main.go new file mode 100644 index 0000000..9b8fb9b --- /dev/null +++ b/first/explain/scopes/01-scopes/main.go @@ -0,0 +1,24 @@ +// 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 + +// file scope +import "fmt" + +// package scope +const ok = true + +// package scope +func main() { // block scope starts + + var hello = "Hello" + + // hello and ok are visible here + fmt.Println(hello, ok) + +} // block scope ends diff --git a/first/explain/scopes/02-block-scope/main.go b/first/explain/scopes/02-block-scope/main.go new file mode 100644 index 0000000..155ba37 --- /dev/null +++ b/first/explain/scopes/02-block-scope/main.go @@ -0,0 +1,26 @@ +// 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 + +func nope() { // block scope starts + + // hello and ok are only visible here + const ok = true + var hello = "Hello" + + _ = hello +} // block scope ends + +func main() { // block scope starts + + // hello and ok are not visible here + + // ERROR: + // fmt.Println(hello, ok) + +} // block scope ends diff --git a/first/explain/scopes/03-nested-scope/main.go b/first/explain/scopes/03-nested-scope/main.go new file mode 100644 index 0000000..1a3f6ed --- /dev/null +++ b/first/explain/scopes/03-nested-scope/main.go @@ -0,0 +1,39 @@ +// 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" + +// I didn't talk about this in a lecture +// As a side-note, I wanted to put it here +// Please review it. + +var declareMeAgain = 10 + +func nested() { // block scope starts + + // declares the same variable + // they both can exist together + // this one only belongs to this scope + // package's variable is still intact + var declareMeAgain = 5 + fmt.Println("inside nope:", declareMeAgain) + +} // block scope ends + +func main() { // block scope starts + + fmt.Println("inside main:", declareMeAgain) + + nested() + + // package-level declareMeAgain isn't effected + // from the change inside the nested func + fmt.Println("inside main:", declareMeAgain) + +} // block scope ends diff --git a/first/explain/statements/01-execution-flow/main.go b/first/explain/statements/01-execution-flow/main.go new file mode 100644 index 0000000..efa800c --- /dev/null +++ b/first/explain/statements/01-execution-flow/main.go @@ -0,0 +1,22 @@ +// 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() { + fmt.Println("Hello!") + + // Statements change the execution flow + // Especially the control flow statements like `if` + if 5 > 1 { + fmt.Println("bigger") + } +} diff --git a/first/explain/statements/02-semicolons/main.go b/first/explain/statements/02-semicolons/main.go new file mode 100644 index 0000000..605765b --- /dev/null +++ b/first/explain/statements/02-semicolons/main.go @@ -0,0 +1,16 @@ +// 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() { + fmt.Println("Hello"); fmt.Println("World!") +} diff --git a/first/first/exercises/01/main.go b/first/first/exercises/01/main.go new file mode 100644 index 0000000..1820160 --- /dev/null +++ b/first/first/exercises/01/main.go @@ -0,0 +1,27 @@ +// 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 + +// --------------------------------------------------------- +// EXERCISE +// Print your name and your best friend's name using +// Println twice +// +// EXPECTED OUTPUT +// YourName +// YourBestFriendName +// +// BONUS +// Use `go run` first. +// And after that use `go build` and run your program. +// --------------------------------------------------------- + +func main() { + // ? + // ? +} diff --git a/first/first/exercises/01/solution/main.go b/first/first/exercises/01/solution/main.go new file mode 100644 index 0000000..b1aadb3 --- /dev/null +++ b/first/first/exercises/01/solution/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" + +// go run main.go + +// go build +// ./solution + +func main() { + fmt.Println("Nikola") + fmt.Println("Thomas") +} diff --git a/first/first/exercises/02/exercise.md b/first/first/exercises/02/exercise.md new file mode 100644 index 0000000..cb2de95 --- /dev/null +++ b/first/first/exercises/02/exercise.md @@ -0,0 +1,7 @@ +// --------------------------------------------------------- +// EXERCISE +// Print your GOPATH using `go env` tool +// +// EXPECTED OUTPUT +// The physical folder path that is referenced by $GOPATH +// --------------------------------------------------------- \ No newline at end of file diff --git a/first/first/exercises/02/solution/solution.md b/first/first/exercises/02/solution/solution.md new file mode 100644 index 0000000..4176bbd --- /dev/null +++ b/first/first/exercises/02/solution/solution.md @@ -0,0 +1,3 @@ +You should type this: + +go env GOPATH \ No newline at end of file diff --git a/first/first/main.go b/first/first/main.go new file mode 100644 index 0000000..2318c92 --- /dev/null +++ b/first/first/main.go @@ -0,0 +1,59 @@ +// 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 is a special package +// it allows Go to create an executable file +package main + +/* +This is a multi-line comment. + +import keyword makes another package available + for this .go "file". + +import "fmt" lets you access fmt package's functionality + here in this file. +*/ +import "fmt" + +// "func main" is special. +// +// Go has to know where to start +// +// func main creates a starting point for Go +// +// After compiling the code, +// Go runtime will first run this function +func main() { + // after: import "fmt" + // Println function of "fmt" package becomes available + + // Look at what it looks like by typing in the console: + // godoc -src fmt Println + + // Println is just an exported function from + // "fmt" package + + // Exported = First Letter is uppercase + fmt.Println("Hello Gopher!") + + // Go cannot call Println function by itself. + // That's why you need to call it here. + // It only calls `func main` automatically. + + // ----- + + // Go supports Unicode characters in string literals + // And also in source-code: KÖSTEBEK! + + // EXERCISE: Remove the comments from below --> // + // fmt.Println("Merhaba Köstebek!") + + // Unnecessary note: + // "Merhaba Köstebek" means "Hello Gopher" + // in Turkish language +} diff --git a/first/first/questions/01-gopath-questions.md b/first/first/questions/01-gopath-questions.md new file mode 100644 index 0000000..7904067 --- /dev/null +++ b/first/first/questions/01-gopath-questions.md @@ -0,0 +1,19 @@ +## Where you should put your Go source code into? +* Anywhere on my computer +* Under $GOPATH +* Under $GOPATH/src *CORRECT* + +## What $GOPATH means? +* It's a file for Go runtime +* Stores Go source code files and compiled packages +* It's a path for gophers to follow + +## Do you need to set your $GOPATH? +* Yes +* No: It's stored on my desktop +* No: It's stored under my user path *CORRECT* + +## How can you print your $GOPATH? +* Using `ls` command +* Using `go env GOPATH` command *CORRECT* +* Using `go environment` command \ No newline at end of file diff --git a/first/first/questions/02-code-your-first-program-questions.md b/first/first/questions/02-code-your-first-program-questions.md new file mode 100644 index 0000000..0179102 --- /dev/null +++ b/first/first/questions/02-code-your-first-program-questions.md @@ -0,0 +1,151 @@ +## What does the package keyword do in the following program? +```go +package main + +func main() { +} +``` +1. func +2. package *CORRECT* +3. fmt.Println +4. import + +> 1. This keyword is used to declare a new function. +> 2. That's right! package keyword allows you to define which package a Go file belongs to. +> 3. This is not a keyword. It's the Println function of the fmt package. +> 4. This keyword is used to import a package. + + +## Which keyword is used to declare a new function? +* func *CORRECT* +* package +* Println +* import + + +## What is a function? +1. It's like a mini-program. It's a reusable and executable block of code. *CORRECT* +2. It allows Go to execute a program. +3. It allows Go to import a package called function. +4. It prints a message to the console. + +> 2. Go looks for package main and func main to do that. A function doesn't do that on its own. +> 3. `import` keyword does that. +> 4. For example: `fmt.Println` does that. + + +## Do you have to call the main function yourself? +1. Yes, so that, I can execute my program. +2. No, Go calls the main function automatically. *CORRECT* + +> 1. No, you don't need to call the main function. Go automatically executes it. + + +## Do you have to call the other functions yourself? +1. Yes, so that, I can execute that function. *CORRECT* +2. Yes, so that, Go can execute my program. +3. No, Go calls the functions automatically. + +> 1. That's right. You need to call a function yourself. Go won't execute it automatically. Go only calls the main function automatically (and some other functions which you didn't learn about yet). + +> 2. That's only the job of the `func main`. There's only one `func main`. + +> 3. Go doesn't call any function automatically except the main func (and some other functions which you didn't learn about yet). So, except the main func, you need to call the functions yourself. + + +## What does `package main` do? +```go +package main + +func main() { +} +``` +* It controls everything +* It allows you to properly exit from a program +* It allows you to create an executable Go program *CORRECT* + + +## What does `func main` do? +```go +package main + +func main() { +} +``` +1. It contains a package called main +2. Go starts executing your program by using the code inside func main *CORRECT* +3. It prints a message to the console + +> 1. main function doesn't contain a package. +> 2. That's right. Go automatically calls the main function to execute your program. +> 3. It doesn't print anything at least directly. + + +## What does `import "fmt"` do? +```go +package main +import "fmt" + +func main() { + fmt.Println("Hi!") +} +``` +1. It prints "fmt" to the console +2. It defines a new package called "fmt" +3. It imports the `fmt` package; so you can use its functionalities *CORRECT* + +> 1. `fmt.Println` prints a message not the `import "fmt"`. +> 2. `package` keyword does that, not the `import` keyword. +> 3. Yes. For example, after you import the fmt package you can call its Println function to print a message to the console. + + +## What this program does? +```go +package main + +func main() { +} +``` +1. It prints a message to the console +2. It's a correct program but it doesn't print anything *CORRECT* +3. It's an incorrect program + +> 1. It doesn't print a message. To do that you can use fmt.Println function. + +> 2. Yes, it's a correct program but since it doesn't contain fmt.Println it doesn't print anything. + +> 3. It's a correct program. It uses the package keyword and it has a main function. So, this is a valid and an executable Go program. + + +## What does this program print? +```go +package main + +func main() { + fmt.Println(Hi! I want to be a Gopher!) +} +``` +* Hi! I want to be a Gopher! +* It doesn't print anything +* This program is incorrect *CORRECT* + +> 1. It doesn't pass the message to Println wrapped between double-quotes. It should be like: fmt.Println("Hi! I want to be a Gopher") + +> 3. It doesn't import "fmt" package. Also see #1. + + +## What does this program print? +```go +package main +import "fmt" + +func main() { + fmt.Println("Hi there!") +} +``` +* Hi there! *CORRECT* +* fmt +* This program is incorrect; it imports the wrong package or there isn't a function called `Println` + +> 2. import "fmt" imports the `fmt` package; so you can use its functionalities. +> 3. Actually, this program is correct. \ No newline at end of file diff --git a/first/first/questions/03-run-your-first-program-questions.md b/first/first/questions/03-run-your-first-program-questions.md new file mode 100644 index 0000000..f0959ad --- /dev/null +++ b/first/first/questions/03-run-your-first-program-questions.md @@ -0,0 +1,43 @@ +## What's the difference between `go build` and `go run`? +1. `go run` just compiles a program; whereas `go build` both compiles and runs it. +2. `go run` both compiles and runs a program; whereas `go build` just compiles it. *CORRECT* + +> 1. It's opposite actually. +> 2. `go run` compiles your program and puts it in a temporary directory. Then it runs the compiled program in there. + + +## Which directory `go build` puts the compiled code into? +1. The same directory where you call `go build` *CORRECT* +2. $GOPATH/src directory +3. $GOPATH/pkg directory +4. Into a temporary directory. + +> 2. There only lives Go source-code files +> 3. Go only puts your code there when you call `go install`. + + +## Which directory `go run` puts the compiled code into? +1. The same directory where you call `go run` +2. $GOPATH/src directory +3. $GOPATH/pkg directory +4. Into a temporary directory. *CORRECT* + + +## Which one below is true for runtime? +1. It happens when your program starts running in a computer *CORRECT* +2. It happens while your program is being compiled + + +## Which one below is true for the compile-time? +1. It happens when your program starts running in a computer +2. It happens while your program is being compiled *CORRECT* + + +## In which stage your program can print a message to the console? +1. While it's being compiled. +2. While it runs (after compile-time). *CORRECT* +3. While it runs (inside the compile-time). + +> 1. In the compilation step your program cannot print a message. In that stage, it's literally dead. +> 2. That's right. That's the only time which your program can interact with a computer and instruct it to print a message to the console. +> 3. Running can only happen after the compile-time \ No newline at end of file diff --git a/first/printer-exercise/solution/golang/cmd/main.go b/first/printer-exercise/solution/golang/cmd/main.go new file mode 100644 index 0000000..70b0709 --- /dev/null +++ b/first/printer-exercise/solution/golang/cmd/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" + + // You should replace this with your username + // If you remove this, vs code will do that for you + "github.com/inancgumus/learngo/first/printer-exercise/solution/golang" +) + +func main() { + fmt.Println(golang.Version()) +} diff --git a/first/printer-exercise/solution/golang/go.go b/first/printer-exercise/solution/golang/go.go new file mode 100644 index 0000000..360ffbb --- /dev/null +++ b/first/printer-exercise/solution/golang/go.go @@ -0,0 +1,17 @@ +// 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 golang + +import ( + "runtime" +) + +// Version returns the current Go version +func Version() string { + return runtime.Version() +} diff --git a/first/printer/cmd/main.go b/first/printer/cmd/main.go new file mode 100644 index 0000000..42b6f6f --- /dev/null +++ b/first/printer/cmd/main.go @@ -0,0 +1,15 @@ +// 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 + +// Automatically imports!... AWESOME! +import "github.com/inancgumus/learngo/first/printer" + +func main() { + printer.Hello() +} diff --git a/first/printer/printer.go b/first/printer/printer.go new file mode 100644 index 0000000..229213d --- /dev/null +++ b/first/printer/printer.go @@ -0,0 +1,15 @@ +// 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 printer + +import "fmt" + +// Hello is an exported function +func Hello() { + fmt.Println("exported hello") +}