I am using go version go1.11.2 windows/amd64 in windows 10
i have folder in desktop with name "GoPro" which has one file - main.go one folder - Modules
The "Modules" folder contains one file - Modules.go
Folder Structure
Desktop
|------->GoPro
|----->main.go
|----->Models
|---->Models.go
main.go
// desktop/GoPro/main.go
package main
import (
"./Models"
)
func main() {
Models.Printhello()
}
Models.go
// desktop/GoPro/Models
package Models
import "fmt"
func Printhello() {
fmt.Println("Hello")
}
If try to run the main.go , I am getting the below error , Go recongise the package but it saying undefined.
go run main.go
command-line-arguments
.\main.go:8:2: undefined: Models
The folder is not in GOPATH. I am just trying to import the package with in sub folder of main.go
You can either set up $GOPATH
and place your library in a path relative to $GOPATH
, or use modules.
For modules, you can do something like:
$ tree
.
├── go.mod
├── main.go
└── mylib
└── mylib.go
Contents of files:
$ cat go.mod
module myproject.com
====
$ cat main.go
package main
import "myproject.com/mylib"
func main() {
mylib.Say()
}
====
$ cat mylib/mylib.go
package mylib
import "fmt"
func Say() {
fmt.Println("Hello from mylib")
}
P.S. use lowercase to name packages/modules