Skip to content

Commit

Permalink
update: map delete key
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatDiscovery committed Jul 26, 2024
1 parent 823a074 commit a072eec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
28 changes: 0 additions & 28 deletions test/basic/collection/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,3 @@ func TestSlice(t *testing.T) {
cutSlice := testCase[2]
fmt.Printf("cutSlice=%v", cutSlice)
}

func TestMap(t *testing.T) {
var siteMap map[string]string /*创建集合 */
siteMap = make(map[string]string)

/* map 插入 key - value 对,各个国家对应的首都 */
siteMap["Google"] = "谷歌"
siteMap["Runoob"] = "菜鸟教程"
siteMap["Baidu"] = "百度"
siteMap["Wiki"] = "维基百科"

/*使用键输出地图值 */
for site := range siteMap {
fmt.Println(site, "首都是", siteMap[site])
}

/*查看元素在集合中是否存在 */
name, ok := siteMap["Facebook"] /*如果确定是真实的,则存在,否则不存在 */
/*fmt.Println(capital) */
/*fmt.Println(ok) */
if ok {
fmt.Println("Facebook 的 站点是", name)
} else {
fmt.Println("Facebook 站点不存在")
}

fmt.Println("长度是", len(siteMap))
}
35 changes: 35 additions & 0 deletions test/basic/collection/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

Expand All @@ -22,3 +23,37 @@ func TestMapPointer(t *testing.T) {
fmt.Println("not ok, v", v)
}
}

func TestMap(t *testing.T) {
var siteMap map[string]string /*创建集合 */
siteMap = make(map[string]string)

/* map 插入 key - value 对,各个国家对应的首都 */
siteMap["Google"] = "谷歌"
siteMap["Runoob"] = "菜鸟教程"
siteMap["Baidu"] = "百度"
siteMap["Wiki"] = "维基百科"

/*使用键输出地图值 */
for site := range siteMap {
fmt.Println(site, "首都是", siteMap[site])
}

/*查看元素在集合中是否存在 */
name, ok := siteMap["Facebook"] /*如果确定是真实的,则存在,否则不存在 */
/*fmt.Println(capital) */
/*fmt.Println(ok) */
if ok {
fmt.Println("Facebook 的 站点是", name)
} else {
fmt.Println("Facebook 站点不存在")
}

fmt.Println("长度是", len(siteMap))

// 删除key
delete(siteMap, "Google")
fmt.Println("长度是", len(siteMap))
name, ok = siteMap["Google"]
assert.Equal(t, "", name)
}

0 comments on commit a072eec

Please sign in to comment.