我只是想在sync.Map上做一些简单的创建或添加
gore> :import sync
gore> var sm sync.Map
gore> sm.Store(12345,1)
gore> result, ok := sm.Load(12345)
1
true
gore> newr := result +1
# command-line-arguments
/var/folders/kl/n95_c8j15wn1784jmsq08mq80000gn/T/112740772/gore_session.go:21:17: invalid operation: result + 1 (mismatched types interface {} and int)
error: exit status 2
exit status 2result是爬虫上的1,但不能用1添加
发布于 2018-06-15 04:37:10
Go程序设计语言规范 类型断言 对于接口类型的表达式x和类型T的表达式,主表达式 十.(T) 断言x不是零,存储在x中的值是T类型,符号x.(T)称为类型断言。
错误消息告诉所有人:
invalid operation: result + 1 (mismatched types interface {} and int)在result类型interface {}上为int使用类型断言
package main
import (
"fmt"
"sync"
)
func main() {
var sm sync.Map
sm.Store(12345, 1)
result, ok := sm.Load(12345)
fmt.Println(result, ok)
newr := result.(int) + 1
fmt.Println(newr)
}操场:https://play.golang.org/p/qotBVR4fSNV
输出:
1 true
2https://stackoverflow.com/questions/50868909
复制相似问题