英文:
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 {"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;
}
and the main package which has the calling function
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);
}
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
│   ├── 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
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 (
"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);
}
Finally
Check you're in $GOPATH/src
and run:
$ go run main.go
output:
[1 2 3 4 5 7 8 9 12]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论