function - 转到未定义的导入函数

标签 function package go undefined

我在使用函数时遇到了问题,但应该不会有任何问题。 在 Go 中,以大写字母开头的 Function 在包外具有可见性。


节点.go

package grid  

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

网格.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_)    
}

源代码/网格/生成文件

include $(GOROOT)/src/Make.inc

TARG=grid

GOFILES=\
    node.go\
    grid.go\

include $(GOROOT)/src/Make.pkg

源代码/主/生成文件

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 文档,但我没有找到它不起作用的原因。

感谢您的帮助。

最佳答案

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

include $(GOROOT)/src/Make.inc

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

include $(GOROOT)/src/Make.pkg

关于function - 转到未定义的导入函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7252557/

相关文章:

javascript - 在检测个位数整数时转换毫秒

python - Python 就地运算符函数与标准运算符函数有何不同?

javascript - Atom PHP/JavaScript 引用包

http - 你如何在 Golang 中使用摘要身份验证进行 HTTP POST?

python - 将整数表示为分数形式的倒数的函数

python - 如何让 Python 搜索文本文件并打印每个匹配的行?

java - Java 中的多个类和主要方法,以及包、 namespace

package - Composer 更新根包

recursion - 如何消除这种类型的递归?

go - 在 go 中枚举字母表的惯用方法是什么?