当前位置:网站首页>Go -- go language naming specification
Go -- go language naming specification
2022-07-26 22:03:00 【redrose2100】
List of articles
One 、Go Introduction to language naming conventions
Naming rules involve variables 、 Constant 、 Global function 、 structure 、 Interface 、 Naming of methods, etc . Go Language has the following restrictions from the grammatical level : Any name that needs to be exposed must begin with a capital letter , Those that do not need to be exposed should start with a lowercase letter .
When naming ( Include constants 、 Variable 、 type 、 Function name 、 Structure fields and so on ) Start with a capital letter , Such as :GetUserName, Then objects that use this form of identifier can be used by the code of the external package ( The client program needs to import this package first ), This is called export ( Like in object-oriented languages public); If the name starts with a lowercase letter , It's invisible outside the package , But they are visible and available inside the entire package ( Like in object-oriented languages private )
Two 、Go Common naming conventions for languages
2.1 Package name
keep package Keep your name consistent with the directory , Try to use meaningful package names , brief , meaningful , Try not to conflict with the standard library . The package name should be a lowercase word , Don't use underscores or mixed case .
package user
package service
2.2 File naming
Try to take meaningful file names , brief , meaningful , Should be lowercase words , Use underscores to separate words .
first_demo.go
2.3 Structure naming
Hump nomenclature , The first letter is uppercase or lowercase according to the access control
struct Declaration and initialization formats are multiline , For example, the following :
type CustomerOrder struct {
Name string
Address string
}
order := CustomerOrder{
"tom", " Beijing haidian "}
2.4 The interface name
The naming rules are basically the same as the above structure type
The structure name of a single function is marked with “er” As a suffix , for example Reader , Writer .
type Reader interface {
Read(p []byte) (n int, err error)
}
2.5 Variable naming
It's like a structure , Variable names generally follow the hump method , The initial letter is capitalized or lowercase according to the access control principle , But when it comes to special nouns , The following rules need to be followed :
If the variable is private , The first word is a special noun , Use lowercase , Such as appService If the variable type is bool type , The name should be Has, Is, Can or Allow start
var isExist bool
var hasConflict bool
var canManage bool
var allowGitHook bool
2.6 Constant naming
Constants are made up of all uppercase letters , And use the underline participle
const APP_URL = "https://www.duoke360.com"
If it's a constant of enumeration type , You need to create the corresponding type first :
type Scheme string
const (
HTTP Scheme = "http"
HTTPS Scheme = "https"
)
2.7 Error handling
The principle of error handling is not to discard any returned err Call to , Do not use _ discarded , All must be dealt with . Error received , Or go back to err, Or use log Write it down as soon as possible return: Once an error occurs , Go back to , Try not to use panic, Unless you know what you're doing , If the error description is in English, it must be lowercase , No punctuation is required , Adopt independent error flow for processing
// Wrong writing
if err != nil {
// Error handling
} else {
// Normal code
}
// Write it correctly
if err != nil {
// Error handling
return // Or go ahead
}
// Normal code
2.7 unit testing
The naming convention of unit test file name is example_test.go The function name of the test case must be Test start , for example :TestExample Every important function must first write test cases , Test cases are submitted together with formal code to facilitate regression testing
边栏推荐
猜你喜欢
![[shutter -- geTx] pop up - dialog, snackBar, bottomsheet](/img/17/af2e45620e96a78235081145b7bb76.png)
[shutter -- geTx] pop up - dialog, snackBar, bottomsheet

Basic operation of (C language) files

Content management tools, blue bookmarks are enough

Let me show you the MySQL isolation level. What happens when two transactions operate on the same row of data at the same time?

Also on Data Governance

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

Pytoch uses RNN model to build person name classifier

matlab 画短时能量图

吃透负载均衡
![[mysql]substr usage - query the value of specific digits of a field in the table](/img/d5/68658ff15f204dc97abfe7c9e6b354.png)
[mysql]substr usage - query the value of specific digits of a field in the table
随机推荐
Japan approves the export of EUV photoresist to South Korea, and the crisis of Samsung and SK Hynix may be alleviated
Afnetworking understand
LDAP——实现用户统一登录管理
TASK04|分类分析
测试员:“我有五年测试经验”HR: “不,你只是把一年的工作经验用了五年”
内容管理工具,用蓝色书签就足够
Kalibr calibration realsensed435i -- multi camera calibration
织梦提示DedeCMS Error:Tag disabled:php!
[shutter -- geTx] pop up - dialog, snackBar, bottomsheet
Cmake compiling obs-studio-27.2.0
补充—非线性规划
Schematic diagram of MOS tube
Thoroughly understand the principle and implementation of service discovery
拼多多获得搜索词推荐 API
Altium designer 22 Chinese character garbled
09 expr 命令
Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated
FreeRTOS个人笔记-软件定时器
Pinduoduo gets search term recommendation API
JDBC operation and entry case of MySQL