无法将数据(类型interface {})转换为字符串:need type assertion [英] cannot convert data (type interface {}) to type string: need type assertion
问题描述
我很新,我正在玩这个通知 起初,我的代码如下所示: 我想将换行符追加到 经过一些Google搜索后,我发现这个问题。 这是我应该做的吗?我的编译器错误消失了,所以我猜这很好?这是否有效?你应该采用不同的方式吗? 根据 A type assertion允许你声明一个包含某个具体类型的接口值,或者它的具体类型满足另一个接口。在你的例子中,你声明了数据(type interface {})具有具体的类型字符串。如果你错了,程序会在运行时恐慌。你不需要担心效率,检查只需要比较两个指针值。 如果你不确定它是否是一个字符串,你可以使用这两个返回语法。 如果数据不是字符串,ok将为false。然后将这样一条语句包装到if语句中是很常见的,如下所示: I am pretty new to go and I was playing with this notify package. At first I had code that looked like this: I wanted to append newline to After After a little bit of Googling I found this question on SO. Then I updated my code to: 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. 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:
这篇关于无法将数据(类型interface {})转换为字符串:need type assertion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
func doit(w http.ResponseWriter,r * http.Request){
notify.Post(my_event,Hello World!)
fmt.Fprint(w,+ OK )
}
Hello World !
,但不在上面的函数 doit
中,因为这样做很简单,但是在处理函数中$ c
$ 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(不匹配的类型接口{}和字符串)
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)
}
str,ok:= data。(string)
pre>
if str,ok:= data。(string );其他{
$ p {
/ *不是字符串* /
}
/ *作用于str * /
} $ p> func doit(w http.ResponseWriter, r *http.Request) {
notify.Post("my_event", "Hello World!")
fmt.Fprint(w, "+OK")
}
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")
}
go run
:$ go run lp.go
# command-line-arguments
./lp.go:15: invalid operation: data + "\n" (mismatched types interface {} and string)
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)
}
str, ok := data.(string)
if str, ok := data.(string); ok {
/* act on str */
} else {
/* not string */
}