Exceptions to golang function naming convention

WBOY
Release: 2024-04-30 14:48:01
Original
1080 people have browsed it

In addition to following camel case naming, there are the following exceptions to Go function naming: method functions: start with a lowercase letter, and the receiver type comes first; exported functions: start with an underscore; constructor: start with New, followed by the target type; test Function: starts with Test, followed by the function/method to be tested; anonymous function: has no name and is created through function literals.

Exceptions to golang function naming convention

Go function naming convention: exceptions

In the Go language, functions are named in camel case, with the first letter lowercase , the first letter of subsequent words is capitalized. However, in some cases, exceptions are made.

1. Method functions

Method functions are functions that belong to a structure, and their naming follows different conventions from other functions. Method function names begin with a lowercase letter, subsequent words have the first letter in uppercase, and the function name is preceded by the receiver type. For example:

type Person struct {
    name string
}

// GetName 方法
func (p Person) GetName() string {
    return p.name
}
Copy after login

2. Exported functions

Exported functions are functions that can be accessed through other code outside the package. Their names begin with an underscore (_), indicating that they are exported functions. For example:

func _privateFunction() {
    // 私有函数
}

func _ExportFunction() {
    // 导出函数
}
Copy after login

3. Constructor

In the Go language, the constructor is a special type conversion function used to convert a type of value into Another type. The name of the constructor begins with New, followed by the name of the type to be converted. For example:

func NewInt(i int) *Int {
    return &Int{i}
}
Copy after login

4. Test function

In the test file, the name of the test function starts with Test, followed by the function to be tested or method name. They usually use lowercase camelCase nomenclature. For example:

func TestGetPersonName(t *testing.T) {
    // 测试 GetPersonName() 方法
}
Copy after login

5. Anonymous functions

Sometimes, you need to use anonymous functions, that is, functions without names. Anonymous functions are created using function literals and cannot be accessed by external code. For example:

func() {
    // 匿名函数
}
Copy after login

In these exceptions, your function naming should still be clear, concise, and reflect the purpose of the function. Applying these exceptions consistently will help ensure the readability and maintainability of your code.

The above is the detailed content of Exceptions to golang function naming convention. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template