无法将数据(类型interface {})转换为字符串:need type assertion [英] cannot convert data (type interface {}) to type string: need type assertion

查看:2385
本文介绍了无法将数据(类型interface {})转换为字符串:need type assertion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新,我正在玩这个通知

起初,我的代码如下所示:

  func doit(w http.ResponseWriter,r * http.Request){
notify.Post(my_event,Hello World!)
fmt.Fprint(w,+ OK )
}

我想将换行符追加到 Hello World !,但不在上面的函数 doit 中,因为这样做很简单,但是在处理函数中
$ p $ func处理程序(w http.ResponseWriter,r * http.Request){
myEventChan:= make(chan interface {})
notify.Start(my_event,myEventChan)
data:=< -myEventChan
fmt.Fprint(w,data +\\ \\ n)
}

go run

  $ go运行lp.go 
#command-lin e-arguments
./lp.go:15:无效操作:data +\ n(不匹配的类型接口{}和字符串)

经过一些Google搜索后,我发现这个问题

然后我更新了我的代码:

  func handler(w http.ResponseWriter,r * http.Request){
myEventChan:= make(chan interface {})
notify.Start(my_event,myEventChan)
data:=< -myEventChan
s:= data。(string)+\\\

fmt.Fprint(w,s)
}

这是我应该做的吗?我的编译器错误消失了,所以我猜这很好?这是否有效?你应该采用不同的方式吗? 根据

A type assertion允许你声明一个包含某个具体类型的接口值,或者它的具体类型满足另一个接口。在你的例子中,你声明了数据(type interface {})具有具体的类型字符串。如果你错了,程序会在运行时恐慌。你不需要担心效率,检查只需要比较两个指针值。



如果你不确定它是否是一个字符串,你可以使用这两个返回语法。

  str,ok:= data。(string)
pre>

如果数据不是字符串,ok将为false。然后将这样一条语句包装到if语句中是很常见的,如下所示:

  if str,ok:= data。(string );其他{
/ *不是字符串* /
}
/ *作用于str * /
} $ p>

I am pretty new to go and I was playing with this notify package.

At first I had code that looked like this:

func doit(w http.ResponseWriter, r *http.Request) {
    notify.Post("my_event", "Hello World!")
    fmt.Fprint(w, "+OK")
}

I wanted to append newline to Hello World! but not in the function doit above, because that would be pretty trivial, but in the handler afterwards like this below:

func handler(w http.ResponseWriter, r *http.Request) {
    myEventChan := make(chan interface{})
    notify.Start("my_event", myEventChan)
    data := <-myEventChan
    fmt.Fprint(w, data + "\n")
}

After go run:

$ go run lp.go 
# command-line-arguments
./lp.go:15: invalid operation: data + "\n" (mismatched types interface {} and string)

After a little bit of Googling I found this question on SO.

Then I updated my code to:

func handler(w http.ResponseWriter, r *http.Request) {
    myEventChan := make(chan interface{})
    notify.Start("my_event", myEventChan)
    data := <-myEventChan
    s:= data.(string) + "\n"
    fmt.Fprint(w, s)
}

Is this what I was supposed to do? My compiler errors are gone so I guess that's pretty good? Is this efficient? Should you do it differently?

解决方案

According to the Go specification:

For an expression x of interface type and a type T, the primary expression x.(T) asserts that x is not nil and that the value stored in x is of type T.

A "type assertion" allows you to declare an interface value contains a certain concrete type or that its concrete type satisfies another interface.

In your example, you were asserting data (type interface{}) has the concrete type string. If you are wrong, the program will panic at runtime. You do not need to worry about efficiency, checking just requires comparing two pointer values.

If you were unsure if it was a string or not, you could test using the two return syntax.

str, ok := data.(string)

If data is not a string, ok will be false. It is then common to wrap such a statement into an if statement like so:

if str, ok := data.(string); ok {
    /* act on str */
} else {
    /* not string */
}

这篇关于无法将数据(类型interface {})转换为字符串:need type assertion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆