A new folder "pkg" in the root. How do I use this?

I downloaded 1.20 and run go mod tidy using VS Code. And a folder was created at the root level (project level) named “pkg”. I wonder if this is anything wrong? Why does this appear? How do I use it?

you put there all your packages that you wish to make public in your project. By public I mean other people can import and use them.

1 Like

As @heidi says, it is a convention to put there the packages that are ok to be imported by other projects outside the repository. But it just a convention, one of many, and not a rule.

See here for more info on conventions on folder layouts: GitHub - golang-standards/project-layout: Standard Go Project Layout

Section about /pkg: project-layout/pkg at master · golang-standards/project-layout · GitHub

2 Likes

Does this mean that this will be imported automatically? To all projects?
Or do I have to “import…” to avoid not needed libraries?

you obviously need to use the import statement. Nothing else, besides what you specifically import using ‘import’, will get imported.

1 Like

Does this mean that this will be imported automatically? To all projects?
Or do I have to “import…” to avoid not needed libraries?

No nothing will happen automatically and the behavior of importing/not importing will be exactly the same as for a regular folder. It is only a way to signal to other developers that you developed the packages within that folder for that purpose. So it is only there to communicate your intentions to other humans. Nothing else will happen and you don’t have to do it if you don’t want to.

As an example how that might work: let’s say I develop a project and that project exposes an API. To increase adoption, I could also write a client for that API so that if other developers want to use that API, they don’t have to do it themselves. They can just import my client and use that. To signal that this client exists and that it is ready to use for others, I could put it in /pkg. Whenever another developer comes across my repository they would quickly see that it is available that way.

Another example: I am just creating some personal project and I don’t intend to even share it with others. I will just not create /pkg at all and do whatever is easiest at the moment.

1 Like

Just a note about the golang-standards org and that “Standard Go Project Layout”. It is not standard at all, as has been noted by Russ Cox:

If it works for people, great. But I do still see it causing more confusion than good with newer gophers.

2 Likes

FWIW: all packages not in a main or internal directory are public packages. I for one really really really dislike this usage of pkg. With that said, it’s fine if you want to use it in your repos.

2 Likes

I did not have a choice. The “pkg” folder appeared automatically when executing “go mod tidy”. Can it be a VS Code thing? The first thought was that this is wrong. But it might be useful. For now it is just an almost empty annoying folder that I cannot get rid of permanently. :slight_smile:

I use vscode almost exclusively these days when working in go. Interesting. I’ve never seen that happen before. is there anything in pkg ?

The only other thing I can think of is that your GOPATH variable is set weirdly: What is the output of go env GOPATH and how does that location relate to where your code is?

/Users/sibert/go

It is the “project folder” with a folder of different projects. What should the GOPATH be?

I don’t suggest putting your code in the same place as go env GOPATH. It used to be that you needed to do that, but in a special way based on the package name. Let me explain: This is the way it used to have to be, but isn’t the case anymore…

So ~/go is your GOPATH. And the package you are working on is located on github.com/user/repo, this could would have to be place in ~/go/src/github.com/user/repo for the go tooling to find it.

This works well for a place like Google, but doesn’t work that well for individual developers. So (and I’m ignoring some of the tooling that was developed to deal with that) eventually the Go team developed the idea of modules and the go mod command which, among other things, allows you to put your code mostly anywhere on your filesystem.

You could keep using ~/go, but I suggest moving it somewhere else and pkg (and possibly other directories) won’t appear in your code.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.