1. 首页
  2. 主题
  3. Go问与答

类型转换出错了 need type assertion

FengGeSe · · 3059 次点击
```go package main import ( "fmt" "container/ring" ) func main () { r := ring.New(1) r.Value = "A1" s := string(r.Value) fmt.Printf("%T", s[0]) } ``` 不知道怎么出现这个错误: # command-line-arguments main/main.go:14: cannot convert r.Value (type interface {}) to type string: need type assertion fmt.Printf("%T", r.Value) 结果是 string 啊?!
interface的类型转换都是,intefaceXx.(typeYy)
#2
更多评论
FengGeSe
好好学习,天天向上
问题解决了== s := r.Value.(string) // 用这行代码就行了,还没具体看为什么. 接口类型向普通类型的转换称为类型断言(运行期确定)
#1

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中