update: foundations
This commit is contained in:
@ -10,13 +10,41 @@ package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// zero-values
|
||||
var (
|
||||
planet = "venus"
|
||||
distance = 261
|
||||
orbital = 224.701
|
||||
hasLife = false
|
||||
planet string
|
||||
distance int
|
||||
orbital float64
|
||||
hasLife bool
|
||||
)
|
||||
|
||||
// assignment
|
||||
planet = "venus"
|
||||
distance = 261
|
||||
orbital = 224.701
|
||||
hasLife = false
|
||||
|
||||
// var (
|
||||
// planet = "venus"
|
||||
// distance = 261
|
||||
// orbital = 224.701
|
||||
// hasLife = false
|
||||
// )
|
||||
|
||||
distance += 5
|
||||
distance *= 2
|
||||
distance++
|
||||
distance--
|
||||
orbital++
|
||||
|
||||
// orbital *= 10
|
||||
const constFactor = 10
|
||||
orbital *= constFactor
|
||||
|
||||
factor := 10
|
||||
// orbital *= factor
|
||||
orbital *= float64(factor)
|
||||
|
||||
// swiss army knife %v verb
|
||||
fmt.Printf("Planet: %v\n", planet)
|
||||
fmt.Printf("Distance: %v millions kms\n", distance)
|
||||
|
@ -16,10 +16,18 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
const usageMsg = `Type a couple of unique numbers.
|
||||
Separate them with spaces.`
|
||||
|
||||
// remember [1:] skips the first argument
|
||||
args := os.Args[1:]
|
||||
if len(args) == 0 {
|
||||
fmt.Println(usageMsg)
|
||||
return
|
||||
}
|
||||
|
||||
main:
|
||||
for _, arg := range os.Args[1:] {
|
||||
for _, arg := range args {
|
||||
n, err := strconv.Atoi(arg)
|
||||
if err != nil {
|
||||
// skip non-numerics
|
||||
|
@ -15,31 +15,21 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
validOps = "%*/+-"
|
||||
|
||||
usageMsg = "Usage: [op=" + validOps + "] [size]"
|
||||
sizeMissingMsg = "Size is missing"
|
||||
validOps = "* / + - mul div add sub"
|
||||
usageMsg = "Usage: [valid ops: " + validOps + "] [size]"
|
||||
sizeMissingMsg = "Size is missing\n" + usageMsg
|
||||
invalidOpMsg = `Invalid operator.
|
||||
Valid ops one of: ` + validOps
|
||||
|
||||
invalidOp = -1
|
||||
)
|
||||
|
||||
func main() {
|
||||
// CHECK THE ARGUMENTS
|
||||
args := os.Args[1:]
|
||||
|
||||
switch l := len(args); {
|
||||
case l == 1:
|
||||
if l := len(args); l == 1 {
|
||||
fmt.Println(sizeMissingMsg)
|
||||
fallthrough
|
||||
case l < 1:
|
||||
fmt.Println(usageMsg)
|
||||
return
|
||||
}
|
||||
|
||||
op := args[0]
|
||||
if strings.IndexAny(op, validOps) == invalidOp {
|
||||
fmt.Println(invalidOpMsg)
|
||||
} else if l < 1 {
|
||||
fmt.Println(usageMsg)
|
||||
return
|
||||
}
|
||||
|
||||
@ -49,12 +39,32 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// CHECK THE VALIDITY OF THE OP
|
||||
op, ops := args[0], strings.Fields(validOps)
|
||||
|
||||
var ok bool
|
||||
for _, o := range ops {
|
||||
if strings.ToLower(o) == op {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !ok {
|
||||
fmt.Println(invalidOpMsg)
|
||||
return
|
||||
}
|
||||
|
||||
// PRINT THE TABLE
|
||||
|
||||
// HEADER
|
||||
fmt.Printf("%5s", op)
|
||||
for i := 0; i <= size; i++ {
|
||||
fmt.Printf("%5d", i)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// CELLS
|
||||
for i := 0; i <= size; i++ {
|
||||
fmt.Printf("%5d", i)
|
||||
|
||||
@ -62,22 +72,24 @@ func main() {
|
||||
var res int
|
||||
|
||||
switch op {
|
||||
case "*":
|
||||
default:
|
||||
fallthrough // default is multiplication
|
||||
case "*", "mul":
|
||||
res = i * j
|
||||
case "%":
|
||||
if j != 0 {
|
||||
res = i % j
|
||||
}
|
||||
case "/":
|
||||
if j != 0 {
|
||||
res = i / j
|
||||
}
|
||||
case "+":
|
||||
case "+", "add":
|
||||
res = i + j
|
||||
case "-":
|
||||
case "-", "sub":
|
||||
res = i - j
|
||||
case "%", "mod":
|
||||
if j == 0 {
|
||||
// continue // will continue the loop
|
||||
break // breaks the switch
|
||||
}
|
||||
res = i % j
|
||||
}
|
||||
|
||||
// break // breaks the loop
|
||||
|
||||
fmt.Printf("%5d", res)
|
||||
}
|
||||
fmt.Println()
|
||||
|
@ -44,7 +44,7 @@ func main() {
|
||||
verbose = true
|
||||
}
|
||||
|
||||
guess, err := strconv.Atoi(args[0])
|
||||
guess, err := strconv.Atoi(args[len(args)-1])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
@ -75,13 +75,36 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
msg := "%s Try again?\n"
|
||||
// msg, n := "%s Try again?\n", rand.Intn(5)
|
||||
// if msg, n := "%s Try again?\n", rand.Intn(5); n <= 2 {
|
||||
// fmt.Printf(msg, "☠️ YOU LOST...")
|
||||
// } else if n < 3 {
|
||||
// fmt.Printf(msg, "☠️ JUST A BAD LUCK...")
|
||||
// } else if n == 4 {
|
||||
// fmt.Printf(msg, "☠️ TRY NEXT TIME...")
|
||||
// }
|
||||
|
||||
switch rand.Intn(2) {
|
||||
case 0:
|
||||
fmt.Printf(msg, "☠️ YOU LOST...")
|
||||
case 1:
|
||||
fmt.Printf(msg, "☠️ JUST A BAD LUCK...")
|
||||
// var msg string
|
||||
// switch rand.Intn(10) {
|
||||
// // more probability
|
||||
// case 0, 1, 2, 3, 4, 5:
|
||||
// msg = "☠️ YOU LOST..."
|
||||
// case 6, 7, 8:
|
||||
// msg = "☠️ JUST A BAD LUCK..."
|
||||
// default:
|
||||
// msg = "☠️ TRY NEXT TIME..."
|
||||
// }
|
||||
// fmt.Printf("%s Try again?\n", msg)
|
||||
|
||||
var msg string
|
||||
switch n := rand.Intn(10); {
|
||||
// more probability
|
||||
case n <= 5:
|
||||
msg = "☠️ YOU LOST..."
|
||||
case n <= 8:
|
||||
msg = "☠️ JUST A BAD LUCK..."
|
||||
default:
|
||||
msg = "☠️ TRY NEXT TIME..."
|
||||
}
|
||||
|
||||
fmt.Printf("%s Try again?\n", msg)
|
||||
}
|
||||
|
@ -32,9 +32,11 @@ func main() {
|
||||
for i, w := range words {
|
||||
q, w = strings.ToLower(q), strings.ToLower(w)
|
||||
|
||||
if strings.Contains(w, q) {
|
||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||
if !strings.Contains(w, q) {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
29
x-tba/foundations/area-of-a-circle/main.go
Normal file
29
x-tba/foundations/area-of-a-circle/main.go
Normal file
@ -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)
|
||||
}
|
20
x-tba/foundations/cels-to-fahr/main.go
Normal file
20
x-tba/foundations/cels-to-fahr/main.go
Normal file
@ -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)
|
||||
}
|
74
x-tba/foundations/counter/main.go
Normal file
74
x-tba/foundations/counter/main.go
Normal file
@ -0,0 +1,74 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// var counter int
|
||||
// var factor float64
|
||||
var (
|
||||
counter int
|
||||
factor float64
|
||||
)
|
||||
|
||||
// counter = counter + 1
|
||||
counter++
|
||||
fmt.Println(counter)
|
||||
|
||||
// counter = counter - 1
|
||||
counter--
|
||||
fmt.Println(counter)
|
||||
|
||||
// counter = counter + 5
|
||||
counter += 5
|
||||
fmt.Println(counter)
|
||||
|
||||
// counter = counter * 10
|
||||
counter *= 10
|
||||
fmt.Println(counter)
|
||||
|
||||
// counter = counter / 2.0
|
||||
counter /= 2.0
|
||||
fmt.Println(counter)
|
||||
|
||||
factor += float64(counter)
|
||||
fmt.Println(counter)
|
||||
|
||||
var bigCounter int64
|
||||
counter += int(bigCounter)
|
||||
fmt.Println(counter)
|
||||
|
||||
fmt.Println(
|
||||
"hello" + ", " + "how" + " " + "are" + " " + "today?",
|
||||
)
|
||||
|
||||
// you can combine raw string and string literals
|
||||
fmt.Println(
|
||||
`hello` + `, ` + `how` + ` ` + `are` + ` ` + "today?",
|
||||
)
|
||||
|
||||
// ------------------------------------------
|
||||
// Converting non-string values into string
|
||||
// ------------------------------------------
|
||||
|
||||
eq := "1 + 2 = "
|
||||
sum := 1 + 2
|
||||
|
||||
// invalid op
|
||||
// string concat op can only be used with strings
|
||||
// fmt.Println(eq + sum)
|
||||
|
||||
// you need to convert it using strconv.Itoa
|
||||
// Itoa = Integer to ASCII
|
||||
|
||||
fmt.Println(eq + strconv.Itoa(sum))
|
||||
}
|
18
x-tba/foundations/feet-to-meters/main.go
Normal file
18
x-tba/foundations/feet-to-meters/main.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c, _ := strconv.ParseFloat(os.Args[1], 64)
|
||||
f := c*1.8 + 32
|
||||
|
||||
// Like this:
|
||||
fmt.Printf("%g ºC is %g ºF\n", c, f)
|
||||
|
||||
// Or just like this (both are correct):
|
||||
fmt.Printf("%g ºF\n", f)
|
||||
}
|
25
x-tba/foundations/volume-of-a-sphere/main.go
Normal file
25
x-tba/foundations/volume-of-a-sphere/main.go
Normal file
@ -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)
|
||||
}
|
Reference in New Issue
Block a user