-1

I have just started learning GO development, read a lot of articles and documentation, however haven't found a concrete answer.

There are some questions

  1. How to use local packages? Using ./../ doesn't work, I can put all my source code under vendror/src directory, but it makes my project structure to look not like I want
  2. Should I always upload somewhere in remote repository my packages? Is it possible to keep them local. Event if I develop a microservice, a microservice itself is a monolith application. And I don't want my microservice to have a single file. I want to split logic between several files.
  3. Is it fine to keep all files in a single file?

I know that this question has been already asked several times, however I haven't found any complete answer.

I would be grateful for any help. Thanks

  • Possible duplicate of [How to import a local Go module not in GOPATH?](https://stackoverflow.com/questions/52123627/how-to-import-a-local-go-module-not-in-gopath) – Inigo Feb 27 '19 at 08:29

2 Answers2

2

First: Read "How to Write Go Code" and stick to it and only to it.

  1. There are no "local packages" for the go tool. Forget that distinction of local, non-local, remote, whatever. For the go tool there are just packages and these packages have an import path. This import path determines where the go tool will look for the code in the filesystem. If your import path is import "int.microsoft/secret/new/foo" then your code for package foo must be located on your machine under `$GOPATH/src/int.microsoft/secret/new/foo".

    For go build and (go install, go test, ...) there is no meaning in the import path at all: "int.microsoft/secret/new/foo" is no other string than "example1/foo" or "github.com/someone/tool7/bar". These are just strings and the packages are searched in these paths. None of these packages is a "local" package, they all are just packages.

    So if you put your code for package "whatever" under $GOPATH/src/some/strange/path/whatever you have to import it like import "some/strange/path/whatever".

  2. If you want to share your code with someone else, keep a remote backup or want to use a codehosting site like github to simplify synchronizing your three workstations you can upload your code to e.g. github. This is unrelated to how you import your packages.

    The only subcommand in the go tool where import path do have an additional meaning other than being a path is for go get which knows about a few code hosting sites like github and interpretes an import path of "github.com/someone/prjct" and can clone this git repo for your, conveniently placing it in the right folder under your $GOPATH.

    So: No. There is absolutely no need to upload each and every bit of code. (E.g. I have a lot of stuff just on my machine under $GOPATH/src/tmp).

    Splitting code into a) different packages and b different files is unrelated to all this. Just do as you find convenient and logical.

  3. Well, no; simpliy because you cannot keep several files in one file. Did you mean "several files in one folder"? Yes that is fine. Split into files and packages if the split makes sense. Take a look at the standard library or any other open source project.

Volker
  • 40,468
  • 7
  • 81
  • 87
  • Thank you so much for such complete answer and explanation ! Appreciate it –  Jun 25 '18 at 08:31
  • Is it possible to reference local modules on the PC (not on git) with the new *go.mod* file? Suppose you have two modules on your desktop in different directories. How to use code from one in the other? How does go, with the *go.mod* file, know where this directory is located? Right now I need to push new code from one directory to Git and synchronize it (`go mod tidy` or `go get -u ...`) with the other project when it's online. – Martin Niederl Sep 15 '18 at 21:34
  • 2
    @MartinNiederl Yes. Use the replace directive in the go.mod file like `replace girhub.com/you/pr => ../../some/where/local` – Volker Sep 16 '18 at 07:52
0
  1. your import path must be valid in $GOPATH/libPath syntax

  2. for that you can share your services source code as much as you want and just seprate bin by using multiple main package

  3. if you mean directory by second files, there is no best practice for that, as long as it doesnt concern your code complexity, you can do that too

CallMeLoki
  • 1,281
  • 10
  • 23