Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public, Private - Upper Case, Lower Case:

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?

like image 397
Vector Avatar asked Dec 31 '13 00:12

Vector


People also ask

What is the difference between upper case and 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.

Why are go functions capitalized?

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.

What is upper case and title case?

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).

Why do they call it upper case and lower case?

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.


2 Answers

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).

like image 73
Eve Freeman Avatar answered Oct 05 '22 23:10

Eve Freeman


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).
like image 28
VonC Avatar answered Oct 06 '22 00:10

VonC