diff --git a/14-arrays/01-whats-an-array/main.go b/14-arrays/01-whats-an-array/main.go index 920a74c..c2f55b4 100644 --- a/14-arrays/01-whats-an-array/main.go +++ b/14-arrays/01-whats-an-array/main.go @@ -14,19 +14,89 @@ func main() { myAge byte herAge byte - // uncomment the code below and observe the error - // wrongDeclaration [-1]byte + // this declares an array with two byte elements + // its length : 2 + // its element type: byte + ages [2]byte - zero [0]byte - ages [2]byte + // this declares an array with five string elements + // its length : 5 + // its element type: string + tags [5]string + + // this array doesn't occupy any memory space (its length is zero) + // its length : 0 + // its element type: byte + zero [0]byte + + // this array uses a constant expression + // its length : 3 + // its element type: byte agesExp [2 + 1]byte - tags [5]string + // uncomment the code below and observe the error + // + // wrongDeclaration [-1]byte ) - fmt.Printf("%d %d\n", myAge, herAge) - fmt.Printf("%#v\n", zero) - fmt.Printf("%#v\n", ages) - fmt.Printf("%#v\n", agesExp) - fmt.Printf("%#v\n", tags) + fmt.Printf("myAge : %d\n", myAge) + fmt.Printf("herAge : %d\n", herAge) + + // Since arrays we've declared here don't have any elements, + // Go automatically sets all their elements to their zero values + // depending on their element type. + + // #v verb prints the array's length, element type and its elements + fmt.Printf("ages : %#v\n", ages) + fmt.Printf("tags : %#v\n", tags) + fmt.Printf("zero : %#v\n", zero) + fmt.Printf("agesExp : %#v\n", agesExp) + + // note: + // ages and agesExp get printed 0x0 because they're byte arrays. + // bytes are represented with hex values. 0x0 means 0. + + // ========================================================================= + // GETTING AND SETTING ARRAY ELEMENTS + // ========================================================================= + + // Note: + // + // Since, I've already declared the ages variable above, + // and, to show you the example below, I needed to create a new block. + // + // ages variable below is in a new block below. So, it's a new variable. + // + // I did so because I need to change the element type of the ages array + // to int (or, subtracting from a byte results in wraparound). + { + var ages [2]int + + fmt.Println() + fmt.Printf("ages : %#v\n", ages) + fmt.Printf("ages's type : %T\n", ages) + + fmt.Println("len(ages) :", len(ages)) + fmt.Println("ages[0] :", ages[0]) + fmt.Println("ages[1] :", ages[1]) + fmt.Println("ages[len(ages)-1] :", ages[len(ages)-1]) + + // WRONG: + // fmt.Println("ages[-1] :", ages[-1]) + // fmt.Println("ages[2] :", ages[2]) + // fmt.Println("ages[len(ages)] :", ages[len(ages)]) + + ages[0] = 6 + ages[1] -= 3 + + // WRONG: + // ages[0] = "Can I?" + + fmt.Println("ages[0] :", ages[0]) + fmt.Println("ages[1] :", ages[1]) + + ages[0] *= ages[1] + fmt.Println("ages[0] :", ages[0]) + fmt.Println("ages[1] :", ages[1]) + } } 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 5cd655c..4156c34 100644 --- a/14-arrays/02-examples-1-hipsters-love-bookstore/main.go +++ b/14-arrays/02-examples-1-hipsters-love-bookstore/main.go @@ -7,6 +7,8 @@ package main +import "fmt" + // STORY: // Hipster's Love store publishes limited books // twice a year. @@ -31,10 +33,30 @@ func main() { books[2] = "Everythingship" books[3] += books[0] + " 2nd Edition" + // -------------------- + // INDEXING + // -------------------- + // Go compiler can catch indexing errors when constant is used // books[4] = "Neverland" // Go compiler cannot catch indexing errors when non-constant is used // i := 4 // books[i] = "Neverland" + + // -------------------- + // PRINTING ARRAYS + // -------------------- + + // print the type + fmt.Printf("books : %T\n", books) + + // print the elements + fmt.Println("books :", books) + + // print the elements in quoted string + fmt.Printf("books : %q\n", books) + + // print the elements along with their types + fmt.Printf("books : %#v\n", books) } 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 dfda36d..2373d1b 100644 --- a/14-arrays/03-examples-2-hipsters-love-bookstore/main.go +++ b/14-arrays/03-examples-2-hipsters-love-bookstore/main.go @@ -57,7 +57,20 @@ func main() { for i := range sBooks { sBooks[i] = books[i+1] + // changes to sBooks[i] will not be visible here. + // sBooks here is a copy of the original array. } + // changes to sBooks are visible here + + // sBooks is a copy of the original sBooks array. + // + // v is also a copy of the original array element. + // + // if you want to update the original element, use it as in the loop above. + // + // for _, v := range sBooks { + // v += "won't effect" + // } fmt.Printf("\nwinter : %#v\n", wBooks) fmt.Printf("\nsummer : %#v\n", sBooks) diff --git a/14-arrays/04-array-literal/main.go b/14-arrays/04-array-literal/main.go index b330c8e..0a1d5d7 100644 --- a/14-arrays/04-array-literal/main.go +++ b/14-arrays/04-array-literal/main.go @@ -17,23 +17,64 @@ import "fmt" // So, let's create a 4 elements string array for the books. -const ( - winter = 1 - summer = 3 - yearly = winter + summer -) - func main() { - // ALTERNATIVE: // Use this only when you don't know about the elements beforehand - // - // var books [yearly]string + { + var books [4]string - books := [yearly]string{ - "Kafka's Revenge", - "Stay Golden", - "Everythingship", - "Kafka's Revenge 2nd Edition", + books[0] = "Kafka's Revenge" + books[1] = "Stay Golden" + books[2] = "Everythingship" + books[3] += "Kafka's Revenge 2nd Edition" + + _ = books + } + + // This is not necessary, use the short declaration syntax below + { + var books = [4]string{ + "Kafka's Revenge", + "Stay Golden", + "Everythingship", + "Kafka's Revenge 2nd Edition", + } + + _ = books + } + + // Use this if you know about the elements + { + books := [4]string{ + "Kafka's Revenge", + "Stay Golden", + "Everythingship", + "Kafka's Revenge 2nd Edition", + } + + _ = books + } + + { + // Use this if you know about the elements + books := [4]string{ + "Kafka's Revenge", + "Stay Golden", + } + + // Uninitialized elements will be set to their zero values + fmt.Printf("books : %#v\n", books) + } + + // You can also use the ellipsis syntax + // ... equals to 4 + { + books := [...]string{ + "Kafka's Revenge", + "Stay Golden", + "Everythingship", + "Kafka's Revenge 2nd Edition", + } + + _ = books } - fmt.Printf("books : %#v\n", books) } diff --git a/14-arrays/xx-examples-3-hipsters-love-bookstore/main.go b/14-arrays/05-array-literal-example/main.go similarity index 62% rename from 14-arrays/xx-examples-3-hipsters-love-bookstore/main.go rename to 14-arrays/05-array-literal-example/main.go index ced96f1..e360f31 100644 --- a/14-arrays/xx-examples-3-hipsters-love-bookstore/main.go +++ b/14-arrays/05-array-literal-example/main.go @@ -24,18 +24,11 @@ const ( ) func main() { - var books [yearly]string - - books[0] = "Kafka's Revenge" - books[1] = "Stay Golden" - books[2] = "Everythingship" - books[3] += books[0] + " 2nd Edition" - - // won't update the original array - for _, v := range books { - v += " (Sold Out)" - fmt.Println(v) + books := [...]string{ + "Kafka's Revenge", + "Stay Golden", + "Everythingship", + "Kafka's Revenge 2nd Edition", } - - fmt.Printf("\nbooks: %#v\n", books) + fmt.Printf("books : %#v\n", books) } diff --git a/14-arrays/_experimental/_14-arrays/12-random-message/01-1st-version/main.go b/14-arrays/06-challenge-moodly/main.go similarity index 100% rename from 14-arrays/_experimental/_14-arrays/12-random-message/01-1st-version/main.go rename to 14-arrays/06-challenge-moodly/main.go diff --git a/14-arrays/06-compare-unnamed/main.go b/14-arrays/06-compare-unnamed/main.go deleted file mode 100644 index 84d4f5b..0000000 --- a/14-arrays/06-compare-unnamed/main.go +++ /dev/null @@ -1,35 +0,0 @@ -// 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" - -// STORY: -// You want to compare two bookcases, -// whether they're equal or not. - -func main() { - type ( - bookcase [5]int - cabinet [5]int - ) - - blue := bookcase{6, 9, 3, 2, 1} - red := cabinet{6, 9, 3, 2, 1} - - fmt.Print("Are they equal? ") - - if cabinet(blue) == red { - fmt.Println("✅") - } else { - fmt.Println("❌") - } - - fmt.Printf("blue: %#v\n", blue) - fmt.Printf("red : %#v\n", bookcase(red)) -} diff --git a/14-arrays/05-compare/main.go b/14-arrays/07-compare/main.go similarity index 100% rename from 14-arrays/05-compare/main.go rename to 14-arrays/07-compare/main.go diff --git a/14-arrays/07-assignment/main.go b/14-arrays/08-assignment/main.go similarity index 89% rename from 14-arrays/07-assignment/main.go rename to 14-arrays/08-assignment/main.go index a9e183d..97337b9 100644 --- a/14-arrays/07-assignment/main.go +++ b/14-arrays/08-assignment/main.go @@ -21,9 +21,6 @@ func main() { books[i] += b + " 2nd Ed." } - // copying arrays using slices - // copy(books[:], prev[:]) - books[3] = "Awesomeness" fmt.Printf("last year:\n%#v\n", prev) diff --git a/14-arrays/08-multi-dimensional/main.go b/14-arrays/09-multi-dimensional/main.go similarity index 71% rename from 14-arrays/08-multi-dimensional/main.go rename to 14-arrays/09-multi-dimensional/main.go index b538d2c..abbc2f1 100644 --- a/14-arrays/08-multi-dimensional/main.go +++ b/14-arrays/09-multi-dimensional/main.go @@ -19,16 +19,16 @@ func main() { {9, 8, 4}, } - var avg float64 + var sum float64 for _, grades := range students { for _, grade := range grades { - avg += grade + sum += grade } } const N = float64(len(students) * len(students[0])) - fmt.Printf("Avg Grade: %g\n", avg/N) + fmt.Printf("Avg Grade: %g\n", sum/N) // ------------------------------------ // #2 - SO SO WAY @@ -40,13 +40,13 @@ func main() { // [3]float64{9, 8, 4}, // } - // var avg float64 + // var sum float64 - // avg += students[0][0] + students[0][1] + students[0][2] - // avg += students[1][0] + students[1][1] + students[1][2] + // sum += students[0][0] + students[0][1] + students[0][2] + // sum += 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) + // fmt.Printf("Avg Grade: %g\n", sum/N) // ------------------------------------ // #3 - MANUAL WAY @@ -55,10 +55,10 @@ func main() { // 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] + // var sum float64 + // sum += student1[0] + student1[1] + student1[2] + // sum += student2[0] + student2[1] + student2[2] // const N = float64(len(student1) * 2) - // fmt.Printf("Avg Grade: %g\n", avg/N) + // fmt.Printf("Avg Grade: %g\n", sum/N) } diff --git a/14-arrays/_experimental/_14-arrays/12-random-message/02-2nd-version/main.go b/14-arrays/10-challenge-moodly-2/main.go similarity index 100% rename from 14-arrays/_experimental/_14-arrays/12-random-message/02-2nd-version/main.go rename to 14-arrays/10-challenge-moodly-2/main.go diff --git a/14-arrays/09-keyed-elements/01-unkeyed/main.go b/14-arrays/11-keyed-elements/01-unkeyed/main.go similarity index 100% rename from 14-arrays/09-keyed-elements/01-unkeyed/main.go rename to 14-arrays/11-keyed-elements/01-unkeyed/main.go diff --git a/14-arrays/09-keyed-elements/02-keyed/main.go b/14-arrays/11-keyed-elements/02-keyed/main.go similarity index 100% rename from 14-arrays/09-keyed-elements/02-keyed/main.go rename to 14-arrays/11-keyed-elements/02-keyed/main.go diff --git a/14-arrays/09-keyed-elements/03-keyed-order/main.go b/14-arrays/11-keyed-elements/03-keyed-order/main.go similarity index 100% rename from 14-arrays/09-keyed-elements/03-keyed-order/main.go rename to 14-arrays/11-keyed-elements/03-keyed-order/main.go diff --git a/14-arrays/09-keyed-elements/04-keyed-auto-initialize/main.go b/14-arrays/11-keyed-elements/04-keyed-auto-initialize/main.go similarity index 100% rename from 14-arrays/09-keyed-elements/04-keyed-auto-initialize/main.go rename to 14-arrays/11-keyed-elements/04-keyed-auto-initialize/main.go diff --git a/14-arrays/09-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go b/14-arrays/11-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go similarity index 100% rename from 14-arrays/09-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go rename to 14-arrays/11-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go diff --git a/14-arrays/09-keyed-elements/06-keyed-and-unkeyed/main.go b/14-arrays/11-keyed-elements/06-keyed-and-unkeyed/main.go similarity index 100% rename from 14-arrays/09-keyed-elements/06-keyed-and-unkeyed/main.go rename to 14-arrays/11-keyed-elements/06-keyed-and-unkeyed/main.go diff --git a/14-arrays/09-keyed-elements/07-xratio-example/01-without-keys/main.go b/14-arrays/11-keyed-elements/07-xratio-example/01-without-keys/main.go similarity index 100% rename from 14-arrays/09-keyed-elements/07-xratio-example/01-without-keys/main.go rename to 14-arrays/11-keyed-elements/07-xratio-example/01-without-keys/main.go diff --git a/14-arrays/09-keyed-elements/07-xratio-example/02-with-keys/main.go b/14-arrays/11-keyed-elements/07-xratio-example/02-with-keys/main.go similarity index 100% rename from 14-arrays/09-keyed-elements/07-xratio-example/02-with-keys/main.go rename to 14-arrays/11-keyed-elements/07-xratio-example/02-with-keys/main.go diff --git a/14-arrays/12-compare-unnamed/main.go b/14-arrays/12-compare-unnamed/main.go new file mode 100644 index 0000000..1eb6e40 --- /dev/null +++ b/14-arrays/12-compare-unnamed/main.go @@ -0,0 +1,63 @@ +// 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" + +// STORY: +// You want to compare two bookcases, +// whether they're equal or not. + +func main() { + type ( + // integer int + + bookcase [5]int + cabinet [5]int + // ^- try changing this to: integer + // but first: uncomment the integer type above + ) + + blue := bookcase{6, 9, 3, 2, 1} + red := cabinet{6, 9, 3, 2, 1} + + fmt.Print("Are they equal? ") + + if cabinet(blue) == red { + fmt.Println("✅") + } else { + fmt.Println("❌") + } + + fmt.Printf("blue: %#v\n", blue) + fmt.Printf("red : %#v\n", bookcase(red)) + + // ------------------------------------------------ + // The underlying type of an unnamed type is itself. + // + // [5]integer's underlying type: [5]integer + // [5]int's underlying type : [5]int + // + // > [5]integer and [5]int are different types. + // > Their memory layout is not important. + // > Their types are not the same. + + // _ = [5]integer{} == [5]int{} + + // ------------------------------------------------ + // An unnamed and a named type can be compared, + // if they've identical underlying types. + // + // [5]integer's underlying type: [5]integer + // cabinet's underlying type : [5]integer + // + // Note: Assuming the cabinet's type definition is like so: + // type cabinet [5]integer + + // _ = [5]integer{} == cabinet{} +} diff --git a/14-arrays/TODO.md b/14-arrays/TODO.md new file mode 100644 index 0000000..c7884c9 --- /dev/null +++ b/14-arrays/TODO.md @@ -0,0 +1,2 @@ +- [ ] add challenge link to the moodly's resources +- [ ] add exercises 1 and 2 after the array basics quiz \ No newline at end of file diff --git a/14-arrays/_experimental/11-avg/main.go b/14-arrays/_experimental/11-avg/main.go deleted file mode 100644 index e395195..0000000 --- a/14-arrays/_experimental/11-avg/main.go +++ /dev/null @@ -1,54 +0,0 @@ -// 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() { - args := os.Args[1:] - - var ( - sum float64 - nums [5]float64 - ) - - for i, v := range args { - n, err := strconv.ParseFloat(v, 64) - if err != nil { - continue - } - - nums[i] = n - sum += n - } - - fmt.Println("Your numbers :", nums) - fmt.Println("Average :", sum/float64(len(nums))) -} - -// EXERCISE -// Average calculator has a flaw. -// It divides the numbers by the length of the array. -// This results in wrong calculatio. -// -// For example: -// -// When you run it like this: -// go run main.go 1 5 -// It tells you that the average number is: -// 1.2 -// Whereas, actually, it should be 3. -// -// Fix this error. -// So that, it will output 3 instead of 1.2 -// -// Do not change the length of the array. diff --git a/14-arrays/_experimental/11-avg/solution-slices/main.go b/14-arrays/_experimental/11-avg/solution-slices/main.go deleted file mode 100644 index 43ede91..0000000 --- a/14-arrays/_experimental/11-avg/solution-slices/main.go +++ /dev/null @@ -1,36 +0,0 @@ -// 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() { - args := os.Args[1:] - - var ( - sum float64 - nums []float64 - ) - - for _, v := range args { - n, err := strconv.ParseFloat(v, 64) - if err != nil { - continue - } - - nums = append(nums, n) - sum += n - } - - fmt.Println("Your numbers:", nums) - fmt.Println("Average:", sum/float64(len(nums))) -} diff --git a/14-arrays/_experimental/11-avg/solution/main.go b/14-arrays/_experimental/11-avg/solution/main.go deleted file mode 100644 index 94076f6..0000000 --- a/14-arrays/_experimental/11-avg/solution/main.go +++ /dev/null @@ -1,39 +0,0 @@ -// 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() { - args := os.Args[1:] - - var ( - sum float64 - nums [5]float64 - total float64 - ) - - for i, v := range args { - n, err := strconv.ParseFloat(v, 64) - if err != nil { - continue - } - - total++ - nums[i] = n - sum += n - } - - fmt.Println("Your numbers:", nums) - fmt.Printf("(Only %g of them were valid)\n", total) - fmt.Println("Average:", sum/total) -} diff --git a/14-arrays/_experimental/11-sort/main.go b/14-arrays/_experimental/11-sort/main.go deleted file mode 100644 index 40d1b3a..0000000 --- a/14-arrays/_experimental/11-sort/main.go +++ /dev/null @@ -1,48 +0,0 @@ -// 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" -) - -const usage = `Sorry. Go arrays are fixed. -So, for now, I'm only supporting sorting %d numbers... -` - -func main() { - args := os.Args[1:] - - var nums [5]float64 - - if len(args) > 5 { - fmt.Printf(usage, len(nums)) - return - } - - for i, v := range args { - n, err := strconv.ParseFloat(v, 64) - if err != nil { - continue - } - - nums[i] = n - } - - for range nums { - for i, v := range nums { - if i < len(nums)-1 && v > nums[i+1] { - nums[i], nums[i+1] = nums[i+1], nums[i] - } - } - } - - fmt.Println(nums) -} diff --git a/14-arrays/_experimental/11-word-finder/main.go b/14-arrays/_experimental/11-word-finder/main.go deleted file mode 100644 index 38dc06f..0000000 --- a/14-arrays/_experimental/11-word-finder/main.go +++ /dev/null @@ -1,41 +0,0 @@ -// 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" -) - -const corpus = `lazy cat jumps again and again and again... -since she is very excited and happy.` - -func main() { - query := os.Args[1:] - words := strings.Fields(corpus) - - filter := [...]string{ - "and", "or", "the", "is", "since", "very", - } - -queries: - for _, q := range query { - for _, v := range filter { - if q == v { - continue queries - } - } - - for i, w := range words { - if q == w { - fmt.Printf("#%-2d: %q\n", i+1, w) - } - } - } -} diff --git a/14-arrays/_experimental/_14-arrays/01-what/01-without-arrays/main.go b/14-arrays/_experimental/_14-arrays/01-what/01-without-arrays/main.go deleted file mode 100644 index 971b653..0000000 --- a/14-arrays/_experimental/_14-arrays/01-what/01-without-arrays/main.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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 -} diff --git a/14-arrays/_experimental/_14-arrays/01-what/02-with-arrays/main.go b/14-arrays/_experimental/_14-arrays/01-what/02-with-arrays/main.go deleted file mode 100644 index cc79c94..0000000 --- a/14-arrays/_experimental/_14-arrays/01-what/02-with-arrays/main.go +++ /dev/null @@ -1,27 +0,0 @@ -// 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) - } -} diff --git a/14-arrays/_experimental/_14-arrays/02-array-literals/01/main.go b/14-arrays/_experimental/_14-arrays/02-array-literals/01/main.go deleted file mode 100644 index e613271..0000000 --- a/14-arrays/_experimental/_14-arrays/02-array-literals/01/main.go +++ /dev/null @@ -1,18 +0,0 @@ -// 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) - } -} diff --git a/14-arrays/_experimental/_14-arrays/02-array-literals/02-fixed-size/main.go b/14-arrays/_experimental/_14-arrays/02-array-literals/02-fixed-size/main.go deleted file mode 100644 index 6187189..0000000 --- a/14-arrays/_experimental/_14-arrays/02-array-literals/02-fixed-size/main.go +++ /dev/null @@ -1,18 +0,0 @@ -// 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) - // } -} diff --git a/14-arrays/_experimental/_14-arrays/02-array-literals/03-constant-expressions/main.go b/14-arrays/_experimental/_14-arrays/02-array-literals/03-constant-expressions/main.go deleted file mode 100644 index 520de38..0000000 --- a/14-arrays/_experimental/_14-arrays/02-array-literals/03-constant-expressions/main.go +++ /dev/null @@ -1,39 +0,0 @@ -// 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. diff --git a/14-arrays/_experimental/_14-arrays/02-array-literals/04-element-type/main.go b/14-arrays/_experimental/_14-arrays/02-array-literals/04-element-type/main.go deleted file mode 100644 index 3b008ba..0000000 --- a/14-arrays/_experimental/_14-arrays/02-array-literals/04-element-type/main.go +++ /dev/null @@ -1,51 +0,0 @@ -// 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. diff --git a/14-arrays/_experimental/_14-arrays/02-array-literals/05-ellipsis/main.go b/14-arrays/_experimental/_14-arrays/02-array-literals/05-ellipsis/main.go deleted file mode 100644 index c12af56..0000000 --- a/14-arrays/_experimental/_14-arrays/02-array-literals/05-ellipsis/main.go +++ /dev/null @@ -1,20 +0,0 @@ -// 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) - } -} diff --git a/14-arrays/_experimental/_14-arrays/03-array-type/main.go b/14-arrays/_experimental/_14-arrays/03-array-type/main.go deleted file mode 100644 index 06ec9ff..0000000 --- a/14-arrays/_experimental/_14-arrays/03-array-type/main.go +++ /dev/null @@ -1,22 +0,0 @@ -// 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 -} diff --git a/14-arrays/_experimental/_14-arrays/04-zero-values/01-uninitialized-array/main.go b/14-arrays/_experimental/_14-arrays/04-zero-values/01-uninitialized-array/main.go deleted file mode 100644 index 6d3fabb..0000000 --- a/14-arrays/_experimental/_14-arrays/04-zero-values/01-uninitialized-array/main.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/04-zero-values/02-partially-initialized-array/main.go b/14-arrays/_experimental/_14-arrays/04-zero-values/02-partially-initialized-array/main.go deleted file mode 100644 index e35abbe..0000000 --- a/14-arrays/_experimental/_14-arrays/04-zero-values/02-partially-initialized-array/main.go +++ /dev/null @@ -1,16 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/04-zero-values/03-ellipsis/main.go b/14-arrays/_experimental/_14-arrays/04-zero-values/03-ellipsis/main.go deleted file mode 100644 index 1a1a500..0000000 --- a/14-arrays/_experimental/_14-arrays/04-zero-values/03-ellipsis/main.go +++ /dev/null @@ -1,18 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/04-zero-values/04-without-ellipsis/main.go b/14-arrays/_experimental/_14-arrays/04-zero-values/04-without-ellipsis/main.go deleted file mode 100644 index 2b62719..0000000 --- a/14-arrays/_experimental/_14-arrays/04-zero-values/04-without-ellipsis/main.go +++ /dev/null @@ -1,18 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/04-zero-values/05-more-examples/main.go b/14-arrays/_experimental/_14-arrays/04-zero-values/05-more-examples/main.go deleted file mode 100644 index 11a0ce3..0000000 --- a/14-arrays/_experimental/_14-arrays/04-zero-values/05-more-examples/main.go +++ /dev/null @@ -1,21 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/05-array-operations/01-getting-length/main.go b/14-arrays/_experimental/_14-arrays/05-array-operations/01-getting-length/main.go deleted file mode 100644 index 94e87ce..0000000 --- a/14-arrays/_experimental/_14-arrays/05-array-operations/01-getting-length/main.go +++ /dev/null @@ -1,35 +0,0 @@ -// 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{}) -} diff --git a/14-arrays/_experimental/_14-arrays/05-array-operations/02-getting-elements/main.go b/14-arrays/_experimental/_14-arrays/05-array-operations/02-getting-elements/main.go deleted file mode 100644 index 84673b2..0000000 --- a/14-arrays/_experimental/_14-arrays/05-array-operations/02-getting-elements/main.go +++ /dev/null @@ -1,32 +0,0 @@ -// 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) - } -} diff --git a/14-arrays/_experimental/_14-arrays/05-array-operations/03-getting-elements/main.go b/14-arrays/_experimental/_14-arrays/05-array-operations/03-getting-elements/main.go deleted file mode 100644 index f3a69c1..0000000 --- a/14-arrays/_experimental/_14-arrays/05-array-operations/03-getting-elements/main.go +++ /dev/null @@ -1,49 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/06-comparing-arrays/01-equal-arrays/main.go b/14-arrays/_experimental/_14-arrays/06-comparing-arrays/01-equal-arrays/main.go deleted file mode 100644 index 440d2fe..0000000 --- a/14-arrays/_experimental/_14-arrays/06-comparing-arrays/01-equal-arrays/main.go +++ /dev/null @@ -1,30 +0,0 @@ -// 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]) - } -} diff --git a/14-arrays/_experimental/_14-arrays/06-comparing-arrays/02-not-equal-arrays/main.go b/14-arrays/_experimental/_14-arrays/06-comparing-arrays/02-not-equal-arrays/main.go deleted file mode 100644 index d88a685..0000000 --- a/14-arrays/_experimental/_14-arrays/06-comparing-arrays/02-not-equal-arrays/main.go +++ /dev/null @@ -1,39 +0,0 @@ -// 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 - } -} diff --git a/14-arrays/_experimental/_14-arrays/06-comparing-arrays/03-comparability/main.go b/14-arrays/_experimental/_14-arrays/06-comparing-arrays/03-comparability/main.go deleted file mode 100644 index b243337..0000000 --- a/14-arrays/_experimental/_14-arrays/06-comparing-arrays/03-comparability/main.go +++ /dev/null @@ -1,24 +0,0 @@ -// 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!") - // } -} diff --git a/14-arrays/_experimental/_14-arrays/06-comparing-arrays/04-example/main.go b/14-arrays/_experimental/_14-arrays/06-comparing-arrays/04-example/main.go deleted file mode 100644 index dd197a9..0000000 --- a/14-arrays/_experimental/_14-arrays/06-comparing-arrays/04-example/main.go +++ /dev/null @@ -1,26 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/07-unnamed-vs-named-types/main.go b/14-arrays/_experimental/_14-arrays/07-unnamed-vs-named-types/main.go deleted file mode 100644 index a030fe1..0000000 --- a/14-arrays/_experimental/_14-arrays/07-unnamed-vs-named-types/main.go +++ /dev/null @@ -1,38 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/01-assigning-arrays/main.go b/14-arrays/_experimental/_14-arrays/08-assigning-arrays/01-assigning-arrays/main.go deleted file mode 100644 index b28c83b..0000000 --- a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/01-assigning-arrays/main.go +++ /dev/null @@ -1,24 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/02-arrays-are-value-types/main.go b/14-arrays/_experimental/_14-arrays/08-assigning-arrays/02-arrays-are-value-types/main.go deleted file mode 100644 index aa36f54..0000000 --- a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/02-arrays-are-value-types/main.go +++ /dev/null @@ -1,27 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/03-assigning-arrays2/main.go b/14-arrays/_experimental/_14-arrays/08-assigning-arrays/03-assigning-arrays2/main.go deleted file mode 100644 index e558aa7..0000000 --- a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/03-assigning-arrays2/main.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/04-cannot-assign-arrays/main.go b/14-arrays/_experimental/_14-arrays/08-assigning-arrays/04-cannot-assign-arrays/main.go deleted file mode 100644 index 20c3a03..0000000 --- a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/04-cannot-assign-arrays/main.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 -} diff --git a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/05-passing-to-funcs/main.go b/14-arrays/_experimental/_14-arrays/08-assigning-arrays/05-passing-to-funcs/main.go deleted file mode 100644 index 7ed609e..0000000 --- a/14-arrays/_experimental/_14-arrays/08-assigning-arrays/05-passing-to-funcs/main.go +++ /dev/null @@ -1,31 +0,0 @@ -// 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) -} diff --git a/14-arrays/_experimental/_14-arrays/09-multi-dimensional-arrays/main.go b/14-arrays/_experimental/_14-arrays/09-multi-dimensional-arrays/main.go deleted file mode 100644 index 108dc0f..0000000 --- a/14-arrays/_experimental/_14-arrays/09-multi-dimensional-arrays/main.go +++ /dev/null @@ -1,49 +0,0 @@ -// 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) - } - } -} diff --git a/14-arrays/_experimental/_14-arrays/10-keyed-elements/01-unkeyed/main.go b/14-arrays/_experimental/_14-arrays/10-keyed-elements/01-unkeyed/main.go deleted file mode 100644 index f04c477..0000000 --- a/14-arrays/_experimental/_14-arrays/10-keyed-elements/01-unkeyed/main.go +++ /dev/null @@ -1,20 +0,0 @@ -// 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/_experimental/_14-arrays/10-keyed-elements/02-keyed/main.go b/14-arrays/_experimental/_14-arrays/10-keyed-elements/02-keyed/main.go deleted file mode 100644 index e74f912..0000000 --- a/14-arrays/_experimental/_14-arrays/10-keyed-elements/02-keyed/main.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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/_experimental/_14-arrays/10-keyed-elements/03-keyed-order/main.go b/14-arrays/_experimental/_14-arrays/10-keyed-elements/03-keyed-order/main.go deleted file mode 100644 index 122d389..0000000 --- a/14-arrays/_experimental/_14-arrays/10-keyed-elements/03-keyed-order/main.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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/_experimental/_14-arrays/10-keyed-elements/04-keyed-auto-initialize/main.go b/14-arrays/_experimental/_14-arrays/10-keyed-elements/04-keyed-auto-initialize/main.go deleted file mode 100644 index 5fa9b54..0000000 --- a/14-arrays/_experimental/_14-arrays/10-keyed-elements/04-keyed-auto-initialize/main.go +++ /dev/null @@ -1,28 +0,0 @@ -// 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/_experimental/_14-arrays/10-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go b/14-arrays/_experimental/_14-arrays/10-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go deleted file mode 100644 index a51b1fc..0000000 --- a/14-arrays/_experimental/_14-arrays/10-keyed-elements/05-keyed-auto-initialize-ellipsis/main.go +++ /dev/null @@ -1,36 +0,0 @@ -// 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/_experimental/_14-arrays/10-keyed-elements/06-keyed-and-unkeyed/main.go b/14-arrays/_experimental/_14-arrays/10-keyed-elements/06-keyed-and-unkeyed/main.go deleted file mode 100644 index 76839a7..0000000 --- a/14-arrays/_experimental/_14-arrays/10-keyed-elements/06-keyed-and-unkeyed/main.go +++ /dev/null @@ -1,34 +0,0 @@ -// 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/13-refactor-lucky-number-game/main.go b/14-arrays/_experimental/_14-arrays/13-refactor-lucky-number-game/main.go deleted file mode 100644 index d92d041..0000000 --- a/14-arrays/_experimental/_14-arrays/13-refactor-lucky-number-game/main.go +++ /dev/null @@ -1,72 +0,0 @@ -// 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 -} diff --git a/14-arrays/_experimental/_14-arrays/exercises/00/main.go b/14-arrays/_experimental/_14-arrays/exercises/00/main.go deleted file mode 100644 index 1bace58..0000000 --- a/14-arrays/_experimental/_14-arrays/exercises/00/main.go +++ /dev/null @@ -1,22 +0,0 @@ -// 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() { -} diff --git a/14-arrays/_experimental/_14-arrays/exercises/00/solution/main.go b/14-arrays/_experimental/_14-arrays/exercises/00/solution/main.go deleted file mode 100644 index b5e729e..0000000 --- a/14-arrays/_experimental/_14-arrays/exercises/00/solution/main.go +++ /dev/null @@ -1,11 +0,0 @@ -// 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/_experimental/_14-arrays/exercises/xxx-lucky-number-unique-picker/main.go b/14-arrays/_experimental/_14-arrays/exercises/xxx-lucky-number-unique-picker/main.go deleted file mode 100644 index 44beb17..0000000 --- a/14-arrays/_experimental/_14-arrays/exercises/xxx-lucky-number-unique-picker/main.go +++ /dev/null @@ -1,82 +0,0 @@ -// 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 -} diff --git a/14-arrays/_experimental/_14-arrays/questions/questions.md b/14-arrays/_experimental/_14-arrays/questions/questions.md deleted file mode 100644 index cac63ae..0000000 --- a/14-arrays/_experimental/_14-arrays/questions/questions.md +++ /dev/null @@ -1,3 +0,0 @@ -## ? -* text *CORRECT* -* text diff --git a/14-arrays/_experimental/__main.go b/14-arrays/_experimental/__main.go deleted file mode 100644 index 12125e9..0000000 --- a/14-arrays/_experimental/__main.go +++ /dev/null @@ -1,23 +0,0 @@ -// 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 - Wed - Thu - Fri - Sat - Sun -) - -var names = [...]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} - - -for d := Mon; d <= Sun; d++ { - fmt.Println(names[d]) -} \ No newline at end of file diff --git a/14-arrays/_experimental/addr/main.go b/14-arrays/_experimental/addr/main.go deleted file mode 100644 index d863b16..0000000 --- a/14-arrays/_experimental/addr/main.go +++ /dev/null @@ -1,26 +0,0 @@ -// 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() { - age1 := getAge(15) - age2 := getAge(25) - age3 := getAge(35) - - fmt.Printf("age1 lives in %p\n", age1) - fmt.Printf("age2 lives in %p\n", age2) - fmt.Printf("age3 lives in %p\n", age3) -} - -func getAge(n byte) *byte { - m := new(byte) - *m = n - return m -} diff --git a/14-arrays/_experimental/csv-writer-slices/main.go b/14-arrays/_experimental/csv-writer-slices/main.go deleted file mode 100644 index 2fdcc63..0000000 --- a/14-arrays/_experimental/csv-writer-slices/main.go +++ /dev/null @@ -1,43 +0,0 @@ -// 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 ( - "encoding/csv" - "log" - "os" -) - -func main() { - records := [...][3]string{ - {"first_name", "last_name", "username"}, - {"Rob", "Pike", "rob"}, - {"Ken", "Thompson", "ken"}, - {"Robert", "Griesemer", "gri"}, - } - - w := csv.NewWriter(os.Stdout) - - for _, record := range records { - if err := w.Write(record[:]); err != nil { - log.Fatalln("error writing record to csv:", err) - } - } - - // Write any buffered data to the underlying writer (standard output). - w.Flush() - - if err := w.Error(); err != nil { - log.Fatal(err) - } - // Output: - // first_name,last_name,username - // Rob,Pike,rob - // Ken,Thompson,ken - // Robert,Griesemer,gri -} diff --git a/14-arrays/_experimental/notes.md b/14-arrays/_experimental/notes.md deleted file mode 100644 index 66d5cbc..0000000 --- a/14-arrays/_experimental/notes.md +++ /dev/null @@ -1,10 +0,0 @@ -# Array - -## Summary - -* An array can store any type of values as long as they've the same type - -* Fixed Length and Type -* Stores the same type of values - -* Array stores its values contagiously \ No newline at end of file diff --git a/14-arrays/_experimental/wall-clock/main.go b/14-arrays/_experimental/wall-clock/main.go deleted file mode 100644 index 76098cc..0000000 --- a/14-arrays/_experimental/wall-clock/main.go +++ /dev/null @@ -1,198 +0,0 @@ -// 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" - "time" -) - -// NOTE: We're going to refactor this code in the functions section. - -const ( - charDefault = "█" - charColon = "░" - charEmpty = " " - charSeparator = " " - - // clears the screen - // - // only works on bash command prompts - // \033 is a control code: [2J clears the screen - // - // For Go Playground, use: "\f" - charClear = "\033[H\033[2J" -) - -func main() { - // for keeping things easy to read and type-safe - type placeholder [5][3]byte - - // we're using arrays because: - // - the pattern of a placeholder is constant (it doesn't change) - // - the placeholders in the clock is constant (which is 8) - // - // so, whenever values are precomputed or constant, use an array. - // - // you can always convert it to a slice easily. you'll learn - // how to do so in the slices section. - digits := [10]placeholder{ - // 0 - { - {1, 1, 1}, - {1, 0, 1}, - {1, 0, 1}, - {1, 0, 1}, - {1, 1, 1}, - }, - - // 1 - { - {1, 1, 0}, - {0, 1, 0}, - {0, 1, 0}, - {0, 1, 0}, - {1, 1, 1}, - }, - - // 2 - { - {1, 1, 1}, - {0, 0, 1}, - {1, 1, 1}, - {1, 0, 0}, - {1, 1, 1}, - }, - - // 3 - { - {1, 1, 1}, - {0, 0, 1}, - {1, 1, 1}, - {0, 0, 1}, - {1, 1, 1}, - }, - - // 4 - { - {1, 0, 1}, - {1, 0, 1}, - {1, 1, 1}, - {0, 0, 1}, - {0, 0, 1}, - }, - - // 5 - { - {1, 1, 1}, - {1, 0, 0}, - {1, 1, 1}, - {0, 0, 1}, - {1, 1, 1}, - }, - - // 6 - { - {1, 1, 1}, - {1, 0, 0}, - {1, 1, 1}, - {1, 0, 1}, - {1, 1, 1}, - }, - - // 7 - { - {1, 1, 1}, - {0, 0, 1}, - {0, 0, 1}, - {0, 0, 1}, - {0, 0, 1}, - }, - - // 8 - { - {1, 1, 1}, - {1, 0, 1}, - {1, 1, 1}, - {1, 0, 1}, - {1, 1, 1}, - }, - - // 9 - { - {1, 1, 1}, - {1, 0, 1}, - {1, 1, 1}, - {0, 0, 1}, - {1, 1, 1}, - }, - } - - colon := placeholder{ - {0, 0, 0}, - {0, 1, 0}, - {0, 0, 0}, - {0, 1, 0}, - {0, 0, 0}, - } - - // For Go Playground: loop 100 steps, for example. - for { - // get the current time: hour, minute, and second - var ( - now = time.Now() - hour, min, sec = now.Hour(), now.Minute(), now.Second() - ) - - // again, an array is used here. - // - // because, the number of placeholders in this clock - // is constant (which is 8 placeholders). - clock := [8]placeholder{ - // separate the digits: 17 becomes, 1 and 7 respectively - digits[hour/10], digits[hour%10], - colon, - digits[min/10], digits[min%10], - colon, - digits[sec/10], digits[sec%10], - } - - fmt.Print(charClear) - - // print the clock - for line := range clock[0] { - - // print a line for each letter - for letter, v := range clock { - // colon blink - char := charDefault - if v == colon { - char = charEmpty - if sec%2 == 0 { - char = charColon - } - } - - // print a line - for _, c := range clock[letter][line] { - if c == 1 { - fmt.Print(char) - } else { - fmt.Print(charEmpty) - } - } - - fmt.Print(charSeparator) - } - fmt.Println() - } - - // wait for 1 second - time.Sleep(time.Second) - } -} diff --git a/14-arrays/exercises/00-name/main.go b/14-arrays/exercises/00-name/main.go index bbf0173..7dd4f79 100644 --- a/14-arrays/exercises/00-name/main.go +++ b/14-arrays/exercises/00-name/main.go @@ -1,10 +1,3 @@ -// 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 b5e729e..da29a2c 100644 --- a/14-arrays/exercises/00-name/solution/main.go +++ b/14-arrays/exercises/00-name/solution/main.go @@ -1,10 +1,3 @@ -// 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 9d7d9d1..0a2da5f 100644 --- a/14-arrays/exercises/01-declare-empty/main.go +++ b/14-arrays/exercises/01-declare-empty/main.go @@ -10,33 +10,46 @@ package main // --------------------------------------------------------- // EXERCISE: Declare empty arrays // -// 1. Declare and printf the following arrays: +// 1. Declare and print the following arrays with their types: // -// 1. A string array with 4 items -// 2. An int array 5 items -// 3. A byte array with 5 items -// 4. A float64 array with 1 item -// 5. A bool array with 4 items -// 6. A byte array without any items +// 1. The names of your best three friends +// 2. The distances to five different locations +// 3. A data buffer with five bytes of capacity +// 4. Currency exchange ratios only for a single currency +// 5. Up/Down status of four different web servers +// 6. A byte array that doesn't occupy memory space // -// 2. Print the types of the previous arrays. +// 2. Print only the types of the same arrays. // -// NOTE -// You should use printf with #v verb. +// 3. Print only the elements of the same arrays. +// +// HINT +// When printing the elements of an array, you can use the usual Printf verbs. +// +// For example: +// When printing a string array, you can use "%q" verb as usual. // // EXPECTED OUTPUT -// names : [4]string{"", "", "", ""} +// names : [3]string{"", "", "", ""} // distances: [5]int{0, 0, 0, 0, 0} // data : [5]uint8{0x0, 0x0, 0x0, 0x0, 0x0} // ratios : [1]float64{0} -// switches : [4]bool{false, false, false, false} -// zero : [0]bool{} -// names : [4]string +// alives : [4]bool{false, false, false, false} +// zero : [0]uint8{} +// +// names : [3]string // distances: [5]int // data : [5]uint8 // ratios : [1]float64 -// switches : [4]bool -// zero : [0]bool +// alives : [4]bool +// zero : [0]uint8 +// +// names : ["" "" ""] +// distances: [0 0 0 0 0] +// data : [0 0 0 0 0] +// ratios : [0.00] +// alives : [false false false false] +// zero : [] // --------------------------------------------------------- func main() { diff --git a/14-arrays/exercises/01-declare-empty/solution/main.go b/14-arrays/exercises/01-declare-empty/solution/main.go index a8637e9..a28c1c2 100644 --- a/14-arrays/exercises/01-declare-empty/solution/main.go +++ b/14-arrays/exercises/01-declare-empty/solution/main.go @@ -10,34 +10,38 @@ package main import "fmt" func main() { - // 1. Declare and printf the following arrays: - // 1. A string array with 4 items - // 2. An int array 5 items - // 3. A byte array with 5 items - // 4. A float64 array with 1 item - // 5. A bool array with 4 items - // 6. A byte array without any items var ( - names [4]string - distances [5]int - data [5]byte - ratios [1]float64 - switches [4]bool - zero [0]bool + names [3]string // The names of your best three friends + distances [5]int // The distances to five different locations + data [5]byte // A data buffer with five bytes of capacity + ratios [1]float64 // Currency exchange ratios only for a single currency + alives [4]bool // Up/Down status of four different web servers + zero [0]byte // A byte array that doesn't occupy memory space ) + // 1. Declare and print the arrays with their types. fmt.Printf("names : %#v\n", names) fmt.Printf("distances: %#v\n", distances) fmt.Printf("data : %#v\n", data) fmt.Printf("ratios : %#v\n", ratios) - fmt.Printf("switches : %#v\n", switches) + fmt.Printf("alives : %#v\n", alives) fmt.Printf("zero : %#v\n", zero) - // 2. Print the types of the previous arrays. + // 2. Print only the types of the same arrays. + fmt.Println() fmt.Printf("names : %T\n", names) fmt.Printf("distances: %T\n", distances) fmt.Printf("data : %T\n", data) fmt.Printf("ratios : %T\n", ratios) - fmt.Printf("switches : %T\n", switches) + fmt.Printf("alives : %T\n", alives) fmt.Printf("zero : %T\n", zero) + + // 3. Print only the elements of the same arrays. + fmt.Println() + fmt.Printf("names : %q\n", names) + fmt.Printf("distances: %d\n", distances) + fmt.Printf("data : %d\n", data) + fmt.Printf("ratios : %.2f\n", ratios) + fmt.Printf("alives : %t\n", alives) + fmt.Printf("zero : %d\n", zero) } diff --git a/14-arrays/exercises/02-get-set-arrays/main.go b/14-arrays/exercises/02-get-set-arrays/main.go new file mode 100644 index 0000000..03009fc --- /dev/null +++ b/14-arrays/exercises/02-get-set-arrays/main.go @@ -0,0 +1,118 @@ +package main + +// --------------------------------------------------------- +// EXERCISE: Get and Set Array Elements +// +// 1. Use the 01-declare-empty exercise +// 2. Remove everything but the array declarations +// +// 3. Assign your best friends' names to the names array +// +// 4. Assign distances to the closest cities to you to the distance array +// +// 5. Assign arbitrary bytes to the data array +// +// 6. Assign a value to the ratios array +// +// 7. Assign true/false values to the alives arrays +// +// 8. Try to assign to the zero array and observe the error +// +// 9. Now use ordinary loop statements for each array and print them +// (do not use for range) +// +// 10. Now use for range loop statements for each array and print them +// +// 11. Try assigning different types of values to the arrays, break things, +// and observe the errors +// +// 12. Remove some of the array assignments and observe the loop outputs +// (zero values) +// +// +// EXPECTED OUTPUT +// +// Note: The output can change depending on the values that you've assigned to them, of course. +// You're free to assign any values. +// +// names +// ==================== +// names[0]: "Einstein" +// names[1]: "Tesla" +// names[2]: "Shepard" +// +// distances +// ==================== +// distances[0]: 50 +// distances[1]: 40 +// distances[2]: 75 +// distances[3]: 30 +// distances[4]: 125 +// +// data +// ==================== +// data[0]: 72 +// data[1]: 69 +// data[2]: 76 +// data[3]: 76 +// data[4]: 79 +// +// ratios +// ==================== +// ratios[0]: 3.14 +// +// alives +// ==================== +// alives[0]: true +// alives[1]: false +// alives[2]: true +// alives[3]: false +// +// zero +// ==================== + +// +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// FOR RANGES +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// +// names +// ==================== +// names[0]: "Einstein" +// names[1]: "Tesla" +// names[2]: "Shepard" +// +// distances +// ==================== +// distances[0]: 50 +// distances[1]: 40 +// distances[2]: 75 +// distances[3]: 30 +// distances[4]: 125 +// +// data +// ==================== +// data[0]: 72 +// data[1]: 69 +// data[2]: 76 +// data[3]: 76 +// data[4]: 79 +// +// ratios +// ==================== +// ratios[0]: 3.14 +// +// alives +// ==================== +// alives[0]: true +// alives[1]: false +// alives[2]: true +// alives[3]: false +// +// zero +// ==================== +// +// --------------------------------------------------------- + +func main() { +} diff --git a/14-arrays/exercises/02-get-set-arrays/solution/main.go b/14-arrays/exercises/02-get-set-arrays/solution/main.go new file mode 100644 index 0000000..814060f --- /dev/null +++ b/14-arrays/exercises/02-get-set-arrays/solution/main.go @@ -0,0 +1,129 @@ +// 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() { + var ( + names [3]string // The names of your best three friends + distances [5]int // The distances to five different locations + data [5]byte // A data buffer with five bytes of capacity + ratios [1]float64 // Currency exchange ratios only for a single currency + alives [4]bool // Up/Down status of four different web servers + zero [0]byte // A byte array that doesn't occupy memory space + ) + + names[0] = "Einstein" + names[1] = "Tesla" + names[2] = "Shepard" + + distances[0] = 50 + distances[1] = 40 + distances[2] = 75 + distances[3] = 30 + distances[4] = 125 + + data[0] = 'H' + data[1] = 'E' + data[2] = 'L' + data[3] = 'L' + data[4] = 'O' + + ratios[0] = 3.14145 + + alives[0] = true + alives[1] = false + alives[2] = true + alives[3] = false + + // zero[0] = "BOMB!" + _ = zero + + // ========================================================================= + + separator := "\n" + strings.Repeat("=", 20) + "\n" + + fmt.Print("names", separator) + for i := 0; i < len(names); i++ { + fmt.Printf("names[%d]: %q\n", i, names[i]) + } + + fmt.Print("\ndistances", separator) + for i := 0; i < len(distances); i++ { + fmt.Printf("distances[%d]: %d\n", i, distances[i]) + } + + fmt.Print("\ndata", separator) + for i := 0; i < len(data); i++ { + // try the %c verb + fmt.Printf("data[%d]: %d\n", i, data[i]) + } + + fmt.Print("\nratios", separator) + for i := 0; i < len(ratios); i++ { + fmt.Printf("ratios[%d]: %.2f\n", i, ratios[i]) + } + + fmt.Print("\nalives", separator) + for i := 0; i < len(alives); i++ { + fmt.Printf("alives[%d]: %t\n", i, alives[i]) + } + + // no loop for zero elements + fmt.Print("\nzero", separator) + for i := 0; i < len(zero); i++ { + fmt.Printf("zero[%d]: %d\n", i, zero[i]) + } + + // ========================================================================= + + // you know how this works :) don't be freaked out! + fmt.Printf(` + +%s +FOR RANGES +%[1]s + +`, strings.Repeat("~", 30)) + + fmt.Print("names", separator) + for i, v := range names { + fmt.Printf("names[%d]: %q\n", i, v) + } + + fmt.Print("\ndistances", separator) + for i, v := range distances { + fmt.Printf("distances[%d]: %d\n", i, v) + } + + fmt.Print("\ndata", separator) + for i, v := range data { + // try the %c verb + fmt.Printf("data[%d]: %d\n", i, v) + } + + fmt.Print("\nratios", separator) + for i, v := range ratios { + fmt.Printf("ratios[%d]: %.2f\n", i, v) + } + + fmt.Print("\nalives", separator) + for i, v := range alives { + fmt.Printf("alives[%d]: %t\n", i, v) + } + + // no loop for zero elements + fmt.Print("\nzero", separator) + for i, v := range zero { + fmt.Printf("zero[%d]: %d\n", i, v) + } +} diff --git a/14-arrays/exercises/03-array-literal/main.go b/14-arrays/exercises/03-array-literal/main.go new file mode 100644 index 0000000..696aca6 --- /dev/null +++ b/14-arrays/exercises/03-array-literal/main.go @@ -0,0 +1,20 @@ +package main + +// --------------------------------------------------------- +// EXERCISE: Refactor to Array Literals +// +// 1. Use the 02-get-set-arrays exercise +// +// 2. Refactor the array assignments to array literals +// +// 1. You would need to change the array declarations to array literals +// +// 2. Then, you would need to move the right-hand side of the assignments, +// into the array literals. +// +// EXPECTED OUTPUT +// The output should be the same as the 02-get-set-arrays exercise. +// --------------------------------------------------------- + +func main() { +} diff --git a/14-arrays/exercises/03-array-literal/solution/main.go b/14-arrays/exercises/03-array-literal/solution/main.go new file mode 100644 index 0000000..f12824c --- /dev/null +++ b/14-arrays/exercises/03-array-literal/solution/main.go @@ -0,0 +1,121 @@ +// 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() { + // The names of your best three friends + names := [3]string{ + "Einstein", + "Tesla", + "Shepard", + } + + // The distances to five different locations + distances := [5]int{50, 40, 75, 30, 125} + + // A data buffer with five bytes of capacity + data := [5]byte{'H', 'E', 'L', 'L', 'O'} + + // Currency exchange ratios only for a single currency + ratios := [1]float64{3.14145} + + // Up/Down status of four different web servers + alives := [4]bool{true, false, true, false} + + // A byte array that doesn't occupy memory space + // + // Don't do this: + // zero := [0]byte{} + // + // Do this (when you don't assign elements): + var zero [0]byte + + // ========================================================================= + + separator := "\n" + strings.Repeat("=", 20) + "\n" + + fmt.Print("names", separator) + for i := 0; i < len(names); i++ { + fmt.Printf("names[%d]: %q\n", i, names[i]) + } + + fmt.Print("\ndistances", separator) + for i := 0; i < len(distances); i++ { + fmt.Printf("distances[%d]: %d\n", i, distances[i]) + } + + fmt.Print("\ndata", separator) + for i := 0; i < len(data); i++ { + // try the %c verb + fmt.Printf("data[%d]: %d\n", i, data[i]) + } + + fmt.Print("\nratios", separator) + for i := 0; i < len(ratios); i++ { + fmt.Printf("ratios[%d]: %.2f\n", i, ratios[i]) + } + + fmt.Print("\nalives", separator) + for i := 0; i < len(alives); i++ { + fmt.Printf("alives[%d]: %t\n", i, alives[i]) + } + + // no loop for zero elements + fmt.Print("\nzero", separator) + for i := 0; i < len(zero); i++ { + fmt.Printf("zero[%d]: %d\n", i, zero[i]) + } + + // ========================================================================= + + // you know how this works :) don't be freaked out! + fmt.Printf(` + +%s +FOR RANGES +%[1]s + +`, strings.Repeat("~", 30)) + + fmt.Print("names", separator) + for i, v := range names { + fmt.Printf("names[%d]: %q\n", i, v) + } + + fmt.Print("\ndistances", separator) + for i, v := range distances { + fmt.Printf("distances[%d]: %d\n", i, v) + } + + fmt.Print("\ndata", separator) + for i, v := range data { + // try the %c verb + fmt.Printf("data[%d]: %d\n", i, v) + } + + fmt.Print("\nratios", separator) + for i, v := range ratios { + fmt.Printf("ratios[%d]: %.2f\n", i, v) + } + + fmt.Print("\nalives", separator) + for i, v := range alives { + fmt.Printf("alives[%d]: %t\n", i, v) + } + + // no loop for zero elements + fmt.Print("\nzero", separator) + for i, v := range zero { + fmt.Printf("zero[%d]: %d\n", i, v) + } +} diff --git a/14-arrays/exercises/04-ellipsis/main.go b/14-arrays/exercises/04-ellipsis/main.go new file mode 100644 index 0000000..27ed39b --- /dev/null +++ b/14-arrays/exercises/04-ellipsis/main.go @@ -0,0 +1,18 @@ +package main + +// --------------------------------------------------------- +// EXERCISE: Refactor to Ellipsis +// +// 1. Use the 03-array-literal exercise +// +// 2. Refactor the length of the array literals to ellipsis +// +// This means: Use the ellipsis instead of defining the array's length +// manually. +// +// EXPECTED OUTPUT +// The output should be the same as the 03-array-literal exercise. +// --------------------------------------------------------- + +func main() { +} diff --git a/14-arrays/exercises/04-ellipsis/solution/main.go b/14-arrays/exercises/04-ellipsis/solution/main.go new file mode 100644 index 0000000..5a49b15 --- /dev/null +++ b/14-arrays/exercises/04-ellipsis/solution/main.go @@ -0,0 +1,117 @@ +// 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() { + // The names of your best three friends + names := [...]string{ + "Einstein", + "Tesla", + "Shepard", + } + + // The distances to five different locations + distances := [...]int{50, 40, 75, 30, 125} + + // A data buffer with five bytes of capacity + data := [...]byte{'H', 'E', 'L', 'L', 'O'} + + // Currency exchange ratios only for a single currency + ratios := [...]float64{3.14145} + + // Up/Down status of four different web servers + alives := [...]bool{true, false, true, false} + + // A byte array that doesn't occupy memory space + // Obviously, do not use ellipsis on this one + var zero []byte + + // ========================================================================= + + separator := "\n" + strings.Repeat("=", 20) + "\n" + + fmt.Print("names", separator) + for i := 0; i < len(names); i++ { + fmt.Printf("names[%d]: %q\n", i, names[i]) + } + + fmt.Print("\ndistances", separator) + for i := 0; i < len(distances); i++ { + fmt.Printf("distances[%d]: %d\n", i, distances[i]) + } + + fmt.Print("\ndata", separator) + for i := 0; i < len(data); i++ { + // try the %c verb + fmt.Printf("data[%d]: %d\n", i, data[i]) + } + + fmt.Print("\nratios", separator) + for i := 0; i < len(ratios); i++ { + fmt.Printf("ratios[%d]: %.2f\n", i, ratios[i]) + } + + fmt.Print("\nalives", separator) + for i := 0; i < len(alives); i++ { + fmt.Printf("alives[%d]: %t\n", i, alives[i]) + } + + // no loop for zero elements + fmt.Print("\nzero", separator) + for i := 0; i < len(zero); i++ { + fmt.Printf("zero[%d]: %d\n", i, zero[i]) + } + + // ========================================================================= + + // you know how this works :) don't be freaked out! + fmt.Printf(` + +%s +FOR RANGES +%[1]s + +`, strings.Repeat("~", 30)) + + fmt.Print("names", separator) + for i, v := range names { + fmt.Printf("names[%d]: %q\n", i, v) + } + + fmt.Print("\ndistances", separator) + for i, v := range distances { + fmt.Printf("distances[%d]: %d\n", i, v) + } + + fmt.Print("\ndata", separator) + for i, v := range data { + // try the %c verb + fmt.Printf("data[%d]: %d\n", i, v) + } + + fmt.Print("\nratios", separator) + for i, v := range ratios { + fmt.Printf("ratios[%d]: %.2f\n", i, v) + } + + fmt.Print("\nalives", separator) + for i, v := range alives { + fmt.Printf("alives[%d]: %t\n", i, v) + } + + // no loop for zero elements + fmt.Print("\nzero", separator) + for i, v := range zero { + fmt.Printf("zero[%d]: %d\n", i, v) + } +} diff --git a/14-arrays/exercises/IDEAS.md b/14-arrays/exercises/IDEAS.md new file mode 100644 index 0000000..6b28c65 --- /dev/null +++ b/14-arrays/exercises/IDEAS.md @@ -0,0 +1,13 @@ +# Array Exercises + +- get data from command-line + - into a fixed array; see how it blows beyond its len + +- add items +- get items +- check the length +- reverse the array +- shuffle the items +- find the first item that contains x +- find the last item that contains y +- find the duplicate items diff --git a/14-arrays/exercises/README.md b/14-arrays/exercises/README.md index 496ef77..f7e2b4a 100644 --- a/14-arrays/exercises/README.md +++ b/14-arrays/exercises/README.md @@ -1,20 +1,19 @@ # Array Exercises +## Basic Exercises + 1. **[Declare Empty Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/01-declare-empty)** -- get data from command-line - - into a fixed array; see how it blows beyond its len +2. **[Get and Set Array Elements](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/02-get-set-arrays)** -- add items -- get items -- check the length -- print the items -- reverse the array -- shuffle the items -- find the first item that contains x -- find the last item that contains y -- find the duplicate items +3. **[Refactor to Array Literals](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/03-array-literal)** -1. **[text](https://github.com/inancgumus/learngo/tree/master/)** +4. **[Refactor to Ellipsis](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/04-ellipsis)** - text \ No newline at end of file +--- + +## Program Exercises + +????. **[text](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/)** + + ? diff --git a/14-arrays/questions/README.md b/14-arrays/questions/README.md index de0d04a..485d3fe 100644 --- a/14-arrays/questions/README.md +++ b/14-arrays/questions/README.md @@ -1,64 +1,3 @@ -# Arrays - -## Where is the 2nd variable below stored in memory? -```go -// Let's say that first variable below is stored in this memory location: 20th -var ( - first int32 = 100 - second int32 = 150 -) -``` -1. 21 -2. 22 -3. 24 -4. It can be stored anywhere *CORRECT* - -> **4:** That's right. It can be anywhere. Because, there's no guarantee that variables will be stored in contiguous memory locations. - - -## Where is the 3rd element of the following array stored in memory? - -```go -// -// Let's say that: -// nums array is stored in this memory location (cell): 500th -// -// So, this means: nums[0] is stored at 500th location as well. -// -var nums [5]int64 -``` -1. 3 -2. 2 -3. 502 -4. 503 -5. 516 *CORRECT* - -> **2:** Nope, that's the index of an element. -> -> **3, 4:** 500+index? You're getting closer. -> -> **5:** Perfect. Array elements are stored in contiguous memory locations (cells). Here, the array's location is 500, and each element is 8 bytes (int64). So, 1st element: 500th, 2nd element: 508th, 3rd element: 516th, and so on. Formula: 516 = 500 + (8 * (3 - 1)). - - -## How many values the following variable represents? -```go -var gophers [10]string -``` -1. 0 -2. 1 *CORRECT* -3. 2 -4. 10 - -> **2:** That's right! A variable can only store one value. Here, it stores a single array value with all its elements. However, through the gophers variable, you can access to 10 string values individually of that array. -> -> **4:** That's the length of the array. It's not the number of values that the gophers variable represents. - - -## ? -1. text *CORRECT* -2. text - -> **1:** -> - +# Array Quizzes +* [Array Basics](array-basics.md) \ No newline at end of file diff --git a/14-arrays/questions/array-basics.md b/14-arrays/questions/array-basics.md new file mode 100644 index 0000000..8b7f70d --- /dev/null +++ b/14-arrays/questions/array-basics.md @@ -0,0 +1,156 @@ +# Arrays + +## What's an array? +1. Accelerated charged particle array gun from Star Wars +2. Collection of values with dynamic length and type +3. Collection of values with fixed length and type *CORRECT* + + +## Where is the 2nd variable below stored in memory? +```go +// Let's say that first variable below is stored in this memory location: 20th +var ( + first int32 = 100 + second int32 = 150 +) +``` +1. 21 +2. 22 +3. 24 +4. It can be stored anywhere *CORRECT* + +> **4:** That's right. It can be anywhere. Because, unlike arrays, there isn't any guarantee that variables will be stored in contiguous memory locations. + + +## Where is the 3rd element of the nums array stored in memory? + +```go +// Let's say: nums array is stored in this memory location: 500th +var nums [5]int64 +``` +1. 3 +2. 2 +3. 502 +4. 503 +5. 516 *CORRECT* + +> **2:** Nope, that's the index of an element. +> +> **3, 4:** 500+index? You're getting closer. +> +> **5:** Perfect. Array elements are stored in contiguous memory locations. Here, the array's location is 500, and each element is 8 bytes (int64). So, 1st element is stored in: 500th, 2nd element: 508th, 3rd element: 516th, and so on. Formula for math lovers: 516 = 500 + 8 * (3 - 1). General formula: Starting position + The size of each element * (Element's index position - 1). + + +## How many values this variable stores? +**HINT:** _I'm asking about the variable not about the array inside the variable._ +```go +var gophers [10]string +``` +1. 0 +2. 1 *CORRECT* +3. 2 +4. 10 + +> **2:** That's right! A variable can only store one value. Here, it stores a single array value. However, through the variable, you can also access to individual string values inside the array value. +> +> **4:** That's the length of the array. It's not the number of values that the gophers variable stores. + + +## What's the length of this array? +```go +var gophers [5]int +``` +1. 5 *CORRECT* +2. 1 +3. 2 + +> **1:** That's right! It stores 5 int values. +> + +## What's the length of this array? +```go +const length = 5 * 2 +var gophers [length - 1]int +``` +1. 10 +2. 9 *CORRECT* +3. 1 + +> **2:** That's right! 5 * 2 - 1 is 9. You can use constant expressions while declaring the length of an array. + + +## What's the element type of this array? +```go +var luminosity [100]float32 +``` +1. [100]float32 +2. luminosity +3. float32 *CORRECT* + + +## What's the type of this array? +```go +var luminosity [100]float32 +``` +1. [100]float32 *CORRECT* +2. luminosity +3. float32 + +> **1:** That's right. Array's type is consisting of its length and its element type together. +> + + +## What does this program print? +```go +package main +import "fmt" + +func main() { + var names [3]string + + names[len(names)-1] = "!" + names[1] = "think" + names[2] + names[0] = "Don't" + names[0] += " " + + fmt.Println(names[0] + names[1] + names[2]) +} +``` +1. !think!Don't +2. Don't think!! *CORRECT* +3. This program is incorrect + +> **2:** "Don't think!! Just do!". Explanation is here: https://play.golang.org/p/y_Tqwn_XRlg +> + + +## What does this program print? +_It's OK if you can't solve this question. This is a hard question. You may try it on Go Playground here: https://play.golang.org/p/o0o0UM7Ktyy_ +```go +package main +import "fmt" + +// This program sets the next element of the array, +// then it quits just before the last element. +func main() { + var sum [5]int + + for i, v := range sum { + if i == len(sum) - 1 { + break + } + + sum[i+1] = 10 + fmt.Print(v, " ") + } +} +``` +1. 0 0 0 0 *CORRECT* +2. 10 10 10 10 +3. 0 10 10 10 + +> **1:** That's right! the for range clause copies the sum array. So, changing the elements of the sum array while inside of the loop won't effect the original sum array. However, if you try to print it right after the loop, you'll see that it has changed. Try printing it like so on Go Playground. +> +> **2:** That's not right. Because, the for range clause copies the sum array. +> + diff --git a/14-arrays/xx-examples-4-hipsters-love-bookstore/main.go b/14-arrays/xx-examples-4-hipsters-love-bookstore/main.go deleted file mode 100644 index 3a0df48..0000000 --- a/14-arrays/xx-examples-4-hipsters-love-bookstore/main.go +++ /dev/null @@ -1,52 +0,0 @@ -// 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" - -// STORY: -// Hipster's Love store publishes limited books -// twice a year. -// -// The number of books they publish is fixed at 4. - -// So, let's create a 4 elements string array for the books. - -const ( - winter = 1 - summer = 3 - yearly = winter + summer -) - -func main() { - var ( - prevBooks [yearly]string - books [yearly]string - ) - - books[0] = "Kafka's Revenge" - books[1] = "Stay Golden" - books[2] = "Everythingship" - books[3] += books[0] + " 2nd Edition" - - prevBooks = books - books[0] = "Silver Ages" - books[1] = "Dragon's Fire" - books[2] = "Nothingless" - books[3] = prevBooks[2] + " 2nd Edition" - - fmt.Println("Last Year's Books (Sold Out!):") - for _, v := range prevBooks { - fmt.Println("+", v) - } - - fmt.Println("\nNew Books:") - for _, v := range books { - fmt.Println("+", v) - } -} diff --git a/etc/stratch/main.go b/etc/stratch/main.go new file mode 100644 index 0000000..31fcc05 --- /dev/null +++ b/etc/stratch/main.go @@ -0,0 +1,42 @@ +package main + +import "fmt" + +func main() { + students := [...][3]float64{ + {5, 6, 1}, + {9, 8, 4}, + } + + var sum float64 + for _, grades := range students { + for _, grade := range grades { + sum += grade + } + } + + const N = float64(len(students) * len(students[0])) + fmt.Printf("Avg Grade: %g\n", sum/N) + + // students := [2][3]float64{ + // [3]float64{5, 6, 1}, + // [3]float64{9, 8, 4}, + // } + + // var sum float64 + // sum += students[0][0] + students[0][1] + students[0][2] + // sum += students[1][0] + students[1][1] + students[1][2] + + // const N = float64(len(students) * len(students[0])) + // fmt.Printf("Avg Grade: %g\n", sum/N) + + // student1 := [3]float64{5, 6, 1} + // student2 := [3]float64{9, 8, 4} + + // var sum float64 + // sum += student1[0] + student1[1] + student1[2] + // sum += student2[0] + student2[1] + student2[2] + + // const N = float64(len(student1) * 2) + // fmt.Printf("Avg Grade: %g\n", sum/N) +} diff --git a/x-tba/15-arrays-project-clock/01-challenge/main.go b/x-tba/15-arrays-project-clock/01-challenge/main.go new file mode 100644 index 0000000..5698e49 --- /dev/null +++ b/x-tba/15-arrays-project-clock/01-challenge/main.go @@ -0,0 +1,160 @@ +package main + +// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ +// ~GET SOCIAL~ +// +// Tweet when you complete: +// http://bit.ly/GOTWEET-CLOCK +// +// Discuss it with other gophers: +// http://bit.ly/LEARNGOSLACK +// ➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖ + +func main() { + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + // Notes + // + I've created the solutions step by step + // + So, if you get stuck, you can check out the next step + // without having to look at the entire final solution + // + For drawing the clock, you may use the artifacts below + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + // ★ Usable Artifacts + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + + // Clock Characters: + // + // You can put these in constants if you like. + // + // Use this for the digits : "█" + // Use this for the separators : "░" + + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + // ★ GOAL 1 : Printing the Digits + // ★ Solution: 02-printing-the-digits + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + + // - [ ] Define a new placeholder type + // ... + + // - [ ] Create the digits + // zero := ... + // one := ... + // ... + + // - [ ] Put them into the "digits" array + // digits := ... + + // - [ ] Print them side-by-side + // - [ ] Loop for all the lines in a digit + // - [ ] Print each digit line by line + // + // - [ ] Don't forget printing a space after each digit + // - [ ] Don't forget printing a newline after each line + // + // EXAMPLE: Let's say you want to print 10. + // + // ██ ███<--- Print a new line after printing a single line from + // █ █ █ all the digits. + // █ █ █ + // █ █ █ + // ███ ███ + // ^^ + // || + // ++----> Add space between the digits + // + + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + // ★ GOAL 2 : Printing the Clock + // ★ Solution: 03-printing-the-clock + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + + // - [ ] Get the current time + + // - [ ] Get the current hour, minute and second from the current time + + // - [ ] Create the clock array + // + // - [ ] Get the individual digits from the digits array + // + // clock := ... + + // - [ ] Print the clock + // + // - [ ] In the loops, use the clocks array instead + + // - [ ] Create a separator array (it's also a placeholder) + + // - [ ] Add the separators into the correct positions of + // the clock array + + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + // ★ GOAL 3 : Updating the Clock + // ★ Solution: 04-updating-the-clock + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + + // - [ ] Create an infinite loop to update the clock + + // - [ ] Update the clock every second + // + // time.Sleep(time.Second) will stop the world for 1 second + // + // See this for more info: + // https://golang.org/pkg/time/#Sleep + + // - [ ] Clear the screen before the infinite loop + // + // This string value clears the screen when printed: + // + // "\033[2J" + // + // + For Go Playground, use this instead: "\f" + // + // + This only works on bash command prompts. + + // - [ ] Move the cursor to the top-left corner of the screen before each step + // of the infinite loop + // + // This string value moves the cursor like so when printed: + // + // "\033[H" + // + // + For Go Playground, use this instead: "\f" + // + // + This only works on bash command prompts. + + // + If you're curious: + // \033 is a special control code: + // [2J clears the screen and the cursor + // [H moves the cursor to 0, 0 screen position + // + // See for more info: + // https://bluesock.org/~willkg/dev/ansi.html + + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + // ★ GOAL 4: Blinking the Separators + // ★ Solution: 05-blink-the-separators + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + + // - [ ] Blink the separators + // + // They should be visible per two seconds. + // + // Example: 1st second invisible + // 2nd second visible + // 3rd second invisible + // 4th second visible + // + // HINT: There are two ways to do this. + // + // 1- Manipulating the clock array directly + // (by adding/removing the separators) + // + // 2- Deciding what to print when printing the clock + + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ + // YOU CAN FIND THE FINAL SOLUTION WITH ANNOTATIONS: + // 06-full-annotated-code + // ★★★★★★★★★★★★★★★★★★★★★★★★★★★ +} diff --git a/x-tba/15-arrays-project-clock/02-printing-the-digits/main.go b/x-tba/15-arrays-project-clock/02-printing-the-digits/main.go new file mode 100644 index 0000000..c22c0bd --- /dev/null +++ b/x-tba/15-arrays-project-clock/02-printing-the-digits/main.go @@ -0,0 +1,135 @@ +// 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" +) + +// GOALS: +// 1. Define a new placeholder type +// 2. Create the digits +// 3. Put all of them in "digits" array +// 4. Print them side-by-side + +// Use this for the digits : "█" +// Use this for the separators : "░" + +func main() { + // for keeping things easy to read and type-safe + type placeholder [5]string + + zero := placeholder{ + "███", + "█ █", + "█ █", + "█ █", + "███", + } + + one := placeholder{ + "██ ", + " █ ", + " █ ", + " █ ", + "███", + } + + two := placeholder{ + "███", + " █", + "███", + "█ ", + "███", + } + + three := placeholder{ + "███", + " █", + "███", + " █", + "███", + } + + four := placeholder{ + "█ █", + "█ █", + "███", + " █", + " █", + } + + five := placeholder{ + "███", + "█ ", + "███", + " █", + "███", + } + + six := placeholder{ + "███", + "█ ", + "███", + "█ █", + "███", + } + + seven := placeholder{ + "███", + " █", + " █", + " █", + " █", + } + + eight := placeholder{ + "███", + "█ █", + "███", + "█ █", + "███", + } + + nine := placeholder{ + "███", + "█ █", + "███", + " █", + "███", + } + + // This array's type is "like": [10][5]string + // + // However: + // + "placeholder" is not equal to [5]string in type-wise. + // + Because: "placeholder" is a defined type, which is different + // from [5]string type. + // + [5]string is an unnamed type. + // + placeholder is a named type. + // + The underlying type of [5]string and placeholder is the same: + // [5]string + digits := [...]placeholder{ + zero, one, two, three, four, five, six, seven, eight, nine, + } + + // Explanation: digits[0] + // + Each element of clock has the same length. + // + So: Getting the length of only one element is OK. + // + This could be: "zero" or "one" and so on... Instead of: digits[0] + // + // The range clause below is ~equal to the following code: + // line := 0; line < 5; line++ + for line := range digits[0] { + // Print a line for each placeholder in digits + for digit := range digits { + fmt.Print(digits[digit][line], " ") + } + fmt.Println() + } +} diff --git a/x-tba/15-arrays-project-clock/03-printing-the-clock/main.go b/x-tba/15-arrays-project-clock/03-printing-the-clock/main.go new file mode 100644 index 0000000..48f2f38 --- /dev/null +++ b/x-tba/15-arrays-project-clock/03-printing-the-clock/main.go @@ -0,0 +1,146 @@ +// 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" + "time" +) + +// GOALS: +// 1. Get time +// 2. Create the clock array +// Use the digits array +// 3. Print the digits +// Use the clock array instead of the digits array +// 4. Add the colons + +func main() { + type placeholder [5]string + + zero := placeholder{ + "███", + "█ █", + "█ █", + "█ █", + "███", + } + + one := placeholder{ + "██ ", + " █ ", + " █ ", + " █ ", + "███", + } + + two := placeholder{ + "███", + " █", + "███", + "█ ", + "███", + } + + three := placeholder{ + "███", + " █", + "███", + " █", + "███", + } + + four := placeholder{ + "█ █", + "█ █", + "███", + " █", + " █", + } + + five := placeholder{ + "███", + "█ ", + "███", + " █", + "███", + } + + six := placeholder{ + "███", + "█ ", + "███", + "█ █", + "███", + } + + seven := placeholder{ + "███", + " █", + " █", + " █", + " █", + } + + eight := placeholder{ + "███", + "█ █", + "███", + "█ █", + "███", + } + + nine := placeholder{ + "███", + "█ █", + "███", + " █", + "███", + } + + colon := placeholder{ + " ", + " ░ ", + " ", + " ░ ", + " ", + } + + digits := [...]placeholder{ + zero, one, two, three, four, five, six, seven, eight, nine, + } + + now := time.Now() + hour, min, sec := now.Hour(), now.Minute(), now.Second() + + fmt.Printf("hour: %d, min: %d, sec: %d\n", hour, min, sec) + + // [8][5]string + clock := [...]placeholder{ + // extract the digits: 17 becomes, 1 and 7 respectively + digits[hour/10], digits[hour%10], + colon, + digits[min/10], digits[min%10], + colon, + digits[sec/10], digits[sec%10], + } + + for line := range clock[0] { + for digit := range clock { + fmt.Print(clock[digit][line], " ") + } + fmt.Println() + } + + // for line := range clock[0] { + // for digit := range clock { + // fmt.Print(clock[digit][line], " ") + // } + // fmt.Println() + // } +} diff --git a/x-tba/15-arrays-project-clock/04-updating-the-clock/main.go b/x-tba/15-arrays-project-clock/04-updating-the-clock/main.go new file mode 100644 index 0000000..437309a --- /dev/null +++ b/x-tba/15-arrays-project-clock/04-updating-the-clock/main.go @@ -0,0 +1,168 @@ +// 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" + "time" +) + +// GOALS: +// 1. Update the clock in a loop +// +// 2. Before the whole loop: +// Clear the screen once +// +// 3. Before each step of the loop: +// Move the cursor to top-left position + +// So, how to clear the screen and move the cursor? +// Check out the following constants. +// +// screenClear : When printed clears the screen. +// cursorMoveTop: Moves the cursor to top-left screen position +// +// + Note: This only works on bash command prompts. +// +// + For Go Playground, use these instead: +// screenClear = "\f" +// cursorMoveTop = "\f" +// +// See for more info: https://bluesock.org/~willkg/dev/ansi.html +const ( + screenClear = "\033[2J" + cursorMoveTop = "\033[H" +) + +func main() { + type placeholder [5]string + + zero := placeholder{ + "███", + "█ █", + "█ █", + "█ █", + "███", + } + + one := placeholder{ + "██ ", + " █ ", + " █ ", + " █ ", + "███", + } + + two := placeholder{ + "███", + " █", + "███", + "█ ", + "███", + } + + three := placeholder{ + "███", + " █", + "███", + " █", + "███", + } + + four := placeholder{ + "█ █", + "█ █", + "███", + " █", + " █", + } + + five := placeholder{ + "███", + "█ ", + "███", + " █", + "███", + } + + six := placeholder{ + "███", + "█ ", + "███", + "█ █", + "███", + } + + seven := placeholder{ + "███", + " █", + " █", + " █", + " █", + } + + eight := placeholder{ + "███", + "█ █", + "███", + "█ █", + "███", + } + + nine := placeholder{ + "███", + "█ █", + "███", + " █", + "███", + } + + colon := placeholder{ + " ", + " ░ ", + " ", + " ░ ", + " ", + } + + digits := [...]placeholder{ + zero, one, two, three, four, five, six, seven, eight, nine, + } + + // clears the command screen + fmt.Print(screenClear) + + // Go Playground will not run an infinite loop. + // So, instead, you may loop for 1000 times: + // for i := 0; i < 1000; i++ { + for { + // moves the cursor to the top-left position + fmt.Print(cursorMoveTop) + + now := time.Now() + hour, min, sec := now.Hour(), now.Minute(), now.Second() + + clock := [...]placeholder{ + digits[hour/10], digits[hour%10], + colon, + digits[min/10], digits[min%10], + colon, + digits[sec/10], digits[sec%10], + } + + for line := range clock[0] { + for digit := range clock { + fmt.Print(clock[digit][line], " ") + } + fmt.Println() + } + + // pause for 1 second + time.Sleep(time.Second) + } +} diff --git a/x-tba/15-arrays-project-clock/05-blink-the-separators/main.go b/x-tba/15-arrays-project-clock/05-blink-the-separators/main.go new file mode 100644 index 0000000..caf8122 --- /dev/null +++ b/x-tba/15-arrays-project-clock/05-blink-the-separators/main.go @@ -0,0 +1,150 @@ +// 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" + "time" +) + +// GOAL: +// Blink the colons + +const ( + screenClear = "\033[2J" + cursorMoveTop = "\033[H" +) + +func main() { + type placeholder [5]string + + zero := placeholder{ + "███", + "█ █", + "█ █", + "█ █", + "███", + } + + one := placeholder{ + "██ ", + " █ ", + " █ ", + " █ ", + "███", + } + + two := placeholder{ + "███", + " █", + "███", + "█ ", + "███", + } + + three := placeholder{ + "███", + " █", + "███", + " █", + "███", + } + + four := placeholder{ + "█ █", + "█ █", + "███", + " █", + " █", + } + + five := placeholder{ + "███", + "█ ", + "███", + " █", + "███", + } + + six := placeholder{ + "███", + "█ ", + "███", + "█ █", + "███", + } + + seven := placeholder{ + "███", + " █", + " █", + " █", + " █", + } + + eight := placeholder{ + "███", + "█ █", + "███", + "█ █", + "███", + } + + nine := placeholder{ + "███", + "█ █", + "███", + " █", + "███", + } + + colon := placeholder{ + " ", + " ░ ", + " ", + " ░ ", + " ", + } + + digits := [...]placeholder{ + zero, one, two, three, four, five, six, seven, eight, nine, + } + + fmt.Print(screenClear) + + for { + fmt.Print(cursorMoveTop) + + now := time.Now() + hour, min, sec := now.Hour(), now.Minute(), now.Second() + + clock := [...]placeholder{ + digits[hour/10], digits[hour%10], + colon, + digits[min/10], digits[min%10], + colon, + digits[sec/10], digits[sec%10], + } + + for line := range clock[0] { + for index, digit := range clock { + // colon blink + next := clock[index][line] + if digit == colon && sec%2 == 0 { + next = " " + } + fmt.Print(next, " ") + } + fmt.Println() + } + + // pause for 1 second + time.Sleep(time.Second) + // fmt.Println() + } +} diff --git a/x-tba/15-arrays-project-clock/06-full-annotated-code/main.go b/x-tba/15-arrays-project-clock/06-full-annotated-code/main.go new file mode 100644 index 0000000..87cf791 --- /dev/null +++ b/x-tba/15-arrays-project-clock/06-full-annotated-code/main.go @@ -0,0 +1,190 @@ +// 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" + "time" +) + +// screenClear : When printed clears the screen. +// cursorMoveTop: Moves the cursor to top-left screen position +// +// + Note: This only works on bash command prompts. +// +// + For Go Playground, use these instead: +// screenClear = "\f" +// cursorMoveTop = "\f" +// +// See for more info: https://bluesock.org/~willkg/dev/ansi.html +const ( + screenClear = "\033[2J" + cursorMoveTop = "\033[H" +) + +func main() { + // for keeping things easy to read and type-safe + type placeholder [5]string + + // put the digits (placeholders) into variables + // using the placeholder array type above + zero := placeholder{ + "███", + "█ █", + "█ █", + "█ █", + "███", + } + + one := placeholder{ + "██ ", + " █ ", + " █ ", + " █ ", + "███", + } + + two := placeholder{ + "███", + " █", + "███", + "█ ", + "███", + } + + three := placeholder{ + "███", + " █", + "███", + " █", + "███", + } + + four := placeholder{ + "█ █", + "█ █", + "███", + " █", + " █", + } + + five := placeholder{ + "███", + "█ ", + "███", + " █", + "███", + } + + six := placeholder{ + "███", + "█ ", + "███", + "█ █", + "███", + } + + seven := placeholder{ + "███", + " █", + " █", + " █", + " █", + } + + eight := placeholder{ + "███", + "█ █", + "███", + "█ █", + "███", + } + + nine := placeholder{ + "███", + "█ █", + "███", + " █", + "███", + } + + colon := placeholder{ + " ", + " ░ ", + " ", + " ░ ", + " ", + } + + // This array's type is "like": [10][5]string + // + // However: + // + "placeholder" is not equal to [5]string in type-wise. + // + Because: "placeholder" is a defined type, which is different + // from [5]string type. + // + [5]string is an unnamed type. + // + placeholder is a named type. + // + The underlying type of [5]string and placeholder is the same: + // [5]string + digits := [...]placeholder{ + zero, one, two, three, four, five, six, seven, eight, nine, + } + + // clears the command screen + fmt.Print(screenClear) + + // Go Playground will not run an infinite loop. + // Loop for example 1000 times instead, like this: + // for i := 0; i < 1000; i++ { ... } + for { + // moves the cursor to the top-left position + fmt.Print(cursorMoveTop) + + // get the current hour, minute and second + now := time.Now() + hour, min, sec := now.Hour(), now.Minute(), now.Second() + + // extract the digits: 17 becomes, 1 and 7 respectively + clock := [...]placeholder{ + digits[hour/10], digits[hour%10], + colon, + digits[min/10], digits[min%10], + colon, + digits[sec/10], digits[sec%10], + } + + // Explanation: clock[0] + // + Each element of clock has the same length. + // + So: Getting the length of only one element is OK. + // + This could be: "zero" or "one" and so on... Instead of: digits[0] + // + // The range clause below is ~equal to the following code: + // line := 0; line < len(clock[0]); line++ + for line := range clock[0] { + // Print a line for each placeholder in clock + for index, digit := range clock { + // Colon blink on every two seconds. + // + On each sec divisible by two, prints an empty line + // + Otherwise: prints the current pixel + next := clock[index][line] + if digit == colon && sec%2 == 0 { + next = " " + } + // Print the next line and, + // give it enough space for the next placeholder + fmt.Print(next, " ") + } + + // After each line of a placeholder, print a newline + fmt.Println() + } + + // pause for 1 second + time.Sleep(time.Second) + } +} diff --git a/x-tba/15-arrays-project-clock/goals.md b/x-tba/15-arrays-project-clock/goals.md new file mode 100644 index 0000000..78e31d9 --- /dev/null +++ b/x-tba/15-arrays-project-clock/goals.md @@ -0,0 +1,23 @@ +## GOALS: + +### STEP #1 — Create Digits +- [ ] Define a new placeholder type +- [ ] Create the digits +- [ ] Put them into the "digits" array +- [ ] Print them side-by-side + +### STEP #2 — Print the Clock +- [ ] Get the current time +- [ ] Create the clock array +- [ ] Print the clock +- [ ] Add the separators + +### STEP #3 — Animate the Clock +- [ ] Create an infinite loop to update the clock +- [ ] Update the clock every second +- [ ] Clear the screen before the infinite loop +- [ ] Move the cursor to the top-left corner of the screen before each + step of the infinite loop + +### BONUS +- [ ] Blink the separators \ No newline at end of file diff --git a/15-slices/01-intro/01-theory/main.go b/x-tba/16-slices/01-intro/01-theory/main.go similarity index 100% rename from 15-slices/01-intro/01-theory/main.go rename to x-tba/16-slices/01-intro/01-theory/main.go diff --git a/15-slices/01-intro/02-examples/main.go b/x-tba/16-slices/01-intro/02-examples/main.go similarity index 100% rename from 15-slices/01-intro/02-examples/main.go rename to x-tba/16-slices/01-intro/02-examples/main.go diff --git a/15-slices/02-append/01-theory/main.go b/x-tba/16-slices/02-append/01-theory/main.go similarity index 100% rename from 15-slices/02-append/01-theory/main.go rename to x-tba/16-slices/02-append/01-theory/main.go diff --git a/15-slices/02-append/02-examples/main.go b/x-tba/16-slices/02-append/02-examples/main.go similarity index 100% rename from 15-slices/02-append/02-examples/main.go rename to x-tba/16-slices/02-append/02-examples/main.go diff --git a/15-slices/03-slices-vs-arrays/01-with-arrays/main.go b/x-tba/16-slices/03-slices-vs-arrays/01-with-arrays/main.go similarity index 100% rename from 15-slices/03-slices-vs-arrays/01-with-arrays/main.go rename to x-tba/16-slices/03-slices-vs-arrays/01-with-arrays/main.go diff --git a/15-slices/03-slices-vs-arrays/02-with-slices/main.go b/x-tba/16-slices/03-slices-vs-arrays/02-with-slices/main.go similarity index 100% rename from 15-slices/03-slices-vs-arrays/02-with-slices/main.go rename to x-tba/16-slices/03-slices-vs-arrays/02-with-slices/main.go diff --git a/15-slices/04-copy/01-usage/main.go b/x-tba/16-slices/04-copy/01-usage/main.go similarity index 100% rename from 15-slices/04-copy/01-usage/main.go rename to x-tba/16-slices/04-copy/01-usage/main.go diff --git a/15-slices/04-copy/02-hacker-incident/main.go b/x-tba/16-slices/04-copy/02-hacker-incident/main.go similarity index 100% rename from 15-slices/04-copy/02-hacker-incident/main.go rename to x-tba/16-slices/04-copy/02-hacker-incident/main.go diff --git a/15-slices/05-whats-a-slice-in-real/01-theory/main.go b/x-tba/16-slices/05-whats-a-slice-in-real/01-theory/main.go similarity index 100% rename from 15-slices/05-whats-a-slice-in-real/01-theory/main.go rename to x-tba/16-slices/05-whats-a-slice-in-real/01-theory/main.go diff --git a/15-slices/05-whats-a-slice-in-real/02-example/main.go b/x-tba/16-slices/05-whats-a-slice-in-real/02-example/main.go similarity index 100% rename from 15-slices/05-whats-a-slice-in-real/02-example/main.go rename to x-tba/16-slices/05-whats-a-slice-in-real/02-example/main.go diff --git a/15-slices/06-slice-expressions/01-theory/main.go b/x-tba/16-slices/06-slice-expressions/01-theory/main.go similarity index 100% rename from 15-slices/06-slice-expressions/01-theory/main.go rename to x-tba/16-slices/06-slice-expressions/01-theory/main.go diff --git a/15-slices/06-slice-expressions/02-examples/main.go b/x-tba/16-slices/06-slice-expressions/02-examples/main.go similarity index 100% rename from 15-slices/06-slice-expressions/02-examples/main.go rename to x-tba/16-slices/06-slice-expressions/02-examples/main.go diff --git a/15-slices/07-full-slice-expressions/main.go b/x-tba/16-slices/07-full-slice-expressions/main.go similarity index 100% rename from 15-slices/07-full-slice-expressions/main.go rename to x-tba/16-slices/07-full-slice-expressions/main.go diff --git a/15-slices/08-make/main.go b/x-tba/16-slices/08-make/main.go similarity index 100% rename from 15-slices/08-make/main.go rename to x-tba/16-slices/08-make/main.go diff --git a/15-slices/09-bouncing-ball-challenge/01-challenge/main.go b/x-tba/16-slices/09-bouncing-ball-challenge/01-challenge/main.go similarity index 100% rename from 15-slices/09-bouncing-ball-challenge/01-challenge/main.go rename to x-tba/16-slices/09-bouncing-ball-challenge/01-challenge/main.go diff --git a/15-slices/09-bouncing-ball-challenge/02-solution-draw-the-board/main.go b/x-tba/16-slices/09-bouncing-ball-challenge/02-solution-draw-the-board/main.go similarity index 100% rename from 15-slices/09-bouncing-ball-challenge/02-solution-draw-the-board/main.go rename to x-tba/16-slices/09-bouncing-ball-challenge/02-solution-draw-the-board/main.go diff --git a/15-slices/09-bouncing-ball-challenge/03-solution-drawing-loop/main.go b/x-tba/16-slices/09-bouncing-ball-challenge/03-solution-drawing-loop/main.go similarity index 100% rename from 15-slices/09-bouncing-ball-challenge/03-solution-drawing-loop/main.go rename to x-tba/16-slices/09-bouncing-ball-challenge/03-solution-drawing-loop/main.go diff --git a/15-slices/09-bouncing-ball-challenge/04-solution-final/main.go b/x-tba/16-slices/09-bouncing-ball-challenge/04-solution-final/main.go similarity index 100% rename from 15-slices/09-bouncing-ball-challenge/04-solution-final/main.go rename to x-tba/16-slices/09-bouncing-ball-challenge/04-solution-final/main.go diff --git a/15-slices/README-WARNING.md b/x-tba/16-slices/README-WARNING.md similarity index 100% rename from 15-slices/README-WARNING.md rename to x-tba/16-slices/README-WARNING.md diff --git a/16-strings-revisited/01-byte/main.go b/x-tba/17-strings-revisited/01-byte/main.go similarity index 100% rename from 16-strings-revisited/01-byte/main.go rename to x-tba/17-strings-revisited/01-byte/main.go diff --git a/16-strings-revisited/02-string-byte-slice/main.go b/x-tba/17-strings-revisited/02-string-byte-slice/main.go similarity index 100% rename from 16-strings-revisited/02-string-byte-slice/main.go rename to x-tba/17-strings-revisited/02-string-byte-slice/main.go diff --git a/16-strings-revisited/03-string-indexing-slicing/main.go b/x-tba/17-strings-revisited/03-string-indexing-slicing/main.go similarity index 100% rename from 16-strings-revisited/03-string-indexing-slicing/main.go rename to x-tba/17-strings-revisited/03-string-indexing-slicing/main.go diff --git a/16-strings-revisited/04-masker-challenge/main.go b/x-tba/17-strings-revisited/04-masker-challenge/main.go similarity index 100% rename from 16-strings-revisited/04-masker-challenge/main.go rename to x-tba/17-strings-revisited/04-masker-challenge/main.go diff --git a/16-strings-revisited/04-masker-challenge/spam.txt b/x-tba/17-strings-revisited/04-masker-challenge/spam.txt similarity index 100% rename from 16-strings-revisited/04-masker-challenge/spam.txt rename to x-tba/17-strings-revisited/04-masker-challenge/spam.txt diff --git a/16-strings-revisited/05-masker-solution/main.go b/x-tba/17-strings-revisited/05-masker-solution/main.go similarity index 100% rename from 16-strings-revisited/05-masker-solution/main.go rename to x-tba/17-strings-revisited/05-masker-solution/main.go diff --git a/16-strings-revisited/05-masker-solution/spam.txt b/x-tba/17-strings-revisited/05-masker-solution/spam.txt similarity index 100% rename from 16-strings-revisited/05-masker-solution/spam.txt rename to x-tba/17-strings-revisited/05-masker-solution/spam.txt diff --git a/16-strings-revisited/06-encoding/main.go b/x-tba/17-strings-revisited/06-encoding/main.go similarity index 100% rename from 16-strings-revisited/06-encoding/main.go rename to x-tba/17-strings-revisited/06-encoding/main.go diff --git a/16-strings-revisited/07-encoding-examples/main.go b/x-tba/17-strings-revisited/07-encoding-examples/main.go similarity index 100% rename from 16-strings-revisited/07-encoding-examples/main.go rename to x-tba/17-strings-revisited/07-encoding-examples/main.go diff --git a/16-strings-revisited/08-wrapper-example/main.go b/x-tba/17-strings-revisited/08-wrapper-example/main.go similarity index 100% rename from 16-strings-revisited/08-wrapper-example/main.go rename to x-tba/17-strings-revisited/08-wrapper-example/main.go diff --git a/16-strings-revisited/09-internals/main.go b/x-tba/17-strings-revisited/09-internals/main.go similarity index 100% rename from 16-strings-revisited/09-internals/main.go rename to x-tba/17-strings-revisited/09-internals/main.go diff --git a/16-strings-revisited/charset-table/main.go b/x-tba/17-strings-revisited/charset-table/main.go similarity index 100% rename from 16-strings-revisited/charset-table/main.go rename to x-tba/17-strings-revisited/charset-table/main.go diff --git a/16-strings-revisited/notes.md b/x-tba/17-strings-revisited/notes.md similarity index 100% rename from 16-strings-revisited/notes.md rename to x-tba/17-strings-revisited/notes.md diff --git a/17-maps/README.md b/x-tba/18-maps/README.md similarity index 100% rename from 17-maps/README.md rename to x-tba/18-maps/README.md diff --git a/17-maps/_experimental/main.go b/x-tba/18-maps/_experimental/main.go similarity index 100% rename from 17-maps/_experimental/main.go rename to x-tba/18-maps/_experimental/main.go diff --git a/18-structs/README.md b/x-tba/19-structs/README.md similarity index 100% rename from 18-structs/README.md rename to x-tba/19-structs/README.md diff --git a/19-functions/README.md b/x-tba/20-functions/README.md similarity index 100% rename from 19-functions/README.md rename to x-tba/20-functions/README.md diff --git a/20-pointers/README.md b/x-tba/20-pointers/README.md similarity index 100% rename from 20-pointers/README.md rename to x-tba/20-pointers/README.md diff --git a/21-closures/README.md b/x-tba/21-closures/README.md similarity index 100% rename from 21-closures/README.md rename to x-tba/21-closures/README.md diff --git a/22-deferred-funcs/README.md b/x-tba/22-deferred-funcs/README.md similarity index 100% rename from 22-deferred-funcs/README.md rename to x-tba/22-deferred-funcs/README.md diff --git a/23-variadic-funcs/README.md b/x-tba/23-variadic-funcs/README.md similarity index 100% rename from 23-variadic-funcs/README.md rename to x-tba/23-variadic-funcs/README.md diff --git a/24-methods/README.md b/x-tba/24-methods/README.md similarity index 100% rename from 24-methods/README.md rename to x-tba/24-methods/README.md diff --git a/25-method-values/README.md b/x-tba/25-method-values/README.md similarity index 100% rename from 25-method-values/README.md rename to x-tba/25-method-values/README.md diff --git a/26-interfaces/README.md b/x-tba/26-interfaces/README.md similarity index 100% rename from 26-interfaces/README.md rename to x-tba/26-interfaces/README.md diff --git a/27-embedding/README.md b/x-tba/27-embedding/README.md similarity index 100% rename from 27-embedding/README.md rename to x-tba/27-embedding/README.md diff --git a/28-concurrency/README.md b/x-tba/28-concurrency/README.md similarity index 100% rename from 28-concurrency/README.md rename to x-tba/28-concurrency/README.md diff --git a/29-tba/README.md b/x-tba/README.md similarity index 100% rename from 29-tba/README.md rename to x-tba/README.md