golang 1.12导入相对目录模块

go 1.12 how to import a relative path module. e.g

demo_project
   - main.go
   -- demo
      - demo.go


demo.go:
pakcage demo
...

I have try to import "./demo" in main.go,but it's report a error message: cannot find module for path _/home/xxx/mywork/go_project/grpc_demo/demo how to import demo in main.go

  • GO wants to import the package from $GOPATH env at first. If go can't find the package, an error is raised.

  • Add our project directory into $GOPATH env.


debug@ulab:/tmp/test$ go run main.go
main.go:3:8: cannot find package "mylib" in any of:
    /home/debug/.golang/go/src/mylib (from $GOROOT)
    /home/debug/.golang/gopath/src/mylib (from $GOPATH)

$ tree
.
├── main.go
├── src
│   └── hello
│       └── hello.go
└── start.sh

2 directories, 3 files

$ cat start.sh
#!/bin/sh

GOPATH="$(pwd):$(go env GOPATH)"

go run main.go

$ ./start.sh
Welcome to golang