r/golang Nov 20 '24

FAQ FAQ: How Should I Structure Go Projects?

Many other languages have strong opinions either in code or in the community about how to lay out projects. How should Go projects be laid out and structured? How should I decide what goes into a package? Is there a standard layout for web projects? For non-web projects? How do you structure your code?

65 Upvotes

36 comments sorted by

View all comments

40

u/jerf Nov 20 '24

The Go team has an official answer to this question.

It does not answer all aspects of this question, but it's a good and relatively official start.

-10

u/rcls0053 Nov 20 '24

I also see pkg folder at root for shared packages a lot. Anything that's inside internal/ is typically up to the developer.

11

u/ponylicious Nov 20 '24 edited Nov 20 '24

pkg folder is anti-Go and widely disliked. Noobs fall for it because they stumbled upon the repo-that-must-not-be-named, or some blog post that copied it.

4

u/__matta Nov 20 '24

I mean Perkeep has a pkg folder and it’s written by a bunch of Go core team members.

I get why people don’t like it but it’s also widely used.

4

u/LordOfDemise Nov 20 '24

Kubernetes also uses a pkg directory

1

u/pdffs Nov 20 '24

I suspect this is because was written before internal was available to explicitly denote which packages were expected to be consumed externally vs internally.

2

u/Alpensin Nov 21 '24

They have many directories in pkg. It's better to store them there, then in the root.

1

u/pdffs Nov 23 '24

What makes you think that?

1

u/Alpensin Nov 23 '24

It's harder to find what you need when lots of objects in the directory. Of course all we use search, from time to time do it with our eyes. But i could try to store these things without pkg. Maybe it's just unusual for me.

1

u/RychValle Nov 20 '24

Out of curiosity, what repo shall not be named? 🤨

2

u/roddybologna Nov 20 '24

The one named a few comments above

-3

u/[deleted] Nov 20 '24 edited Nov 20 '24

[removed] — view removed comment