-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/xujihui1985/learninggo
- Loading branch information
Showing
5 changed files
with
233 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |