2016-12-07 33 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。謝謝。 –

相關問題