HiveBrain v1.2.0
Get Started
← Back to all entries
snippetgoCritical

How to import local packages in go?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howlocalpackagesimport

Problem

I am new to go and working on an example code that I want to localize.

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/myapp

So 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 package


Also, 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 $HOME/go/src

So 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.