去未定义的导入功能

时间:2011-08-31 04:29:02

标签: function package go undefined

在没有任何问题的情况下使用函数时出现问题。 在Go中,以大写字母开头的函数在包外具有可见性。


node.go

package grid  

type Node struct {  
    id uint  
    name string  
    pos_i uint  
    pos_j uint  
    node_type string  
}

grid.go

package grid

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    the Grid Structure
____________________________________________________________________________
*/
type Grid struct {
    // The numbers of divisions in the Grid
    number_lines uint
    number_columns uint 

    // The Sizes of the Grid
    width uint
    height uint

    // An Array of the Nodes
    nodes []Node
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Initialize the Grid
____________________________________________________________________________
*/
func InitGrid() *Grid {
    g := new(Grid)

    g.number_lines = 4
    g.number_columns = 4

    g.width = 400
    g.height = 400

    return g
}

main.go

package main

import (
    "fmt"
    "grid"
)

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Entry Point of the Application
____________________________________________________________________________
*/
func main() {
    grid_ := grid.InitGrid()
    fmt.Println(grid_)    
}

的src /网格/生成文件

include $(GOROOT)/src/Make.inc

TARG=grid

GOFILES=\
    node.go\
    grid.go\

include $(GOROOT)/src/Make.pkg

的src /主/生成文件

include $(GOROOT)/src/Make.inc

TARG=main

GOFILES=\
    main.go\

include $(GOROOT)/src/Make.cmd

当我编译网格包时,一切顺利,但是当我尝试编译le main包时,它给了我错误信息:

manbear@manbearpig:~/Bureau/go_code/main$ gomake  
6g  -o _go_.6 main.go  
main.go:15: undefined: grid.InitGrid  
make: *** [_go_.6] Erreur 1  

我不明白为什么它给了我这个错误,我已经花了一些时间阅读Go文档,但是我找不到它不起作用的原因。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您只使用grid源文件编译并安装了node.go包。使用gridnode.go源文件编译并安装grid.go包。例如,

include $(GOROOT)/src/Make.inc

TARG=grid
GOFILES=\
    grid.go\
    node.go\

include $(GOROOT)/src/Make.pkg