英文:
Printing a list from within a list in Go. Loop trouble
问题
尝试从网络命名空间中打印路由列表。netlink.RouteList
函数需要一个 Interface
类型。通过 LinkList()
收集所有接口的列表。
我试图使用每个接口调用 RouteList 并打印其输出。RouteList 返回类型为 Route
,我试图打印 int
类型的 LinkIndex。
看起来我的循环
for j := range rt {
log.Printf("Route: %d : %d",rt[j].LinkIndex)
}
由于某种原因没有执行,如果在其中运行另一个 Printf 测试,将不会输出任何内容。
为什么这个循环不会被调用?
func (h *NSHandle) showInts() {
nh := (*netlink.Handle)(h) //cast required
int, err := nh.LinkList()
if err != nil {
log.Fatal(err)
}
log.Printf("Namespace Ints:")
for i, r := range int {
log.Printf("%d: %s", i, r.Attrs().Name)
rt, err := netlink.RouteList(r,-1)
if err != nil {
log.Fatal(err)
}
for j := range rt {
log.Printf("Route: %d : %d",rt[j].LinkIndex)
}
}
}
英文:
Trying to print a list of routes from within a network namespace. The netlink.RouteList
function requires an Interface
type. A list of all interfaces is gathered by LinkList()
.
I'm trying to call RouteList with every interface and print it's output. RouteList returns type Route
where I'm trying to print the int
LinkIndex.
It appears as if my loop
for j := range rt {
log.Printf("Route: %d : %d",rt[j].LinkIndex)
}
Isn't executing for some reason, running another Printf test in there yields nothing.
Why wouldn't this loop be called?
func (h *NSHandle) showInts() {
nh := (*netlink.Handle)(h) //cast required
int, err := nh.LinkList()
if err != nil {
log.Fatal(err)
}
log.Printf("Namespace Ints:")
for i, r := range int {
log.Printf("%d: %s", i, r.Attrs().Name)
rt, err := netlink.RouteList(r,-1)
if err != nil {
log.Fatal(err)
}
for j := range rt {
log.Printf("Route: %d : %d",rt[j].LinkIndex)
}
}
}
答案1
得分: 1
这是一个糟糕的问题。发布后不久,我意识到数组显然是空的,因为在调用RouteList时没有使用接收器Handler
。简单地修复了这个问题:
for i, r := range rl {
log.Printf("%d: %s", i, LinkIndex)
}
英文:
This was a bad question. Soon after posting I had realised that the array was obviously empty due to the fact that RouteList was being called without the receiver Handler
. This was fixed by simply:
for i, r := range rl {
log.Printf("%d: %s", i, LinkIndex)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论