Ztyx
Ztyx

Reputation: 14908

Should functions/constants/variables be exported or not in `package main`?

I'm developing a tiny project that has a single package main. AFAIK, best-practice for small Golang binary projects is to have all code in a single (main) namespace, so that's what I've done.

Just curious, within a package main, is it best practice to keep functions/constants/variables exported (MyFunction) or unexported (myFunction)?

Upvotes: 0

Views: 595

Answers (2)

John Murphy
John Murphy

Reputation: 76

The most important thing is to be consistent. I prefer to name un-exported items in package main starting with lowercase even though they can't be exported. This is because it's an additional hint that these entities aren't used outside their containing package. You don't need to know that the declaration is in main to know that the thing being declared is not used externally.

Upvotes: 3

jcbwlkr
jcbwlkr

Reputation: 7999

Really it doesn't matter. main packages can't be imported so whether you export them or not doesn't matter in that regard.

However if you do export them then tools like golint will encourage you to document them so maybe that's a good reason to go ahead an export them.

Upvotes: 3

Related Questions