In Go how can I iterate over a pointer list?

huangapple go评论79阅读模式
英文:

In Go how can I iterate over a pointer list?

问题

我想遍历这个:

*list.List

我该怎么做?

func Foo(key string, values *list.List) string {
...

//我想遍历并从"values"中获取值
}

这个Foo函数被这样调用:

kvs := make(map[string]*list.List)
res := Foo(k, kvs[k]) //k是字符串

谢谢!

英文:

I want to iterate over this :

*list.List

How can I do it?

func Foo(key string, values *list.List) string {
...

//I want to iterate and get value from "values"
}

this Foo is being called like this:

kvs := make(map[string]*list.List)
res := Foo(k, kvs[k]) //k is string

Thanks!

答案1

得分: 9

请检查示例@ http://golang.org/pkg/container/list/

func Foo(key string, vl *list.List) string {
    for e := l.Front(); e != nil; e = e.Next() {
    	v := e.Value
    }
    return ""
}

//edit : 请查看https://stackoverflow.com/questions/12677934/create-a-golang-map-of-lists?rq=1

英文:

Check the example @ http://golang.org/pkg/container/list/ :

func Foo(key string, vl *list.List) string {
    for e := l.Front(); e != nil; e = e.Next() {
    	v := e.Value
    }
    return ""
}

//edit : check https://stackoverflow.com/questions/12677934/create-a-golang-map-of-lists?rq=1

huangapple
  • 本文由 发表于 2014年5月17日 13:05:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/23707788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定