add: x-tba/foundations

This commit is contained in:
Inanc Gumus
2019-05-11 13:22:43 +03:00
parent 9a8a1ee0bb
commit 0c88436ce7
10 changed files with 498 additions and 0 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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!")
}

View File

@ -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)
}

View File

@ -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.
}

View File

@ -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()
}

View File

@ -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)
}

View File

@ -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()
}
}

View File

@ -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...")
}
}

View File

@ -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)
}
}
}
}