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

View File

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

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() {
ages := [3]int{15, 30, 25}
for _, age := range ages {
fmt.Println(age)
}
}

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
func main() {
// uncomment the code below to observe the error
// ages := [3]int{15, 30, 25, 44}
// for _, age := range ages {
// fmt.Println(age)
// }
}

View File

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

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

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() {
ages := [...]int{15, 30, 25}
// equals to:
// ages := [3]int{15, 30, 25}
for _, age := range ages {
fmt.Println(age)
}
}

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

View File

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

View File

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

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() {
names := [...]string{
"lina", "bob", "jane",
}
fmt.Printf("%#v\n", names)
}

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() {
names := [5]string{
"lina", "bob", "jane",
}
fmt.Printf("%#v\n", names)
}

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

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

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

View File

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

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

View File

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

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

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

View File

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

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

View File

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

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

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

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

View File

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

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() {
rates := [3]float64{
0.5,
2.5,
1.5,
}
fmt.Println(rates)
}

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"
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,
// }
}

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"
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,
// }
}

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"
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,
// }
}

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
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,
// }
}

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

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

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

View File

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

View File

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

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

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

View File

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