Home > Backend Development > Golang > Why Does `list.List` Use Lowercase While Other Public Members in Go Use Uppercase?

Why Does `list.List` Use Lowercase While Other Public Members in Go Use Uppercase?

Linda Hamilton
Release: 2024-11-19 18:44:02
Original
673 people have browsed it

Why Does `list.List` Use Lowercase While Other Public Members in Go Use Uppercase?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template