문제

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?

도움이 되었습니까?

해결책

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: starting Go 1.5 (Q2/Q3 2015), you will get "protected" import as well (named "internal")!

See Go 1.4 doc:

Go's package system makes it easy to structure programs into components with clean boundaries, but there are only two forms of access: local (unexported) and global (exported).
Sometimes one wishes to have components that are not exported, for instance to avoid acquiring clients of interfaces to code that is part of a public repository but not intended for use outside the program to which it belongs.

The Go language does not have the power to enforce this distinction, but as of Go 1.4 the go command introduces a mechanism to define "internal" packages that may not be imported by packages outside the source subtree in which they reside.

To create such a package, place it in a directory named internal or in a subdirectory of a directory named internal.
When the go command sees an import of a package with internal in its path, it verifies that the package doing the import is within the tree rooted at the parent of the internal directory.
For example, a package .../a/b/c/internal/d/e/f can be imported only by code in the directory tree rooted at .../a/b/c.
It cannot be imported by code in .../a/b/g or in any other repository.

For Go 1.4, the internal package mechanism is enforced for the main Go repository;
from 1.5 and onward it will be enforced for any repository.

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:

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top