Home > Backend Development > Golang > GoLang Accessibility Modifiers: Public, Private, and Capitalization - How Does It Work?

GoLang Accessibility Modifiers: Public, Private, and Capitalization - How Does It Work?

Barbara Streisand
Release: 2024-12-05 04:50:13
Original
186 people have browsed it

GoLang Accessibility Modifiers: Public, Private, and Capitalization - How Does It Work?

Public, Private - Upper Case, Lower Case:

As a beginner transitioning from Delphi and C to GoLang, you may encounter confusion regarding accessibility modifiers and capitalization conventions. Let's explore this:

Capitalization Conventions

In GoLang, public accessibility is indeed indicated by using uppercase characters for function names. However, this rule only applies to identifiers within a single package.

When referencing external packages, such as "container/list," the package name itself is lowercase. This is because the package name is an alias assigned when importing the package, typically using the last part of the package path.

Public and Private Identifiers

Public identifiers follow the uppercase convention, while private identifiers are denoted by leading lowercase characters. Private identifiers are not accessible outside the package in which they are defined.

Example Clarification

In your example, the GetFactors function is public because its name starts with an uppercase letter. However, the "list" type is part of the imported "container/list" package. Its public identifier is List, following the uppercase convention within that package. When you use the lowercase "list" identifier, you are referencing the package alias, not the public identifier.

Alias and Package Names

While the package name is often the same as the last part of the package path, it is not always the case. The actual package name is defined in the package's declaration code. Therefore, it's important to refer to the package documentation for the correct package name and capitalization conventions.

The above is the detailed content of GoLang Accessibility Modifiers: Public, Private, and Capitalization - How Does It Work?. 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