Golang命名规范和开发规范

太叔景同
2023-12-01

命名

文件命名

文件命名一律采用小写,不用驼峰式,尽量见名思义,看见文件名就可以知道这个文件下的大概内容。
其中测试文件以_test.go结尾,除测试文件外,命名不出现_。

例子:

stringutil.go, stringutil_test.go

package

包名用小写,使用短命名,尽量和标准库不要冲突。
包名统一使用单数形式。

变量

变量命名一般采用驼峰式,当遇到特有名词(缩写或简称,如DNS)的时候,特有名词根据是否私有全部大写或小写。

例子:

apiClient、URLString

常量

同变量规则,力求语义表达完整清楚,不要嫌名字长。
如果模块复杂,为避免混淆,可按功能统一定义在package下的一个文件中。

接口

单个函数的接口名以 er 为后缀

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

两个函数的接口名综合两个函数名,如:

type WriteFlusher interface {
    Write([]byte) (int, error)
    Flush() error
}

三个以上函数的接口名类似于结构体名,如:

type Car interface {
    Start() 
    Stop()
    Drive()
}

结构体

结构体名应该是名词或名词短语,如Account,Book,避免使用Manager这样的。
如果该数据结构需要序列化,如json, 则首字母大写, 包括里面的字段。

方法

方法名应该是动词或动词短语,采用驼峰式。将功能及必要的参数体现在名字中, 不要嫌长, 如updateById,getUserInfo.

如果是结构体方法,那么 Receiver 的名称应该缩写,一般使用一个或者两个字符作为 Receiver 的名称。如果 Receiver 是指针, 那么统一使用p。 如:

func (f foo) method() {
	...
}

func (p *foo) method() {
	...
}

对于Receiver命名应该统一, 要么都使用值, 要么都用指针。

注释

每个包都应该有一个包注释,位于 package 之前。如果同一个包有多个文件,只需要在一个文件中编写即可;如果你想在每个文件中的头部加上注释,需要在版权注释和 Package前面加一个空行,否则版权注释会作为Package的注释。如:

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net

每个以大写字母开头(即可以导出)的方法应该有注释,且以该函数名开头。如:

// Get 会响应对应路由转发过来的 get 请求
func (c *Controller) Get() {
    ...
}

大写字母开头的方法以为着是可供调用的公共方法,如果你的方法想只在本包内掉用,请以小写字母开发。如:

func (c *Controller) curl() {
    ...
}

注释应该用一个完整的句子,注释的第一个单词应该是要注释的指示符,以便在 godoc 中容易查找。

注释应该以一个句点 . 结束。

README

每个文件夹下都应该有一个README文件,该文件是对当前目录下所有文件的一个概述,和主要方法描述。并给出一些相应的链接地址,包含代码所在地、引用文档所在地、API文档所在地,以以太坊的README文档为例,如:

## Go Ethereum

Official golang implementation of the Ethereum protocol.

[![API Reference](
https://camo.githubusercontent.com/915b7be44ada53c290eb157634330494ebe3e30a/68747470733a2f2f676f646f632e6f72672f6769746875622e636f6d2f676f6c616e672f6764646f3f7374617475732e737667
)](https://godoc.org/github.com/ethereum/go-ethereum)
[![Go Report Card](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9nb3JlcG9ydGNhcmQuY29tL2JhZGdlL2dpdGh1Yi5jb20vZXRoZXJldW0vZ28tZXRoZXJldW0?x-oss-process=image/format,png)](https://goreportcard.com/report/github.com/ethereum/go-ethereum)
[![Travis](https://imgconvert.csdnimg.cn/aHR0cHM6Ly90cmF2aXMtY2kub3JnL2V0aGVyZXVtL2dvLWV0aGVyZXVtLnN2Zw?x-oss-process=image/format,png)](https://travis-ci.org/ethereum/go-ethereum)
[![Discord](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9pbWcuc2hpZWxkcy5pby9iYWRnZS9kaXNjb3JkLWpvaW4lMjBjaGF0LWJsdWUuc3Zn?x-oss-process=image/format,png)](https://discord.gg/nthXNEv)

## Executables

The go-ethereum project comes with several wrappers/executables found in the `cmd` directory.

| Command    | Description |
|:----------:|-------------|
| **`geth`** | Our main Ethereum CLI client. It is the entry point into the Ethereum network (main-, test- or private net), capable of running as a full node (default), archive node (retaining all historical state) or a light node (retrieving data live). It can be used by other processes as a gateway into the Ethereum network via JSON RPC endpoints exposed on top of HTTP, WebSocket and/or IPC transports. `geth --help` and the [CLI Wiki page](https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options) for command line options. |
| `abigen`   | Source code generator to convert Ethereum contract definitions into easy to use, compile-time type-safe Go packages. It operates on plain [Ethereum contract ABIs](https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI) with expanded functionality if the contract bytecode is also available. However it also accepts Solidity source files, making development much more streamlined. Please see our [Native DApps](https://github.com/ethereum/go-ethereum/wiki/Native-DApps:-Go-bindings-to-Ethereum-contracts) wiki page for details. |
| `bootnode` | Stripped down version of our Ethereum client implementation that only takes part in the network node discovery protocol, but does not run any of the higher level application protocols. It can be used as a lightweight bootstrap node to aid in finding peers in private networks. |
| `evm`      | Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode. Its purpose is to allow isolated, fine-grained debugging of EVM opcodes (e.g. `evm --code 60ff60ff --debug`). |
| `rlpdump`  | Developer utility tool to convert binary RLP ([Recursive Length Prefix](https://github.com/ethereum/wiki/wiki/RLP)) dumps (data encoding used by the Ethereum protocol both network as well as consensus wise) to user friendlier hierarchical representation (e.g. `rlpdump --hex CE0183FFFFFFC4C304050583616263`). |
| `swarm`    | Swarm daemon and tools. This is the entrypoint for the Swarm network. `swarm --help` for command line options and subcommands. See [Swarm README](https://github.com/ethereum/go-ethereum/tree/master/swarm) for more information. |
| `puppeth`  | a CLI wizard that aids in creating a new Ethereum network. |
 类似资料: