Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic naming conventions of Go

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the basic naming specifications of Go". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "what are the basic naming specifications of Go" together.

basic rules

Go conventions use MixedCaps or MixedCaps instead of underscores to write the names of multiple words.

project name

The project name (warehouse name) can be named with letters and numbers.

Multiple words are recommended to be separated by a center line. At present, most items in github are separated by a center line. Camps are not recommended. Do not use underscores (component names in kubernetes are not allowed to use underscores).

Name can be a description of the function of the project; it can also be a code name (such as the name of a mythical figure, or Greek). There are two kinds of projects suitable for code names. One is the basic component of the company or the open source project. Generally, such projects have detailed documentation.

The project name (warehouse name) should avoid duplication as much as possible. If it is possible to duplicate, add necessary prefix or suffix to distinguish it.

Try to name them within three words.

Correct:

user、user-api、user-service,product、product-search、redis-go,druid、zeus、kubernetes.

Error:

user_api, Product package name

Keep the package name consistent with the directory, and the package name must be consistent in all source files in the same folder.

Package names should be lowercase words, not underscores or mixed case.

package domainpackage main file naming

Try to make sense of file names, short and meaningful. They should be lowercase words, separated by underscores.

approve_service.go struct naming

Use hump nomenclature with initials capitalized or lowercase depending on access control

struct declaration and initialization format takes multiple lines, for example:

type MainConfig struct { Port string `json:"port"` Address string `json:"address"`}config := MainConfig{"1234", "123.221.134"} Interface naming

Naming conventions Basic and structure types above

Structure names of individual functions have an "er" suffix, e.g. Reader , Writer.

type Reader interface { Read(p []byte) (n int, err error)} Variable naming

Similar to structs, variable names generally follow the hump method, with the first letter capitalized or lowercase according to access control principles, but when encountering unique nouns, the following rules need to be followed:

If the variable is private and has a noun as the first word, use lowercase, such as appService

If the variable type is bool, the name should start with Has, Is, Can or Allow.

The smaller the scope, the shorter the name. For example, index is represented by i inside the for loop.

constant naming

Constants should be composed of all capital letters and underlined participles

If it is a constant of enumeration type, you need to create the corresponding type first:

error handling

The principle of error handling is not to drop any call that returns err, do not use_drop, must be handled in full. Error received, either return err or log it

Early return: return immediately if an error occurs

Try not to use panic unless you know what you're doing.

Error description must be lowercase if it is English, no punctuation is required

Processing with independent error streams

unit test

The unit test file name specification is example_test.go The function name of the test case must start with Test, for example: TestExample Every important function must first write the test case, and the test case and the regular code are submitted together for regression testing.

Thank you for your reading. The above is the content of "What are the basic naming specifications of Go?" After studying this article, I believe that you have a deeper understanding of what the basic naming specifications of Go have. The specific use situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report