From 0c88436ce7e29b6b8496760f796967d2221c0df8 Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Sat, 11 May 2019 13:22:43 +0300 Subject: [PATCH] add: x-tba/foundations --- .../01-print-args/01-printf/main.go | 51 +++++++++++ x-tba/foundations/01-print-args/main.go | 28 ++++++ .../02-variables/01-basics/main.go | 43 +++++++++ .../02-variables/02-short-discard/main.go | 35 ++++++++ .../02-variables/03-conversion/main.go | 30 +++++++ .../01-for-crunch-the-primes/main.go | 55 ++++++++++++ .../02-switch-months/main.go | 44 ++++++++++ .../03-math-table-if-switch-loop/main.go | 85 ++++++++++++++++++ .../04-lucky-number-if-for-switch/main.go | 87 +++++++++++++++++++ .../05-path-searcher-for-range/main.go | 40 +++++++++ 10 files changed, 498 insertions(+) create mode 100644 x-tba/foundations/01-print-args/01-printf/main.go create mode 100644 x-tba/foundations/01-print-args/main.go create mode 100644 x-tba/foundations/02-variables/01-basics/main.go create mode 100644 x-tba/foundations/02-variables/02-short-discard/main.go create mode 100644 x-tba/foundations/02-variables/03-conversion/main.go create mode 100644 x-tba/foundations/03-if-switch-loop/01-for-crunch-the-primes/main.go create mode 100644 x-tba/foundations/03-if-switch-loop/02-switch-months/main.go create mode 100644 x-tba/foundations/03-if-switch-loop/03-math-table-if-switch-loop/main.go create mode 100644 x-tba/foundations/03-if-switch-loop/04-lucky-number-if-for-switch/main.go create mode 100644 x-tba/foundations/03-if-switch-loop/05-path-searcher-for-range/main.go diff --git a/x-tba/foundations/01-print-args/01-printf/main.go b/x-tba/foundations/01-print-args/01-printf/main.go new file mode 100644 index 0000000..341112c --- /dev/null +++ b/x-tba/foundations/01-print-args/01-printf/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/x-tba/foundations/01-print-args/main.go b/x-tba/foundations/01-print-args/main.go new file mode 100644 index 0000000..b182803 --- /dev/null +++ b/x-tba/foundations/01-print-args/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() { + 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.") + + path := `c:\program files\duper super\fun.txt +c:\program files\really\funny.png` + + fmt.Println(path) +} diff --git a/x-tba/foundations/02-variables/01-basics/main.go b/x-tba/foundations/02-variables/01-basics/main.go new file mode 100644 index 0000000..1ed170d --- /dev/null +++ b/x-tba/foundations/02-variables/01-basics/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" +) + +func main() { + // var ( + // name string + // name2 string + // name3 string + // ) + + // var name, name2, name3 string + // name = os.Args[1] + // name2 = os.Args[2] + // name3 = os.Args[3] + + // name := os.Args[1] + // name2 := os.Args[2] + // name3 := os.Args[3] + + name, name2, name3 := os.Args[1], os.Args[2], 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/x-tba/foundations/02-variables/02-short-discard/main.go b/x-tba/foundations/02-variables/02-short-discard/main.go new file mode 100644 index 0000000..0502f9c --- /dev/null +++ b/x-tba/foundations/02-variables/02-short-discard/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" + "path" + "strconv" +) + +func main() { + var radius, area float64 + + radius, _ = strconv.ParseFloat(os.Args[1], 64) + + area = 4 * math.Pi * math.Pow(radius, 2) + // area := 4 * math.Pi * math.Pow(radius, 2) + + fmt.Printf("radius: %g -> area: %.2f\n", + radius, area) + + dir, _ := path.Split("secret/file.txt") + fmt.Println(dir) + + color, color2 := "red", "blue" + color, color2 = color2, color + fmt.Println(color, color2) +} diff --git a/x-tba/foundations/02-variables/03-conversion/main.go b/x-tba/foundations/02-variables/03-conversion/main.go new file mode 100644 index 0000000..78ed39f --- /dev/null +++ b/x-tba/foundations/02-variables/03-conversion/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/x-tba/foundations/03-if-switch-loop/01-for-crunch-the-primes/main.go b/x-tba/foundations/03-if-switch-loop/01-for-crunch-the-primes/main.go new file mode 100644 index 0000000..1c6fbc5 --- /dev/null +++ b/x-tba/foundations/03-if-switch-loop/01-for-crunch-the-primes/main.go @@ -0,0 +1,55 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +// go run . {1..100} + +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 { + // prime + case n == 2 || n == 3: + fmt.Print(n, " ") + continue + + // not a prime + case n <= 1 || n%2 == 0 || n%3 == 0: + 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/x-tba/foundations/03-if-switch-loop/02-switch-months/main.go b/x-tba/foundations/03-if-switch-loop/02-switch-months/main.go new file mode 100644 index 0000000..79f207e --- /dev/null +++ b/x-tba/foundations/03-if-switch-loop/02-switch-months/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" + "strings" + "time" +) + +func main() { + if len(os.Args) != 2 { + fmt.Println("Give me a month name") + return + } + + year := time.Now().Year() + leap := year%4 == 0 && (year%100 != 0 || year%400 == 0) + + days, month := 28, os.Args[1] + + switch strings.ToLower(month) { + case "april", "june", "september", "november": + days = 30 + case "january", "march", "may", "july", + "august", "october", "december": + days = 31 + case "february": + if leap { + days = 29 + } + default: + fmt.Printf("%q is not a month.\n", month) + return + } + + fmt.Printf("%q has %d days.\n", month, days) +} diff --git a/x-tba/foundations/03-if-switch-loop/03-math-table-if-switch-loop/main.go b/x-tba/foundations/03-if-switch-loop/03-math-table-if-switch-loop/main.go new file mode 100644 index 0000000..3175f8b --- /dev/null +++ b/x-tba/foundations/03-if-switch-loop/03-math-table-if-switch-loop/main.go @@ -0,0 +1,85 @@ +// For more tutorials: https://blog.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 ( + validOps = "%*/+-" + + usageMsg = "Usage: [op=" + validOps + "] [size]" + sizeMissingMsg = "Size is missing" + invalidOpMsg = `Invalid operator. +Valid ops one of: ` + validOps + + invalidOp = -1 +) + +func main() { + args := os.Args[1:] + + switch l := len(args); { + case l == 1: + fmt.Println(sizeMissingMsg) + fallthrough + case l < 1: + fmt.Println(usageMsg) + return + } + + op := args[0] + if strings.IndexAny(op, validOps) == invalidOp { + fmt.Println(invalidOpMsg) + return + } + + size, err := strconv.Atoi(args[1]) + if err != nil || size <= 0 { + fmt.Println("Wrong size") + return + } + + fmt.Printf("%5s", op) + for i := 0; i <= size; i++ { + fmt.Printf("%5d", i) + } + fmt.Println() + + for i := 0; i <= size; i++ { + fmt.Printf("%5d", i) + + for j := 0; j <= size; j++ { + var res int + + switch op { + case "*": + res = i * j + case "%": + if j != 0 { + res = i % j + } + case "/": + if j != 0 { + res = i / j + } + case "+": + res = i + j + case "-": + res = i - j + } + + fmt.Printf("%5d", res) + } + fmt.Println() + } +} diff --git a/x-tba/foundations/03-if-switch-loop/04-lucky-number-if-for-switch/main.go b/x-tba/foundations/03-if-switch-loop/04-lucky-number-if-for-switch/main.go new file mode 100644 index 0000000..fdeaecb --- /dev/null +++ b/x-tba/foundations/03-if-switch-loop/04-lucky-number-if-for-switch/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" + "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[0]) + if err != nil { + fmt.Println("Not a number.") + return + } + + if guess < 0 { + fmt.Println("Please pick a positive number.") + return + } + + for turn := 0; turn < maxTurns; turn++ { + n := rand.Intn(guess + 1) + + if verbose { + fmt.Printf("%d ", n) + } + + 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/x-tba/foundations/03-if-switch-loop/05-path-searcher-for-range/main.go b/x-tba/foundations/03-if-switch-loop/05-path-searcher-for-range/main.go new file mode 100644 index 0000000..10fdaed --- /dev/null +++ b/x-tba/foundations/03-if-switch-loop/05-path-searcher-for-range/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" + "path/filepath" + "strings" +) + +func main() { + // Get and split the PATH environment variable + + // SplitList function automatically finds the + // separator for the path env variable + words := filepath.SplitList(os.Getenv("PATH")) + + // Alternative way, but above one is better: + // words := strings.Split( + // os.Getenv("PATH"), + // string(os.PathListSeparator)) + + query := os.Args[1:] + + for _, q := range query { + for i, w := range words { + q, w = strings.ToLower(q), strings.ToLower(w) + + if strings.Contains(w, q) { + fmt.Printf("#%-2d: %q\n", i+1, w) + } + } + } +}