Karlom
Karlom

Reputation: 14834

How to idiomatically change go github import paths?

I have imported an app from github which has many imports, spread in several files like:

import (
    "log"
    "net/http"
    "github.com/johndoe/sleekrest/model"
    "github.com/johndoe/sleekrest/shared/passhash"
    "github.com/johndoe/sleekrest/shared/recaptcha"
    "github.com/johndoe/sleekrest/shared/session"
    "github.com/johndoe/sleekrest/shared/view"
    "github.com/johndoe/csrfbanana"
)

I want to work on the packages on my local path /go/src/myrest, so I'd like to have imports to be like

import (
    "log"
    "net/http"
    "./model"
    "./shared/passhash"
    "./shared/recaptcha"
    "./shared/session"
    "./shared/view"
    "./csrfbanana"
)

I know I can use bash commands like sed, find, etc to replace the import paths, but I'm wondering if there is an idiomatic way to do so in golang?

Upvotes: 2

Views: 2292

Answers (1)

Matthew Rankin
Matthew Rankin

Reputation: 461147

There is not an idiomatic way to do this because relative import paths are not idiomatic Go.

Reasons to not use relative import paths

The following is from Organizing Go code:

An import path is the string with which users import a package. It specifies the directory (relative to $GOROOT/src/pkg or $GOPATH/src) in which the package's source code resides.

Sometimes people set GOPATH to the root of their source repository and put their packages in directories relative to the repository root, such as "src/my/package". On one hand, this keeps the import paths short ("my/package" instead of "github.com/me/project/my/package"), but on the other it breaks go get and forces users to re-set their GOPATH to use the package. Don't do this.

The following is from Command go:

Second, if you are compiling a Go program not in a work space, you can use a relative path in an import statement in that program to refer to nearby code also not in a work space. This makes it easy to experiment with small multipackage programs outside of the usual work spaces, but such programs cannot be installed with go install (there is no work space in which to install them), so they are rebuilt from scratch each time they are built. To avoid ambiguity, Go programs cannot use relative import paths within a work space.

Also, you might look at these StackOverflow answers:

Vendoring

Vendoring became an experimental feature in Go 1.5. As of Go 1.6, vendoring is no longer experimental. For more information on vendoring, see:

Upvotes: 2

Related Questions