Go的结构体方法在调用时抛出了太多的参数。

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

Go's method on struct throws too many argument in call

问题

在运行以下Go代码时,我遇到了以下编译器错误:

package sort

type InsertionSort struct {
    Unsorted []int;
}

func (is InsertionSort) Sort(mode string) []int {
    length := len(is.Unsorted);

    funcs := map[string] func(int, int) bool {"method":is.greaterThan};
    if mode == "desc" {
        funcs = map[string] func(int, int) bool {"method":is.lesserThan};
    }

    for i := 0; i < length; i++ {
        temp := is.Unsorted[i];
        j := i - 1;
        for ; j >=0 && funcs["method"](is.Unsorted[j], temp); j-- {
            is.Unsorted[j + 1] = is.Unsorted[j];
        }
        is.Unsorted[j + 1] = temp;
    }

    return is.Unsorted;
}

func (is InsertionSort) greaterThan (a int, b int) bool {
    return a > b;
}

func (is InsertionSort) lesserThan (a int, b int) bool {
    return a < b;
}

以及包含调用函数的主要包:

package main

import (
  "learning/Go/testgo/sort"
  "fmt"
)

func main() {
    unsort := []int {5, 7, 3, 2 ,4 ,8, 9, 12, 1};
    i := sort.InsertionSort {unsort};

    mode := "asc";
    sorted := i.Sort(mode);
    fmt.Println(sorted);
}

编译器返回的错误信息是:

.\sort.go:16: too many arguments in call to i.Sort

注意:

  • sort已经有另一个名为BubbleSort的结构体,它具有相同的Sort方法但没有参数。我不知道它是否与当前结构体冲突。

请帮我解决这个问题。

英文:

I am getting the following compiler error while running the following Go Code.

package sort

type InsertionSort struct {
    Unsorted []int;
}

func (is InsertionSort) Sort(mode string) []int {
    length := len(is.Unsorted);

    funcs := map[string] func(int, int) bool {&quot;method&quot;:is.greaterThan};
    if mode == &quot;desc&quot; {
        funcs = map[string] func(int, int) bool {&quot;method&quot;:is.lesserThan};
    }

    for i := 0; i &lt; length; i++ {
        temp := is.Unsorted[i];
        j := i - 1;
        for ; j &gt;=0 &amp;&amp; funcs[&quot;method&quot;](is.Unsorted[j], temp); j-- {
            is.Unsorted[j + 1] = is.Unsorted[j];
        }
        is.Unsorted[j + 1] = temp;
    }

    return is.Unsorted;
}

func (is InsertionSort) greaterThan (a int, b int) bool {
    return a &gt; b;
}

func (is InsertionSort) lesserThan (a int, b int) bool {
    return a &lt; b;
}

and the main package which has the calling function

package main

import (
  &quot;learning/Go/testgo/sort&quot;
  &quot;fmt&quot;
)

func main() {
    unsort := []int {5, 7, 3, 2 ,4 ,8, 9, 12, 1};
    i := sort.InsertionSort {unsort};

    mode := &quot;asc&quot;;
    sorted := i.Sort(mode);
    fmt.Println(sorted);
}

Error message returned by the compiler is

> .\sort.go:16: too many arguments in call to i.Sort

Note :-
Package sort already has another struct called BubbleSort, which has the same Sort method with no arguments. I don't know whether it is conflicting the current struct.

Please help me on this.

答案1

得分: 0

我认为你的目录顺序可能不正确,或者可能有其他一些奇怪的地方。

**注意:**我正在运行Ubuntu 16.04.1 64位,使用go version go1.6.2 linux/amd64

以下是我用来运行你的包的步骤。

1-

检查你的GOPATH,或者像下面这个例子一样导出一个新的:

$ export GOPATH=$HOME/Bureau/stack

2-

检查你的$GOPATH是否指向一个目录:

$ $GOPATH
bash: /home/nexus/Bureau/stack/ : 是一个目录

3-

$GOPATH/src文件夹中克隆你的仓库

$ cd $GOPATH/src
$ git clone https://github.com/gokulnathk/Go

现在,文件夹的层次结构如下:

$ tree
.
├── Go
│   ├── atm-dispening
│   ├── download
│   │   └── download.go
│   ├── downloadParallel
│   │   └── downloadParallel.go
│   ├── hello
│   │   └── hello.go
│   ├── portserver
│   │   └── simpleport.go
│   ├── safebrowsing
│   ├── testgo
│   │   ├── functionParam.go
│   │   ├── functions.go
│   │   ├── greeting
│   │   │   └── greeting.go
│   │   ├── routine.go
│   │   ├── sort
│   │   │   ├── bubblesort.go
│   │   │   └── insertionsort.go
│   │   ├── sort.go
│   │   ├── sorttest.go
│   │   ├── struct.go
│   │   └── test.go
│   └── webserver
│       ├── modcloth.txt
│       └── simple.go
└── main.go

11 directories, 17 files

在这里,我们需要知道如何运行你的代码:

  • sort文件夹的路径是:Go/testgo/sort
  • main.go放在$GOPATH/src

main.go:

package main

import (
  "Go/testgo/sort"
  "fmt"
)

func main() {
    unsort := []int {5, 7, 3, 2 ,4 ,8, 9, 12, 1};
    i := sort.InsertionSort {unsort};

    mode := "asc";
    sorted:= i.Sort(mode);
    fmt.Println(sorted);
}

最后

确保你在$GOPATH/src中,并运行:

$ go run main.go

输出:

[1 2 3 4 5 7 8 9 12]
英文:

I think that the order of your directories isn't correct or maybe you've some other oddities.

Note: I'm running Ubuntu 16.04.1 64bit wihtin go version go1.6.2 linux/amd64

So, here are the steps that i used to run your package.

1-

Check Your GOPATH or export a new one like this example

$ export GOPATH=$HOME/Bureau/stack

2-

Check if your $GOPATH points to a directory :

$ $GOPATH
bash: /home/nexus/Bureau/stack/ : is a directory

3-

Clone your repository in $GOPATH/src folder

$ cd $GOPATH/src
$ git clone https://github.com/gokulnathk/Go

Now, the hierarchy of the folders are:

$ tree
.
├── Go
│&#160;&#160; ├── atm-dispening
│&#160;&#160; ├── download
│&#160;&#160; │&#160;&#160; └── download.go
│&#160;&#160; ├── downloadParallel
│&#160;&#160; │&#160;&#160; └── downloadParallel.go
│&#160;&#160; ├── hello
│&#160;&#160; │&#160;&#160; └── hello.go
│&#160;&#160; ├── portserver
│&#160;&#160; │&#160;&#160; └── simpleport.go
│&#160;&#160; ├── safebrowsing
│&#160;&#160; ├── testgo
│&#160;&#160; │&#160;&#160; ├── functionParam.go
│&#160;&#160; │&#160;&#160; ├── functions.go
│&#160;&#160; │&#160;&#160; ├── greeting
│&#160;&#160; │&#160;&#160; │&#160;&#160; └── greeting.go
│&#160;&#160; │&#160;&#160; ├── routine.go
│&#160;&#160; │&#160;&#160; ├── sort
│&#160;&#160; │&#160;&#160; │&#160;&#160; ├── bubblesort.go
│&#160;&#160; │&#160;&#160; │&#160;&#160; └── insertionsort.go
│&#160;&#160; │&#160;&#160; ├── sort.go
│&#160;&#160; │&#160;&#160; ├── sorttest.go
│&#160;&#160; │&#160;&#160; ├── struct.go
│&#160;&#160; │&#160;&#160; └── test.go
│&#160;&#160; └── webserver
│&#160;&#160;     ├── modcloth.txt
│&#160;&#160;     └── simple.go
└── main.go

11 directories, 17 files

Here, what we need to know to run your code is:

  • Path of sort folder which is : Go/testgo/sort
  • main.go placed in $GOPATH/src

main.go :

package main

import (
  &quot;Go/testgo/sort&quot;
  &quot;fmt&quot;
)

func main() {
    unsort := []int {5, 7, 3, 2 ,4 ,8, 9, 12, 1};
    i := sort.InsertionSort {unsort};

    mode := &quot;asc&quot;;
    sorted:= i.Sort(mode);
    fmt.Println(sorted);
}

Finally

Check you're in $GOPATH/src and run:

$ go run main.go

output:

[1 2 3 4 5 7 8 9 12]

huangapple
  • 本文由 发表于 2016年12月7日 16:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/41012624.html
匿名

发表评论

匿名网友

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

确定