New to GoLang, coming from Delphi, C++ :
First time I tried to create my own package in Go, I followed all the instructions about how to lay out the workspace, etc, but I kept on getting a compiler error:
./myPackage.go:52: undefined: myFunc
After poking around a bit I discovered that the public access modifier in Go is achieved simply by declaring a function in upper case. Great.
But when I started experimenting with the container classes - List
for starters, I discovered I had to declare a List reference return value like this:
func GetFactors(value *int64) *list.List {...
*list
is in lower case.
Same when I declared a local reference to a list - I had to use:
l := list.New()
Again, lower case for list
.
So, I'm confused. What is the rule? The list calls and references are obviously public, or I wouldn't be able to call/use them - so why are they in lower case?
What are uppercase letters? Uppercase letters are capital letters—the bigger, taller versions of letters (like W), as opposed to the smaller versions, which are called lowercase letters (like w). Uppercase means the same thing as capital. Uppercase letters can also be called capitals.
Go uses capitalization to determine identifier visibility. Those that start with a lower case letter are package-private, and those that start with a capital are package-public.
In sentence case, only the first letters of the first word and proper nouns are in uppercase. By contrast, title case has major words in uppercase and minor words in lowercase (unless they are the first or last word of a title).
It's actually a remnant of a past where printing presses had manually set letters. Small letters, which were used the majority of the time, were kept in the lower, easier to access case. Where as large letters were kept in the upper.
In this case, list
is the name of the package, which you are importing via import "container/list"
, and its public members are upper case, like List
.
The rule is that public functions, types, etc., should be upper case.
You can alias imported packages however you want, but by default it is just the name of the last part of the package path--in this case, list
.
Update: It's not the last part of the package path. It's the actual package name (which is often the same thing).
Note: the Go Spec for package name don't mention that a package name is always in lowercase.
It only state that its name is represented by an identifier, which is composed of a collection of "letter".
This thread does clarify:
Package names can be anything, you can start them with an uppercase letter if you want to.
But the convention is all lowercase, which I guess saves you the hassle of typing an uppercase letter.The uppercase/lowercase exportability isn't really relevant to packages since you can't have a private package.
Once you know that, it is easier to recognize:
list.New()
for a constructor (always at the package level, to build an initialized instance of a type), like os.NewFile()
,list.List
for a struct type of the package list (the other struct type of that same package being list.Element
).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With