-1

I am learning Go and how to structure a reasonable complex Go project. I can't seem to get a definitive answer on the following. When running build, will Go include all project packages in the build, or only those used?

Example: I'm developing a framework, which has multiple packages inside a /pkg folder. Some packages are for admin purposes and some are non admin. A non admin service would utilize the relevant packages for it's function, but exclude the admin ones. When building the service, will the service only build with the packages it has used from the framework?

Thank you!

Cazineer
  • 2,235
  • 4
  • 26
  • 44
  • 1
    This [answer](https://stackoverflow.com/a/55132813/1218512) shows some of the potential bloat a build can incur with package references - even if the package methods/types are not used. – colm.anseo Jan 21 '20 at 18:53
  • 1
    Much of the bloat is due to static linking. – Burak Serdar Jan 21 '20 at 19:08
  • Also see possible duplicates: [How to remove unused code at compile time?](https://stackoverflow.com/questions/42825926/how-to-remove-unused-code-at-compile-time/42827979#42827979); and [Splitting client/server code](https://stackoverflow.com/questions/38875016/splitting-client-server-code/38875901#38875901) – icza Jan 21 '20 at 19:50
  • 1
    "will Go include all project packages in the build" as there is no notion of "project" there are no "project packages". The question is nonsensical. – Volker Jan 21 '20 at 21:49

1 Answers1

4

A go build will only include the transitive closure of the packages referenced from the main. Within a package, it will only include the functions used. For types with methods, if the type is used then all the methods of that type will be included.

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • If I am understanding this correctly, if two packages reside in the /pkg folder and main.go only references one of the packages, the other will not be included in a build? – Cazineer Jan 21 '20 at 22:34
  • If you build the main package, then only those packages used by the main package will be built. So, yes. – Burak Serdar Jan 21 '20 at 22:39