2016-12-07 101 views
0

运行以下Go代码时出现以下编译器错误。Go的struct方法在调用中抛出太多参数

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:调用的参数太多i.Sort

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

请帮我解决这个问题。

+0

您是否尝试过重命名的方法来解决冲突? – fafl

+0

@fafl,是的,我试过了。我在这两个软件包(sort&main)中替换了函数Sort和InSort。它返回以下错误。 ** i.InSort未定义(类型sort.InsertionSort没有字段或方法InSort)** –

+0

您发布的代码的作品。请张贴您的完整代码或这个问题,因为它是无关紧要的。 – icza

回答

0

我认为你的目录顺序不正确,或许你还有一些其他的怪事。

注:我运行Ubuntu 16.04.1 64bit wihtin go version go1.6.2 linux/amd64

所以,这里是我用来运行包的步骤。

检查您GOPATH或导出这样的例子

$ export GOPATH=$HOME/Bureau/stack 

2-

检查一个新的,如果你的$GOPATH指向一个目录:

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

3-

克隆你的repository$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 

在这里,我们需要知道运行代码是:

  • 路径分类文件夹中的哪一个:Go/testgo/sort
  • main.go置于$GOPATH/src

主。去:

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] 
+0

它工作正常。谢谢您的帮助。你能解释我的文件结构有什么问题吗? –

+2

@GokulnathK:每个目录只能创建一个包装器,始终保持包装在GOPATH中,并且永远不要使用相对导入。仔细阅读[如何编写代码](https://golang.org/doc/code.html) – JimB

+0

好的答案@JimB。谢谢。 –

相关问题