doubianxian6557 2014-05-16 03:36
浏览 372
已采纳

goLang。 调用“ .Front()”以获取字符串列表。 但是返回错误,提示字符串列表没有Front方法

I have code like this

t := strings.FieldsFunc(value, extract_word)
        fmt.Println("t:", len(t),t)
        m := make(map[string]int)
    for word := t.Front(); word != nil; word=word.Next(){
            m[word]++   
    }

and it gets this error

t.Front undefined (type []string has no field or method Front)

I know list has Front() method. http://golang.org/pkg/container/list/ but why it complains here? so confused, need help. thank you!

  • 写回答

2条回答 默认 最新

  • duanjiao8007 2014-05-16 03:41
    关注

    []T is not a "list" as it's referred to in other languages (e.g. Python). In Go, it's referred to as the "Slice" http://golang.org/ref/spec#Slice_types

    Its elements range from 0 to len(slice)-1, and are accessed with C-like array access notation. The "front" of the slice is generally considered slice[0], though you may consider a different index the front if you're using a slice to implement something like, e.g., a stack.

    No built-in type in Go has any methods defined on it, but do have built-in functions that take them as arguments, such as len.

    The package you linked to implements the List type. As the documentation at the top says "Package list implements a doubly linked list." This doubly linked list, which you can create by calling list.New(), has a Front method, among the others listed in the package documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?