From 3c6a90101d5b0cbdfd9ce5a9c0ecb70263ae4066 Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 28 Oct 2018 18:18:13 +0800 Subject: [PATCH] update --- reflect/overview/main.go | 22 +++++++ slice/append/main.go | 5 ++ slice/basic/main.go | 51 ++++++++++++++++ slice/range/main.go | 33 ++++++++++- slice/tips/main.go | 123 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 reflect/overview/main.go create mode 100644 slice/basic/main.go create mode 100644 slice/tips/main.go diff --git a/reflect/overview/main.go b/reflect/overview/main.go new file mode 100644 index 0000000..f678c98 --- /dev/null +++ b/reflect/overview/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "reflect" +) + +func main() { + + type A = [16]int16 + + var c <-chan map[A][]byte + tc := reflect.TypeOf(c) + fmt.Println(tc.Kind()) // chan + fmt.Println(tc.ChanDir()) // <-chan + + // Elem returns a type's element type. + // It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice. + tm := tc.Elem() + ta, tb := tm.Key(), tm.Elem() + fmt.Println(tm.Kind(), ta.Kind(), tb.Kind()) +} diff --git a/slice/append/main.go b/slice/append/main.go index b325542..17af6de 100644 --- a/slice/append/main.go +++ b/slice/append/main.go @@ -24,4 +24,9 @@ func case2() { s1 = append(s1, "world") *p1 = "hello2" fmt.Printf("value of p1 is %s, value of s1[0] is %s \n", *p1, s1[0]) + + // var a uint = 1 + // var _ = map[uint]int{a: 123} // okay + // var _ = []int{a: 100} // error: index must be non-negative integer constant + // var _ = [5]int{a: 100} // e } diff --git a/slice/basic/main.go b/slice/basic/main.go new file mode 100644 index 0000000..c24a790 --- /dev/null +++ b/slice/basic/main.go @@ -0,0 +1,51 @@ +package main + +import "fmt" + +type language struct { + name string + year int +} + +type LangCategory struct { + dynamic bool + strong bool +} + +func main() { + + // Nested Composite Literals Can Be Simplified + l := [...]language{ + {"C", 1972}, + {"Python", 1991}, + } + + fmt.Println(l) + + m := map[LangCategory]map[string]int{ + {true, true}: { + "python": 1991, + "Erlang": 1986, + }, + } + + fmt.Println(m) + + var s []string + + for i, n := range s { + fmt.Println(i, n) + + } + fmt.Println(s) + fmt.Println(s == nil) + + var m2 map[int][]string + + fmt.Println(m2) + fmt.Println(m2 == nil) + for _, i := range m2 { + fmt.Println(i) + } + +} diff --git a/slice/range/main.go b/slice/range/main.go index 35a1922..55d1734 100644 --- a/slice/range/main.go +++ b/slice/range/main.go @@ -8,7 +8,7 @@ type u struct { func main() { s := []u{ - u{ + { name: "sean", }, } @@ -18,4 +18,35 @@ func main() { } fmt.Println(s) + for range s { + + } + + // the element iteration variable is omitted + for key := range s { + fmt.Println(key) + } + + for key, element := range s { + fmt.Println(key, element) + } + + type Person struct { + name string + age int + } + + persons := [...]Person{{"Alice", 28}, {"Bob", 25}} + for i, p := range persons { + fmt.Println("before", i, p) + // This modification has no effects on the iteration, + // for the ranged array is a copy of the persons array. + persons[1].name = "Jack" + fmt.Println("after", i, p) + // This modification has not effects on the persons array, + // for p is just a copy of a copy of one persons element. + p.age = 31 + } + fmt.Println("persons:", persons) + } diff --git a/slice/tips/main.go b/slice/tips/main.go new file mode 100644 index 0000000..b7dc2f2 --- /dev/null +++ b/slice/tips/main.go @@ -0,0 +1,123 @@ +package main + +import ( + "fmt" +) + +func main() { + // cloneSlice() + // deleteElement() + // deleteElement2() + fmt.Println("insertSliceIntoAnother") + insertSliceIntoAnother() + + fmt.Println("unshift") + unshift() + + fmt.Println("popfront") + popfront() + fmt.Println("popback") + popBack() + + s := []string{"a", "d", "c", "d", "b"} + + res := DeleteElements(s, func(s string) bool { + return s != "d" + }, true) + fmt.Println(res) + fmt.Println(s) +} + +type Person struct { + name string + age int +} + +func cloneSlice() { + var s []Person = nil + s2 := s[:] + sClone := append(s[:0:0], s...) + fmt.Println(sClone == nil) + fmt.Println(s2) +} + +func deleteElement() { + s := []string{"a", "b", "c", "d"} + s = append(s[:1], s[2:]...) + fmt.Println(s) +} + +func deleteElement2() { + s := []string{"a", "b", "c", "d"} + i := 1 + fmt.Println(s[i+1:]) + res := copy(s[i:], s[i+1:]) + fmt.Println(res) + fmt.Println(s) + fmt.Println(s[:3]) +} + +func DeleteElements(s []string, shouldKeep func(string) bool, clear bool) []string { + result := make([]string, 0, len(s)) + for _, v := range s { + if shouldKeep(v) { + result = append(result, v) + } + } + if clear { // avoid memory leaking + temp := s[len(result):] + for i := range temp { + temp[i] = "" // t0 is a zero value literal of T. + } + } + return result +} + +func insertSliceIntoAnother() { + s := []string{"a", "b", "c", "d"} + + i := 2 // insertion position is 2 + + e := []string{"e", "f", "g"} + + s = append(s[:i], append(e, s[i:]...)...) + + fmt.Println(s) +} + +func push() { + s := []string{"a", "b", "c", "d"} + s = append(s, []string{"e", "f"}...) +} + +func pushfront() { + s := []string{"a", "b", "c", "d"} + f := "e" + s = append([]string{f}, s...) + fmt.Println(s) +} + +func unshift() { + s := []string{"a", "b", "c", "d"} + elements := []string{"e", "f"} + s = append(elements, s...) + fmt.Println(s) +} + +func popfront() { + s := []string{"a", "b", "c", "d"} + s, e := s[1:], s[0] + fmt.Println(e) + fmt.Println(s) +} + +func popBack() { + s := []string{"a", "b", "c", "d"} + s, e := s[:len(s)-1], s[len(s)-1] + fmt.Println(e) + fmt.Println(s) +} + +func modifyArray() { + []int{1, 2, 3}[1] = 9 +}