HexaGridBrain
HexaGridBrain

Reputation: 522

Go undefined imported function

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.

Upvotes: 1

Views: 3136

Answers (1)

peterSO
peterSO

Reputation: 166549

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

Upvotes: 1

Related Questions