了解是否需要将.go文件放在pkg文件夹下? [关闭]

It's very often I see .go files committed to pkg folder, like here https://github.com/kubernetes/apiserver what particular reason doing that vs using vendor folder for 3rdparty dependencies or commit your own code into src dir?

Please clarify further.

  • The pkg folder on that repo you linked is where they put the packages they wrote. Reason? well they need to put them somewhere, they chose pkg.

  • You use vendor folder when you don't want future changes to 3rd party packages you've used possibly make your program unstable, in that case you "vendor" those 3rd party packages. So you don't put your own packages in vendor folder for obvious reasons.

  • Which src? $GOPATH/src? you can do it! probably, I haven't test it. But you can only have one project in that case, and that's the project that you've placed there.

There's no need to have your project's files under a pkg folder. And there's no need to have a pkg folder, inside your project, in the first place.

pkg is not a "reserved" folder name, at least not in the context of your project.

vendor, on the other hand, has special significance for the Go compiler and is inteded to hold code that was written as part of a separate project, e.g. 3rd party packages. Therefore it makes no sense, for the linked project, to move the code they have under pkg into vendor.

If you want to understand why kubernetes or other projects decided to use pkg you would best be served by enquiring through those projects' communication channels (mailing lists, irc, social media, etc).