diff --git a/14-arrays/01-whats-an-array/main.go b/14-arrays/01-whats-an-array/main.go index 91d6b49..920a74c 100644 --- a/14-arrays/01-whats-an-array/main.go +++ b/14-arrays/01-whats-an-array/main.go @@ -1,3 +1,10 @@ +// 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" diff --git a/14-arrays/02-examples-1-hipsters-love-bookstore/main.go b/14-arrays/02-examples-1-hipsters-love-bookstore/main.go index 64dbc70..5cd655c 100644 --- a/14-arrays/02-examples-1-hipsters-love-bookstore/main.go +++ b/14-arrays/02-examples-1-hipsters-love-bookstore/main.go @@ -1,3 +1,10 @@ +// 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 // STORY: diff --git a/14-arrays/03-examples-2-hipsters-love-bookstore/main.go b/14-arrays/03-examples-2-hipsters-love-bookstore/main.go index 2a39ad1..dfda36d 100644 --- a/14-arrays/03-examples-2-hipsters-love-bookstore/main.go +++ b/14-arrays/03-examples-2-hipsters-love-bookstore/main.go @@ -1,3 +1,10 @@ +// 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" diff --git a/14-arrays/04-array-literal/main.go b/14-arrays/04-array-literal/main.go index 85bffad..b330c8e 100644 --- a/14-arrays/04-array-literal/main.go +++ b/14-arrays/04-array-literal/main.go @@ -1,3 +1,10 @@ +// 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" @@ -19,6 +26,7 @@ const ( func main() { // ALTERNATIVE: // Use this only when you don't know about the elements beforehand + // // var books [yearly]string books := [yearly]string{ @@ -28,30 +36,4 @@ func main() { "Kafka's Revenge 2nd Edition", } fmt.Printf("books : %#v\n", books) - - // var ( - // wBooks [winter]string - // sBooks [summer]string - // ) - - // wBooks[0] = books[0] - - // for i := range sBooks { - // sBooks[i] = books[i+1] - // } - - // fmt.Printf("\nwinter : %#v\n", wBooks) - // fmt.Printf("\nsummer : %#v\n", sBooks) - - // var published [len(books)]bool - - // published[0] = true - // published[len(books)-1] = true - - // fmt.Println("\nPublished Books:") - // for i, ok := range published { - // if ok { - // fmt.Printf("+ %s\n", books[i]) - // } - // } } diff --git a/14-arrays/05-compare/main.go b/14-arrays/05-compare/main.go index b905e0a..07cb7fa 100644 --- a/14-arrays/05-compare/main.go +++ b/14-arrays/05-compare/main.go @@ -1,3 +1,10 @@ +// 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" diff --git a/14-arrays/06-compare-unnamed/main.go b/14-arrays/06-compare-unnamed/main.go index 5722aa4..84d4f5b 100644 --- a/14-arrays/06-compare-unnamed/main.go +++ b/14-arrays/06-compare-unnamed/main.go @@ -1,3 +1,10 @@ +// 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" diff --git a/14-arrays/07-assignment/main.go b/14-arrays/07-assignment/main.go new file mode 100644 index 0000000..a9e183d --- /dev/null +++ b/14-arrays/07-assignment/main.go @@ -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() { + prev := [3]string{"Kafka's Revenge", "Stay Golden", "Everythingship"} + + // You can't do this: + // books = prev + + var books [4]string + + for i, b := range prev { + books[i] += b + " 2nd Ed." + } + + // copying arrays using slices + // copy(books[:], prev[:]) + + books[3] = "Awesomeness" + + fmt.Printf("last year:\n%#v\n", prev) + fmt.Printf("\nthis year:\n%#v\n", books) +} diff --git a/14-arrays/08-multi-dimensional/main.go b/14-arrays/08-multi-dimensional/main.go new file mode 100644 index 0000000..b538d2c --- /dev/null +++ b/14-arrays/08-multi-dimensional/main.go @@ -0,0 +1,64 @@ +// 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() { + // ------------------------------------ + // #1 - THE BEST WAY + // ------------------------------------ + + students := [...][3]float64{ + {5, 6, 1}, + {9, 8, 4}, + } + + var avg float64 + + for _, grades := range students { + for _, grade := range grades { + avg += grade + } + } + + const N = float64(len(students) * len(students[0])) + fmt.Printf("Avg Grade: %g\n", avg/N) + + // ------------------------------------ + // #2 - SO SO WAY + // ------------------------------------ + + // // You don't need to define the types for the inner arrays + // students := [2][3]float64{ + // [3]float64{5, 6, 1}, + // [3]float64{9, 8, 4}, + // } + + // var avg float64 + + // avg += students[0][0] + students[0][1] + students[0][2] + // avg += students[1][0] + students[1][1] + students[1][2] + + // const N = float64(len(students) * len(students[0])) + // fmt.Printf("Avg Grade: %g\n", avg/N) + + // ------------------------------------ + // #3 - MANUAL WAY + // ------------------------------------ + + // student1 := [3]float64{5, 6, 1} + // student2 := [3]float64{9, 8, 4} + + // var avg float64 + // avg += student1[0] + student1[1] + student1[2] + // avg += student2[0] + student2[1] + student2[2] + + // const N = float64(len(student1) * 2) + // fmt.Printf("Avg Grade: %g\n", avg/N) +} diff --git a/14-arrays/09-keyed-elements/01-unkeyed/main.go b/14-arrays/09-keyed-elements/01-unkeyed/main.go new file mode 100644 index 0000000..f04c477 --- /dev/null +++ b/14-arrays/09-keyed-elements/01-unkeyed/main.go @@ -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) +} diff --git a/14-arrays/09-keyed-elements/02-keyed/main.go b/14-arrays/09-keyed-elements/02-keyed/main.go new file mode 100644 index 0000000..e74f912 --- /dev/null +++ b/14-arrays/09-keyed-elements/02-keyed/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" + +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, + // } +} diff --git a/14-arrays/09-keyed-elements/03-keyed-order/main.go b/14-arrays/09-keyed-elements/03-keyed-order/main.go new file mode 100644 index 0000000..122d389 --- /dev/null +++ b/14-arrays/09-keyed-elements/03-keyed-order/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" + +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, + // } +} diff --git a/14-arrays/09-keyed-elements/04-keyed-auto-initialize/main.go b/14-arrays/09-keyed-elements/04-keyed-auto-initialize/main.go new file mode 100644 index 0000000..5fa9b54 --- /dev/null +++ b/14-arrays/09-keyed-elements/04-keyed-auto-initialize/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" + +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, + // } +} diff --git a/14-arrays/09-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go b/14-arrays/09-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go new file mode 100644 index 0000000..a51b1fc --- /dev/null +++ b/14-arrays/09-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go @@ -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, + // } +} diff --git a/14-arrays/09-keyed-elements/06-keyed-and-unkeyed/main.go b/14-arrays/09-keyed-elements/06-keyed-and-unkeyed/main.go new file mode 100644 index 0000000..76839a7 --- /dev/null +++ b/14-arrays/09-keyed-elements/06-keyed-and-unkeyed/main.go @@ -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, + // } +} diff --git a/14-arrays/_experimental/_14-arrays/11-project-xratio/01-without-keys/main.go b/14-arrays/09-keyed-elements/07-xratio-example/01-without-keys/main.go similarity index 100% rename from 14-arrays/_experimental/_14-arrays/11-project-xratio/01-without-keys/main.go rename to 14-arrays/09-keyed-elements/07-xratio-example/01-without-keys/main.go diff --git a/14-arrays/_experimental/_14-arrays/11-project-xratio/02-with-keys/main.go b/14-arrays/09-keyed-elements/07-xratio-example/02-with-keys/main.go similarity index 65% rename from 14-arrays/_experimental/_14-arrays/11-project-xratio/02-with-keys/main.go rename to 14-arrays/09-keyed-elements/07-xratio-example/02-with-keys/main.go index 91759e3..b58106d 100644 --- a/14-arrays/_experimental/_14-arrays/11-project-xratio/02-with-keys/main.go +++ b/14-arrays/09-keyed-elements/07-xratio-example/02-with-keys/main.go @@ -11,20 +11,28 @@ import "fmt" // REFACTORED VERSION // It uses well-defined names instead of magic numbers. -// Thanks to keyed elements and constants. +// Thanks to the keyed elements and constants. func main() { const ( - ETH = iota + ETH = 9 - iota WAN + ICX + // you can add more cryptocurrencies here + // watch out the -1 index though! ) rates := [...]float64{ ETH: 25.5, WAN: 120.5, + ICX: 20, + // you can add more cryptocurrencies here } // 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]) + fmt.Printf("1 BTC is %g ICX\n", rates[ICX]) + + fmt.Printf("%#v\n", rates) } diff --git a/14-arrays/_experimental/11-avg/main.go b/14-arrays/_experimental/11-avg/main.go index f322a8e..e395195 100644 --- a/14-arrays/_experimental/11-avg/main.go +++ b/14-arrays/_experimental/11-avg/main.go @@ -1,3 +1,10 @@ +// 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 ( diff --git a/14-arrays/_experimental/11-avg/solution-slices/main.go b/14-arrays/_experimental/11-avg/solution-slices/main.go index 4bf06b7..43ede91 100644 --- a/14-arrays/_experimental/11-avg/solution-slices/main.go +++ b/14-arrays/_experimental/11-avg/solution-slices/main.go @@ -1,3 +1,10 @@ +// 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 ( diff --git a/14-arrays/_experimental/11-avg/solution/main.go b/14-arrays/_experimental/11-avg/solution/main.go index 8fde9d0..94076f6 100644 --- a/14-arrays/_experimental/11-avg/solution/main.go +++ b/14-arrays/_experimental/11-avg/solution/main.go @@ -1,3 +1,10 @@ +// 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 ( diff --git a/14-arrays/_experimental/11-sort/main.go b/14-arrays/_experimental/11-sort/main.go index 5c34e31..40d1b3a 100644 --- a/14-arrays/_experimental/11-sort/main.go +++ b/14-arrays/_experimental/11-sort/main.go @@ -1,3 +1,10 @@ +// 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 ( diff --git a/14-arrays/_experimental/11-word-finder/main.go b/14-arrays/_experimental/11-word-finder/main.go index ec6c0b3..38dc06f 100644 --- a/14-arrays/_experimental/11-word-finder/main.go +++ b/14-arrays/_experimental/11-word-finder/main.go @@ -1,3 +1,10 @@ +// 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 ( diff --git a/14-arrays/_experimental/__main.go b/14-arrays/_experimental/__main.go index 7d05c98..12125e9 100644 --- a/14-arrays/_experimental/__main.go +++ b/14-arrays/_experimental/__main.go @@ -1,3 +1,10 @@ +// 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/ +// + const ( Mon Weekday = iota Tue diff --git a/14-arrays/_experimental/addr/main.go b/14-arrays/_experimental/addr/main.go index c260f81..d863b16 100644 --- a/14-arrays/_experimental/addr/main.go +++ b/14-arrays/_experimental/addr/main.go @@ -1,3 +1,10 @@ +// 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" diff --git a/14-arrays/_experimental/csv-writer-slices/main.go b/14-arrays/_experimental/csv-writer-slices/main.go index 68ea314..2fdcc63 100644 --- a/14-arrays/_experimental/csv-writer-slices/main.go +++ b/14-arrays/_experimental/csv-writer-slices/main.go @@ -1,3 +1,10 @@ +// 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 ( diff --git a/14-arrays/_experimental/wall-clock/main.go b/14-arrays/_experimental/wall-clock/main.go index 3f1c58f..76098cc 100644 --- a/14-arrays/_experimental/wall-clock/main.go +++ b/14-arrays/_experimental/wall-clock/main.go @@ -1,3 +1,10 @@ +// 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 ( diff --git a/14-arrays/exercises/00-name/main.go b/14-arrays/exercises/00-name/main.go index 7dd4f79..bbf0173 100644 --- a/14-arrays/exercises/00-name/main.go +++ b/14-arrays/exercises/00-name/main.go @@ -1,3 +1,10 @@ +// 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 // --------------------------------------------------------- diff --git a/14-arrays/exercises/00-name/solution/main.go b/14-arrays/exercises/00-name/solution/main.go index da29a2c..b5e729e 100644 --- a/14-arrays/exercises/00-name/solution/main.go +++ b/14-arrays/exercises/00-name/solution/main.go @@ -1,3 +1,10 @@ +// 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() { diff --git a/14-arrays/exercises/01-declare-empty/main.go b/14-arrays/exercises/01-declare-empty/main.go index 56b756a..9d7d9d1 100644 --- a/14-arrays/exercises/01-declare-empty/main.go +++ b/14-arrays/exercises/01-declare-empty/main.go @@ -1,3 +1,10 @@ +// 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 // --------------------------------------------------------- diff --git a/14-arrays/exercises/01-declare-empty/solution/main.go b/14-arrays/exercises/01-declare-empty/solution/main.go index 338d083..a8637e9 100644 --- a/14-arrays/exercises/01-declare-empty/solution/main.go +++ b/14-arrays/exercises/01-declare-empty/solution/main.go @@ -1,3 +1,10 @@ +// 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" diff --git a/14-arrays/exercises/0x-hipsters-love-search/main.go b/14-arrays/exercises/0x-hipsters-love-search/main.go index f2b2aa4..356105b 100644 --- a/14-arrays/exercises/0x-hipsters-love-search/main.go +++ b/14-arrays/exercises/0x-hipsters-love-search/main.go @@ -1,3 +1,10 @@ +// 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 // search for books in hipster's love diff --git a/14-arrays/exercises/0x-hipsters-love-search/solution/main.go b/14-arrays/exercises/0x-hipsters-love-search/solution/main.go index 6542793..8e0a074 100644 --- a/14-arrays/exercises/0x-hipsters-love-search/solution/main.go +++ b/14-arrays/exercises/0x-hipsters-love-search/solution/main.go @@ -1,3 +1,10 @@ +// 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 ( diff --git a/14-arrays/xx-examples-3-hipsters-love-bookstore/main.go b/14-arrays/xx-examples-3-hipsters-love-bookstore/main.go index 0fc1386..ced96f1 100644 --- a/14-arrays/xx-examples-3-hipsters-love-bookstore/main.go +++ b/14-arrays/xx-examples-3-hipsters-love-bookstore/main.go @@ -1,3 +1,10 @@ +// 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" diff --git a/14-arrays/xx-examples-4-hipsters-love-bookstore/main.go b/14-arrays/xx-examples-4-hipsters-love-bookstore/main.go index 0b8c908..3a0df48 100644 --- a/14-arrays/xx-examples-4-hipsters-love-bookstore/main.go +++ b/14-arrays/xx-examples-4-hipsters-love-bookstore/main.go @@ -1,3 +1,10 @@ +// 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" diff --git a/main.go b/main.go index edc536c..7029026 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,10 @@ +// 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"