1

Usually i import local packages by

import "github.com/crsov/myproj/mypkg"

but when i am editing them i need to go get -u "github.com/crsov/myproj/mypkg" every save.

That's the best way to import local package? I have found many answers to this question but most of them are for older golang versions.

fonini
  • 3,243
  • 5
  • 30
  • 53
  • 4
    `replace` directives in your `go.mod` file could be useful here, see https://golang.org/ref/mod – Eli Bendersky Mar 10 '21 at 16:28
  • 1
    [Can I work entirely outside of VCS on my local filesystem?](https://github.com/golang/go/wiki/Modules#can-i-work-entirely-outside-of-vcs-on-my-local-filesystem) – JimB Mar 10 '21 at 16:28
  • And here I was thinking we'd all switched over to `go mod` ages ago... :P Seriously, though: `go mod init `, just import whatever packages you need (either "github.com/foo/bar"`, or whatever their module name is (in go.mod file), and run `go mod download`, it'll fetch all deps – Elias Van Ootegem Mar 10 '21 at 16:31
  • There is no "best" way to import a package, there is just _one_ _way_: `import "proper-import-path-of-package"` with proper import path of the form /relative/filesystem/path. If from a different module: use `replace in your go.mod` but the import is always the same. – Volker Mar 10 '21 at 16:49

1 Answers1

2

If you have a local package that is very new and has not stabilized, you can do this instead:

go mod init mypkg

and import like this:

import "mypkg"

then you don't have to worry about pulling from the internet, it will just pull from the local directory. Then once your package stabilizes, you can rename so that it can be properly published for others to use.

https://golang.org/doc/code.html

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Zombo
  • 1
  • 62
  • 391
  • 407