Go - Public Private - Upper Case Lower Case

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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?

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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/20850047/public-private-upper-case-lower-case

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils