forked from geektime-geekbang/go_learning
-
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.
- Loading branch information
chaocai
committed
Mar 11, 2019
1 parent
0d7941b
commit 1920f44
Showing
3 changed files
with
65 additions
and
0 deletions.
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,34 @@ | ||
package my_map | ||
|
||
import "testing" | ||
|
||
func TestInitMap(t *testing.T) { | ||
m1 := map[int]int{1: 1, 2: 4, 3: 9} | ||
t.Log(m1[2]) | ||
t.Logf("len m1=%d", len(m1)) | ||
m2 := map[int]int{} | ||
m2[4] = 16 | ||
t.Logf("len m2=%d", len(m2)) | ||
m3 := make(map[int]int, 10) | ||
t.Logf("len m3=%d", len(m3)) | ||
} | ||
|
||
func TestAccessNotExistingKey(t *testing.T) { | ||
m1 := map[int]int{} | ||
t.Log(m1[1]) | ||
m1[2] = 0 | ||
t.Log(m1[2]) | ||
m1[3] = 0 | ||
if v, ok := m1[3]; ok { | ||
t.Logf("Key 3's value is %d", v) | ||
} else { | ||
t.Log("key 3 is not existing.") | ||
} | ||
} | ||
|
||
func TestTravelMap(t *testing.T) { | ||
m1 := map[int]int{1: 1, 2: 4, 3: 9} | ||
for k, v := range m1 { | ||
t.Log(k, v) | ||
} | ||
} |
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,31 @@ | ||
package map_ext | ||
|
||
import "testing" | ||
|
||
func TestMapWithFunValue(t *testing.T) { | ||
m := map[int]func(op int) int{} | ||
m[1] = func(op int) int { return op } | ||
m[2] = func(op int) int { return op * op } | ||
m[3] = func(op int) int { return op * op * op } | ||
t.Log(m[1](2), m[2](2), m[3](2)) | ||
} | ||
|
||
func TestMapForSet(t *testing.T) { | ||
mySet := map[int]bool{} | ||
mySet[1] = true | ||
n := 3 | ||
if mySet[n] { | ||
t.Logf("%d is existing", n) | ||
} else { | ||
t.Logf("%d is not existing", n) | ||
} | ||
mySet[3] = true | ||
t.Log(len(mySet)) | ||
delete(mySet, 1) | ||
n = 1 | ||
if mySet[n] { | ||
t.Logf("%d is existing", n) | ||
} else { | ||
t.Logf("%d is not existing", n) | ||
} | ||
} |
Binary file not shown.