1

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 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

My Makefile had the order of grid.go and node.go reversed (I did the change). I've done a 'gomake clean' and 'gomake' the grid package, I've tried to compile my main package but it gives me the same result: 'main.go:15: undefined: grid.InitGrid'.
For the grid package, gomake clean then gomake install.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.