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 naming conventions of Golang

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the naming conventions of Golang". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Few people summarize some naming conventions, or maybe the author is ill-informed. As a two-year golang developer, I summarized some common naming conventions based on many well-known projects, such as moby, kubernetess and so on.

Naming conventions can make the code easier to read with fewer errors.

If you have different opinions, you are welcome to complain and discuss.

File naming convention

Since the file has nothing to do with the package and avoids the problem of windows case, the recommended specification is as follows:

All file names should be lowercase, different words should be separated by underscores, and naming should be as clear as possible.

Constant naming convention

The constant is clearly named with camelcase. The example is as follows

Const todayNews = "Hello" / / const should be organized in parentheses if it exceeds a constant (systemName = "What" sysVal = "dasdsada")

Variable naming convention

Like constant naming, variables should also be named as humps, but be careful not to match or start with the package name

Var x stringx: = new (string)

Function naming specification

Due to the particularity of Golang (using case to control the visibility of functions), except for special performance tests and unit test functions, the following principles should be followed

Use hump naming

If you do not need access outside the package, please start with a lowercase function

If you need to expose it to access outside the package, you need to use the function name that begins with uppercase.

A typical function naming method is as follows:

/ / double slashes are used for all comments. The method exposed by the object func (* fileDao) AddFile (file * model.File) bool {result: = db.NewRecord (* file) if result {db.Create (file)} return result} / / functions that do not need to be accessed outside the package are as follows: func removeCommaAndQuote (content string) string {re, _: = regexp.Compile ("[\\ `\\,] +") return strings.TrimSpace (re.ReplaceAllString (content, ")}

Interface naming specification

API naming should also follow the hump style. You can use type alias to define the uppercase type for out-of-package access.

Type helloWorld interface {func Hello ();} type SayHello helloWorld

Struct naming convention

Similar to the interface naming convention

Receiver naming convention

The concept of receiver exists in golang

Receiver names should be kept consistent as far as possible to avoid some semantic keywords in this, super, and other languages as follows

Type A struct {} func (a * A) methodA () {} func (a * A) methodB () {a.methodA ()}

Annotation specification

Double slashes should be used for all comments

Other

Formatted, tab is not a space, can be compatible with go fmt

This is the end of the content of "what are the naming conventions of Golang". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report