【问题标题】:Go undefined imported function去未定义的导入函数
【发布时间】:2011-11-07 07:54:09
【问题描述】:

我在使用不应该有任何问题的函数时遇到了问题。 在 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/grid/Makefile

include $(GOROOT)/src/Make.inc

TARG=grid

GOFILES=\
    node.go\
    grid.go\

include $(GOROOT)/src/Make.pkg

src/main/Makefile

include $(GOROOT)/src/Make.inc

TARG=main

GOFILES=\
    main.go\

include $(GOROOT)/src/Make.cmd

当我编译 grid 包时,一切顺利,但是当我尝试编译 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 文档,但我找不到它不起作用的原因。

感谢您的帮助。

【问题讨论】:

    标签: function package go undefined


    【解决方案1】:

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

    include $(GOROOT)/src/Make.inc
    
    TARG=grid
    GOFILES=\
        grid.go\
        node.go\
    
    include $(GOROOT)/src/Make.pkg
    

    【讨论】:

    • 我的 Makefile 的 grid.go 和 node.go 的顺序颠倒了(我做了更改)。我已经完成了 'gomake clean' 和 'gomake' 网格包,我尝试编译我的主包,但它给了我相同的结果:'main.go:15: undefined: grid.InitGrid'。跨度>
    • 对于grid 包,gomake clean 然后是gomake install
    猜你喜欢
    • 2020-05-12
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 2020-05-29
    • 2019-08-11
    • 2021-10-24
    • 2020-05-31
    相关资源
    最近更新 更多