Importing local libraries in GO

0

I'm having trouble importing a go library. The library is in a folder in the same project directory and always the import error, when not the import error, of the error to access the function.

main.go

packagemainimport("fmt"
    "./test"
)

func main(){
    test.makeCalc(1,1)
    fmt.Println("test.makeCalc(3,6)")
}

test.go

package test

func makeCalc(x int, y int) int{
    return x+y
}
    
asked by anonymous 10.05.2018 / 21:15

1 answer

0

First create the following folder structure:

/path/to/app
  |-main.go
  |-src
     |-test
        |-test.go

Make sure /path/to/app is included in GOPATH. If not, include it:

$ # em linux
$ cd /path/to/app
$ export GOPATH=$GOPATH:/path/to/app

The code that is in test.go, to be used in main.go, needs to be exported, then use:

package test

func MakeCalc(x int, y int) int{
    return x+y
}

I hope I have helped!

    
12.05.2018 / 02:49