snippetgoCritical
How to import local packages in go?
Viewed 0 times
howlocalpackagesimport
Problem
I am new to go and working on an example code that I want to localize.
In the original
Now I have
So I converted the import statement to:
But when I run
Also, when I use
How can I fix this?
In the original
main.go import statement it was:import (
"log"
"net/http"
"github.com/foo/bar/myapp/common"
"github.com/foo/bar/myapp/routers"
)Now I have
common and routers package in /home/me/go/src/myappSo I converted the import statement to:
import (
"log"
"net/http"
"./common"
"./routers"
)But when I run
go install myapp I get these errors:can't load package: /home/me/go/src/myapp/main.go:7:3: local import "./common" in non-local packageAlso, when I use
common and routers instead of ./common and ./routers in the import statement, I get:myapp/main.go:7:3: cannot find package "common" in any of:
/usr/local/go/src/common (from $GOROOT)
/home/me/go/src/common (from $GOPATH)
myapp/main.go:8:2: cannot find package "routers" in any of:
/usr/local/go/src/routers (from $GOROOT)
/home/me/go/src/routers (from $GOPATH)How can I fix this?
Solution
Well, I figured out the problem.
Basically Go starting path for import is
So I just needed to add
Basically Go starting path for import is
$HOME/go/srcSo I just needed to add
myapp in front of the package names, that is, the import should be:import (
"log"
"net/http"
"myapp/common"
"myapp/routers"
)Code Snippets
import (
"log"
"net/http"
"myapp/common"
"myapp/routers"
)Context
Stack Overflow Q#35480623, score: 159
Revisions (0)
No revisions yet.