Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
xujihui1985 committed Oct 29, 2018
2 parents d3ae2f4 + 3c6a901 commit 935813a
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 1 deletion.
22 changes: 22 additions & 0 deletions reflect/overview/main.go
Original file line number Diff line number Diff line change
@@ -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())
}
5 changes: 5 additions & 0 deletions slice/append/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
51 changes: 51 additions & 0 deletions slice/basic/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

}
33 changes: 32 additions & 1 deletion slice/range/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type u struct {

func main() {
s := []u{
u{
{
name: "sean",
},
}
Expand All @@ -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)

}
123 changes: 123 additions & 0 deletions slice/tips/main.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 935813a

Please sign in to comment.