Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for not-exported type names in Go

I like to name my types using Pascal case - starting with an upper case letter. In Go this implies the name is exported.

To avoid export, I've started to prefix the type name with undercsore instead of lower-casing the first letter.

E.g: Instead of type Column struct{}, I use type _Column struct{} to avoid export.

I haven't seen this naming scheme used, but neither found any reason not to use it.

Since golint accepts it without complaint, I guess this is OK?

Conclusion: Based on answers and comments I've decided to stay with lower-cased type names.

like image 744
runec Avatar asked Oct 29 '25 22:10

runec


2 Answers

I'd suggest using column in preference to _Column, on the basis that the style used by the standard libraries follow that naming convention.

This is not explicit in the Names section of the style guide, but based on the fact that underscores are generally discouraged, I'd say that using _Column is, at best, not idiomatic.

like image 145
Vatine Avatar answered Nov 02 '25 02:11

Vatine


"I like to" and go don't super mix.

There are idiomatic bits and tooling enforced bits.

The community sticking to the standards makes for codebases that can be reasonably easy to read and comprehend by others.

I find this to be one of the best attributes of go.

Sure, channels and goroutines are nice.
Easily being able to read a codebase is often much more valuable.

like image 23
David Budworth Avatar answered Nov 02 '25 02:11

David Budworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!