forked from lifei6671/interview-go
-
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
lifeilin01
committed
May 23, 2021
1 parent
8889499
commit 1576d64
Showing
2 changed files
with
35 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
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 @@ | ||
# sync.Map 的用法 | ||
|
||
## 问题 | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
func main(){ | ||
var m sync.Map | ||
m.Store("address",map[string]string{"province":"江苏","city":"南京"}) | ||
v,_ := m.Load("address") | ||
fmt.Println(v["province"]) | ||
} | ||
``` | ||
|
||
- A,江苏; | ||
- B`,v["province"]`取值错误; | ||
- C,`m.Store`存储错误; | ||
- D,不知道 | ||
|
||
## 解析 | ||
|
||
`invalid operation: v["province"] (type interface {} does not support indexing)` | ||
因为 `func (m *Map) Store(key interface{}, value interface{})` | ||
所以 `v`类型是 `interface {}` ,这里需要一个类型断言 | ||
|
||
```go | ||
fmt.Println(v.(map[string]string)["province"]) //江苏 | ||
``` |