Public vs. Private: Upper vs. Lower Case in Go
Many programmers coming from languages like Delphi and C face confusion when working with access modifiers in Go. This article aims to clarify the rules for defining public and private members in Go.
In Go, as you discovered, public access is achieved by naming functions in upper case. However, you encountered a discrepancy with the list package, where public members are in lower case.
The key to understanding this discrepancy lies in the difference between package names and member names. The list in *list.List refers to the package name, which is traditionally written in lower case in Go. The List part, on the other hand, represents the public member name.
The rule for member visibility in Go is simple: public members are named in upper case, while private members are named in lower case. This applies to functions, types, variables, and constants declared within a package.
Therefore, the GetFactors function, being public, is correctly named in upper case. The list.List type, despite using a lower case package name, is a public member within the container/list package.
For local references to types from imported packages, you can use the default package name (e.g., l := list.New()). Alternatively, you can alias imported packages to use a different name
The above is the detailed content of Why Does `list.List` Use Lowercase While Other Public Members in Go Use Uppercase?. For more information, please follow other related articles on the PHP Chinese website!