From f1894e1cc86221c7b0a4a210b62f25f4835e055a Mon Sep 17 00:00:00 2001 From: vinicius Date: Sun, 13 Oct 2024 14:50:21 -0300 Subject: [PATCH 1/8] feat: using slices and range function --- src/udemy/go_ted/section13/slices.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/udemy/go_ted/section13/slices.go diff --git a/src/udemy/go_ted/section13/slices.go b/src/udemy/go_ted/section13/slices.go new file mode 100644 index 0000000..9170f9e --- /dev/null +++ b/src/udemy/go_ted/section13/slices.go @@ -0,0 +1,16 @@ +package main + +import "fmt" + +func main() { + xs := []string{"Almond Biscotti Café", "Banana Pudding ", "Balsamic Strawberry (GF)", "Bittersweet Chocolate (GF)", "Blueberry Pancake (GF)", "Bourbon Turtle (GF)", "Browned Butter Cookie Dough", "Chocolate Covered Black Cherry (GF)", "Chocolate Fudge Brownie", "Chocolate Peanut Butter (GF)", "Coffee (GF)", "Cookies & Cream", "Fresh Basil (GF)", "Garden Mint Chip (GF)", "Lavender Lemon Honey (GF)", "Lemon Bar", "Madagascar Vanilla (GF)", "Matcha (GF)", "Midnight Chocolate Sorbet (GF, V)", "Non-Dairy Chocolate Peanut Butter (GF, V)", "Non-Dairy Coconut Matcha (GF, V)", "Non-Dairy Sunbutter Cinnamon (GF, V)", "Orange Cream (GF) ", "Peanut Butter Cookie Dough", "Raspberry Sorbet (GF, V)", "Salty Caramel (GF)", "Slate Slate Different", "Strawberry Lemonade Sorbet (GF, V)", "Vanilla Caramel Blondie", "Vietnamese Cinnamon (GF)", "Wolverine Tracks (GF)"} + fmt.Println(xs) + fmt.Printf("Type of variable\t%T\n", xs) + + // 'i' is the counter + // v stands for value and will receive each item in slice to be manipulated + for i, v := range xs { + // to print index and value + fmt.Printf("%v - %v\n", i, v) + } +} From 532800066c32da16dce1277443c789a2c54f4707 Mon Sep 17 00:00:00 2001 From: vinicius Date: Sun, 13 Oct 2024 14:53:06 -0300 Subject: [PATCH 2/8] chore: added comments --- src/udemy/go_ted/section13/slices.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/udemy/go_ted/section13/slices.go b/src/udemy/go_ted/section13/slices.go index 9170f9e..0f69045 100644 --- a/src/udemy/go_ted/section13/slices.go +++ b/src/udemy/go_ted/section13/slices.go @@ -4,7 +4,9 @@ import "fmt" func main() { xs := []string{"Almond Biscotti Café", "Banana Pudding ", "Balsamic Strawberry (GF)", "Bittersweet Chocolate (GF)", "Blueberry Pancake (GF)", "Bourbon Turtle (GF)", "Browned Butter Cookie Dough", "Chocolate Covered Black Cherry (GF)", "Chocolate Fudge Brownie", "Chocolate Peanut Butter (GF)", "Coffee (GF)", "Cookies & Cream", "Fresh Basil (GF)", "Garden Mint Chip (GF)", "Lavender Lemon Honey (GF)", "Lemon Bar", "Madagascar Vanilla (GF)", "Matcha (GF)", "Midnight Chocolate Sorbet (GF, V)", "Non-Dairy Chocolate Peanut Butter (GF, V)", "Non-Dairy Coconut Matcha (GF, V)", "Non-Dairy Sunbutter Cinnamon (GF, V)", "Orange Cream (GF) ", "Peanut Butter Cookie Dough", "Raspberry Sorbet (GF, V)", "Salty Caramel (GF)", "Slate Slate Different", "Strawberry Lemonade Sorbet (GF, V)", "Vanilla Caramel Blondie", "Vietnamese Cinnamon (GF)", "Wolverine Tracks (GF)"} + // print entire values in variable at once fmt.Println(xs) + // %T -> show the type of variable fmt.Printf("Type of variable\t%T\n", xs) // 'i' is the counter From 8243222fc966115b3cbd0c6fd992ce2b3fd5984d Mon Sep 17 00:00:00 2001 From: vinicius Date: Sun, 13 Oct 2024 15:09:06 -0300 Subject: [PATCH 3/8] feat: new tests --- src/udemy/go_ted/section13/slices.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/udemy/go_ted/section13/slices.go b/src/udemy/go_ted/section13/slices.go index 0f69045..f7c1648 100644 --- a/src/udemy/go_ted/section13/slices.go +++ b/src/udemy/go_ted/section13/slices.go @@ -15,4 +15,12 @@ func main() { // to print index and value fmt.Printf("%v - %v\n", i, v) } + fmt.Println() + + // new values for the follwing test + xs = []string{"Al", "Mon", "De", "Ga"} + // you can use too blank identifier ('_')to ignore index counter and just get the value + for _, v := range xs { + fmt.Printf("%v\n", v) + } } From 71eb81c156cbdf7f4acd6974d674833aeb99d887 Mon Sep 17 00:00:00 2001 From: vinicius Date: Mon, 14 Oct 2024 00:38:39 -0300 Subject: [PATCH 4/8] feat: index slice --- src/udemy/go_ted/section13/slice_index.go | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/udemy/go_ted/section13/slice_index.go diff --git a/src/udemy/go_ted/section13/slice_index.go b/src/udemy/go_ted/section13/slice_index.go new file mode 100644 index 0000000..294c694 --- /dev/null +++ b/src/udemy/go_ted/section13/slice_index.go @@ -0,0 +1,24 @@ +package main + +import "fmt" + +func main() { + xs := []string{"Almond Biscotti Café", "Banana Pudding ", "Balsamic Strawberry (GF)"} + fmt.Println(xs) + fmt.Printf("Type of variable\t%T\n", xs) + + for _, v := range xs { + fmt.Printf("%v\n", v) + } + + fmt.Println("----------------------") + fmt.Println(xs[0]) + fmt.Println(xs[1]) + fmt.Println(xs[2]) + fmt.Println("len of slice", len(xs)) + + fmt.Println("----------------------") + for i := 0; i < len(xs); i++ { + fmt.Println(xs[i]) + } +} From 4a89607543b69a777770ea650c0ec620328707ea Mon Sep 17 00:00:00 2001 From: vinicius Date: Mon, 14 Oct 2024 01:07:27 -0300 Subject: [PATCH 5/8] feat: slice manipulation this was a great class! --- src/udemy/go_ted/section13/slice_append.go | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/udemy/go_ted/section13/slice_append.go diff --git a/src/udemy/go_ted/section13/slice_append.go b/src/udemy/go_ted/section13/slice_append.go new file mode 100644 index 0000000..253c2a0 --- /dev/null +++ b/src/udemy/go_ted/section13/slice_append.go @@ -0,0 +1,62 @@ +package main + +import ( + "fmt" + "slices" +) + +func main() { + xi := []int{42, 43, 44} + fmt.Println(xi) + fmt.Println("----------------------") + + // can appende variadic values + xi = append(xi, 12, 13, 14, 15, 555, 111, 222) + fmt.Println(xi) + fmt.Println("----------------------") + + xi = append(xi, 6943, 234, 2349) + fmt.Println(xi) + fmt.Println("----------------------") + + // print len exclusive (one way) + fmt.Println(xi[len(xi)-1]) + fmt.Println("----------------------") + + // [inclusive:exclusive] + fmt.Printf("inc/exc xi: %#v\n", xi[0:4]) + fmt.Println("----------------------") + + // [exclusive] + fmt.Printf("exclusive xi: %#v\n", xi[:7]) + fmt.Println("----------------------") + + // [inclusive] + fmt.Printf("inclusive xi: %#v\n", xi[7:]) + fmt.Println("----------------------") + + // [all] can be just variable name too + fmt.Printf("all xi: %#v\n", xi[:]) + fmt.Println("----------------------") + + // to delete or remove + xi = append(xi[:4], xi[5:]...) + fmt.Printf("deleting xi: %v\n", xi) + fmt.Println("----------------------") + + // copy slice + xiCopy := make([]int, len(xi)) + copy(xiCopy, xi) + fmt.Println("copy xi:", xiCopy) + fmt.Println("----------------------") + + // reverse slice + slices.Reverse(xi) + fmt.Printf("reverse xi: %v\n", xi) + fmt.Println("----------------------") + + // sort slice + slices.Sort(xi) + fmt.Printf("sorted xi: %v\n", xi) + fmt.Println("----------------------") +} From 5eced816a6607be12befe2b1a8f815079641719c Mon Sep 17 00:00:00 2001 From: vinicius Date: Mon, 14 Oct 2024 21:26:20 -0300 Subject: [PATCH 6/8] feat: using make to allocate memory to a slice --- src/udemy/go_ted/section13/slice_make.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/udemy/go_ted/section13/slice_make.go diff --git a/src/udemy/go_ted/section13/slice_make.go b/src/udemy/go_ted/section13/slice_make.go new file mode 100644 index 0000000..3b00cb2 --- /dev/null +++ b/src/udemy/go_ted/section13/slice_make.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +func main() { + si := []string{"a", "b", "c"} + fmt.Printf("si: %v\n", si) + + xi := make([]int, 0, 10) + + fmt.Printf("xi: %v\n", xi) + fmt.Printf("len(xi): %v\n", len(xi)) + fmt.Printf("cap(xi): %v\n", cap(xi)) + xi = append(xi, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + fmt.Printf("xi: %v\n", xi) + + fmt.Println("----------------") + + xi = append(xi, 11, 12, 13, 14, 15) + fmt.Printf("xi: %v\n", xi) + fmt.Printf("len(xi): %v\n", len(xi)) + fmt.Printf("cap(xi): %v\n", cap(xi)) +} From cecb934953c1f99ba04c22f6db0321d6f75b9420 Mon Sep 17 00:00:00 2001 From: vinicius Date: Mon, 14 Oct 2024 21:41:22 -0300 Subject: [PATCH 7/8] feat: multidimensional slice --- .../section13/slice_multidimensional.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/udemy/go_ted/section13/slice_multidimensional.go diff --git a/src/udemy/go_ted/section13/slice_multidimensional.go b/src/udemy/go_ted/section13/slice_multidimensional.go new file mode 100644 index 0000000..c219882 --- /dev/null +++ b/src/udemy/go_ted/section13/slice_multidimensional.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func main() { + // slices in slices + jb := []string{"James", "Bond", "Martini", "Chocolate"} + jm := []string{"Jenny", "Moneypenny", "Guiness", "Wolverine Tracks"} + fmt.Printf("jb: %v\n", jb) + fmt.Printf("jm: %v\n", jm) + + xp := [][]string{jb, jm} + xxp := [][]string{jb, jm} + xxxp := [][][]string{xp, xxp} + fmt.Printf("xxp: %v\n", xxp) + fmt.Printf("xp: %v\n", xp) + fmt.Printf("xxxp: %v\n", xxxp) + +} From 3b431bcb026919d3062d040a5852616c73841f9d Mon Sep 17 00:00:00 2001 From: vinicius Date: Mon, 14 Oct 2024 22:17:18 -0300 Subject: [PATCH 8/8] feat: underlying with arrays and slices --- src/udemy/go_ted/section13/underying_array.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/udemy/go_ted/section13/underying_array.go diff --git a/src/udemy/go_ted/section13/underying_array.go b/src/udemy/go_ted/section13/underying_array.go new file mode 100644 index 0000000..956a13f --- /dev/null +++ b/src/udemy/go_ted/section13/underying_array.go @@ -0,0 +1,13 @@ +package main + +import "fmt" + +func main() { + a := []int{1, 2, 3, 4, 5, 6} + b := make([]int, len(a)) + copy(b, a) + b[0] = 7 + fmt.Printf("b: %v\n", b) + fmt.Printf("a: %v\n", a) + +}