英文:
foo.Name undefined (type interface {} has no field or method Name)
问题
我使用原生的golang包"container/list"来管理一个堆栈中的inotify事件。当我访问堆栈的项时,出现了一个类型错误(我认为是这个原因)。
import (
"golang.org/x/exp/inotify"
"container/list"
"log"
"fmt"
)
func main() {
stack := list.New()
watcher, err := inotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.Watch(os.Args[1])
if err != nil {
log.Fatal(err)
}
for {
select {
case ev := <-watcher.Event:
stack.PushFront(ev)
fmt.Printf("%#v\n", ev)
}
foo := stack.Front().Value
fmt.Printf("%#v\n", foo)
log.Println("Name: ", foo.Name)
}
}
当我输出__ev__变量时,对象的类型是__&inotify.Event__。
当我弹出一个项并输出变量时,我的对象类型是__&inotify.Event__。
根据错误信息,我认为这是一个接口接受的类型对象的问题,但我找不到如何定义类型的方法。
英文:
I use the native golang package "container/list" to manage inotify event in a stack. When I access at stack's item, I have a fail with the type (I think).
import (
"golang.org/x/exp/inotify"
"container/list"
"log"
"fmt"
)
func main() {
stack := list.New()
watcher, err := inotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.Watch(os.Args[1])
if err != nil {
log.Fatal(err)
}
for {
select {
case ev := <-watcher.Event:
stack.PushFront(ev)
fmt.Printf("%#v\n", ev)
}
foo := stack.Front().Value
fmt.Printf("%#v\n", foo)
log.Println("Name: ", foo.Name)
}
}
When I dump ev variable, the object type is &inotify.Event.
When I pop one item and I dump the variable, my object type is &inotify.Event.
With the error message I think it's a problem with type object accept by interface but I don't find how define type.
答案1
得分: 9
你需要对foo
进行类型断言,将其断言为*inotify.Event
类型,堆栈不知道它是什么,它只保存interface{}
对象。
你需要做的是像这样:
elem := stack.Front().Value
if foo, ok := elem.(*inotify.Event); ok {
fmt.Printf("%#v\n", foo)
log.Println("Name: ", foo.Name)
}
ok
部分确保如果它不是一个事件,你的程序不会崩溃。当然,你需要处理它不是事件的情况。
有关接口转换和类型断言的更多信息,请参阅:https://golang.org/doc/effective_go.html#interface_conversions
英文:
You need to do type-assertion for foo
into *inotify.Event
, the stack doesn't know what it is, it just holds interface{}
objects.
What you need to do is something like:
elem := stack.Front().Value
if foo, ok := elem.(*inotify.Event); ok {
fmt.Printf("%#v\n", foo)
log.Println("Name: ", foo.Name)
}
The ok
bit makes sure if it's not an event, your program won't crash. Of course you need to handle the case that it isn't.
More info on interface conversions and type assertions: https://golang.org/doc/effective_go.html#interface_conversions
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论