Skip to content

Commit

Permalink
Merge pull request #1 from 0bvim/feat/section13
Browse files Browse the repository at this point in the history
feat/section13
  • Loading branch information
0bvim authored Oct 15, 2024
2 parents 7e66776 + 3b431bc commit ea14b75
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/udemy/go_ted/section13/slice_append.go
Original file line number Diff line number Diff line change
@@ -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("----------------------")
}
24 changes: 24 additions & 0 deletions src/udemy/go_ted/section13/slice_index.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
23 changes: 23 additions & 0 deletions src/udemy/go_ted/section13/slice_make.go
Original file line number Diff line number Diff line change
@@ -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))
}
19 changes: 19 additions & 0 deletions src/udemy/go_ted/section13/slice_multidimensional.go
Original file line number Diff line number Diff line change
@@ -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)

}
26 changes: 26 additions & 0 deletions src/udemy/go_ted/section13/slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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)"}
// 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
// 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)
}
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)
}
}
13 changes: 13 additions & 0 deletions src/udemy/go_ted/section13/underying_array.go
Original file line number Diff line number Diff line change
@@ -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)

}

0 comments on commit ea14b75

Please sign in to comment.