Go 未定义的导入函数

huangapple go评论93阅读模式
英文:

Go undefined imported function

问题

我在使用一个函数时遇到了问题,但实际上不应该有任何问题。
在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

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    网格结构
____________________________________________________________________________
*/
type Grid struct {
    // 网格的划分数
    number_lines uint
    number_columns uint

    // 网格的尺寸
    width uint
    height uint

    // 节点数组
    nodes []Node
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    初始化网格
____________________________________________________________________________
*/
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"
)

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    应用程序入口点
____________________________________________________________________________
*/
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包时,一切都正常,但是当我尝试编译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文档,但我找不到它不起作用的原因。

谢谢你的帮助。

英文:

I have a problem using a function when there should not be any problem.
In Go, a Function that starts with a capital letter has a visibility outside the package.


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

When I compile the grid package, everything goes well, but when I try to compile le main package, it gives me that error message:

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

I don't understand why it gives me that error, I've passed some time reading the Go documentation, but I don't find the reason why it doesn't work.

Thank you for your help.

答案1

得分: 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
英文:

You compiled and installed the grid package with just the node.go source file. Compile and install the grid package with the node.go and grid.go source files. For example,

include $(GOROOT)/src/Make.inc

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

include $(GOROOT)/src/Make.pkg

huangapple
  • 本文由 发表于 2011年8月31日 12:29:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/7252557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定