Search code examples
goprivatepubliccase-sensitiveaccess-modifiers

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?


Solution

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