Initial commit

This commit is contained in:
Inanc Gumus
2018-10-13 23:30:21 +03:00
commit cde4e6632c
567 changed files with 17896 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View 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() {
// 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)
}

View File

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

View 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() {
// 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)
}

View File

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

View File

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

View File

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

View File

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

View 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() {
score, valid := 3, true
if score > 3 && valid {
fmt.Println("good")
} else {
fmt.Println("low")
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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

View File

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

View File

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

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

View File

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

View File

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

View File

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

View File

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

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

View File

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

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

View File

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

View File

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

View File

@@ -0,0 +1,3 @@
## ?
* text *CORRECT*
* text