len方法未定义

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

len method undefined

问题

我一直在阅读[golang-book]: http://www.golang-book.com,并且在阅读过程中完成了练习。在第6章中,有一个练习必须找到未排序列表[x]中的最小元素。

我有以下代码,但不知道为什么方法length (len) 在第14行给我一个错误:x.len未定义(类型[]int没有字段或方法len)

package main

import "fmt"

func main() {
    x := []int{
        48, 96, 86, 68,
        57, 82, 63, 70,
        37, 34, 83, 27,
        19, 97, 9, 17,
    }

    small := x[0]
    for i := 1; i < x.len(); i++ {
        if x[i] < small {
            fmt.Println(x[i])
        }
    }
}

我使用的逻辑是通过谷歌搜索得到的,所以可能数组上没有len方法?非常感谢任何帮助。

英文:

I've been reading the [golang-book]: http://www.golang-book.com and completing the excercises as I go along. In Chapter 6, there is an exercise that must find the smallest element in an unsorted list[x].

I have the following code but somehow I don't know why the method length (len) gives me an error on line 14: x.len undefined (type []int has no field or method len)

package main

import &quot;fmt&quot;

func main() {
	x := []int{
		48, 96, 86, 68,
		57, 82, 63, 70,
		37, 34, 83, 27,
		19, 97, 9, 17,
	}

	small := x[0]
	for i := 1; i &lt; x.len(); i++ {
		if x[i] &lt; small {
			fmt.Println(x[i])
		}
	}
}

The logic I used was Googled so perhaps there is no len method on arrays? Any help is greatly appreciated.

答案1

得分: 8

数组和切片没有len()方法。len()函数是语言内置的。

所以你的代码

for i := 1; i &lt; x.len(); i++ {

应该改为

for i := 1; i &lt; len(x); i++ {

这是一个在<a href="http://play.golang.org/p/hVetB2_acU">playground</a>上工作的版本。

package main

import &quot;fmt&quot;

func main(){
    x := []int{
        48,96,86,68,
        57,82,63,70,
        37,34,83,27,
        19,97, 9,17,
    }   

    small := x[0]
    for i := 1; i &lt; len(x); i++ {
        if x[i] &lt; small { 
        fmt.Println(x[i]);
        }
    }
}
英文:

Arrays and slices do not have a len() method. The len() function is a language built-in.

So your code

for i := 1; i &lt; x.len(); i++ {

Should be

for i := 1; i &lt; len(x); i++ {

Here is a working version in <a href="http://play.golang.org/p/hVetB2_acU">the playground</a>.

package main

import &quot;fmt&quot;

func main(){
    x := []int{
        48,96,86,68,
        57,82,63,70,
        37,34,83,27,
        19,97, 9,17,
    }   

    small := x[0]
    for i := 1; i &lt; len(x); i++ {
        if x[i] &lt; small { 
        fmt.Println(x[i]);
        }
    }
}

答案2

得分: 4

len()不是切片的方法,它是一个全局函数。你想要说的是len(x)

package main

import "fmt"

func main() {
    x := []int{
        48, 96, 86, 68,
        57, 82, 63, 70,
        37, 34, 83, 27,
        19, 97, 9, 17,
    }

    small := x[0]
    for i := 1; i < len(x); i++ {
        if x[i] < small {
            fmt.Println(x[i])
        }
    }
}
英文:

len() isn't a method of a slice. It's a global function. You want to say len(x).

package main

import &quot;fmt&quot;

func main() {
	x := []int{
		48, 96, 86, 68,
		57, 82, 63, 70,
		37, 34, 83, 27,
		19, 97, 9, 17,
	}

	small := x[0]
	for i := 1; i &lt; len(x); i++ {
		if x[i] &lt; small {
			fmt.Println(x[i])
		}
	}
}

答案3

得分: 1

实际上,数组和切片类型可以有一个len()方法,就像这样:

http://play.golang.org/p/CkU4LJZgUb

package main

import "fmt"

type SliceType []int

func (s SliceType) len() int {
    return len(s)
}

func main() {
    s := SliceType{1, 2, 3, 4, 5, 6, 7, 8, 9}
    for i := 0; i < s.len(); i++ {
        fmt.Println(s[i])
    }
}

你找到的代码可能是container/listcontainer/ring的实现,它们都指定了一个Len()方法。

然而,原始切片和数组没有任何附加的方法,你必须使用内置函数len()

英文:

In fact, array- and slice-types can have a len() method, like this:

http://play.golang.org/p/CkU4LJZgUb

package main

import &quot;fmt&quot;

type SliceType []int

func (s SliceType) len() int {
	return len(s)
}

func main() {
	s := SliceType{1, 2, 3, 4, 5, 6, 7, 8, 9}
	for i := 0; i &lt; s.len(); i++ {
		fmt.Println(s[i])
	}
}

The code you found is probably an implementation of container/list or container/ring, both of which specify a Len() method.

Primitive slices and arrays, though, don't have any methods attached to them and you must use the built-in function len().

huangapple
  • 本文由 发表于 2013年2月28日 05:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/15122361.html
匿名

发表评论

匿名网友

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

确定