golang 命名规范和开发规范

目录

文件命名

package

变量

常量

接口

结构体

方法

注释

README


 

文件命名

文件命名一律采用小写,不用驼峰式,尽量见名思义,看见文件名就可以知道这个文件下的大概内容。
其中测试文件以_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://goreportcard.com/badge/github.com/ethereum/go-ethereum)](https://goreportcard.com/report/github.com/ethereum/go-ethereum)
[![Travis](https://travis-ci.org/ethereum/go-ethereum.svg?branch=master)](https://travis-ci.org/ethereum/go-ethereum)
[![Discord](https://img.shields.io/badge/discord-join%20chat-blue.svg)](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. |

README文件不仅是对自己代码的一个梳理,更是让别人在接手你的代码时能帮助快速上手的有效资料。

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
golang开发规范pdf是指一份关于使用Go语言进行开发规范的电子文档。这份规范的目的是为了提高团队开发效率、代码质量和维护性,确保项目的一致性和可扩展性。 在golang开发规范pdf中,通常会包含以下几个方面的内容: 1. 项目结构:定义项目的目录结构,包括如何组织代码文件、测试文件和配置文件等。这样可以使整个项目更加清晰、易于理解和维护。 2. 命名约定:定义变量、函数、类型和包等的命名规范。良好的命名规范可以增加代码的可读性,并且有助于其他开发人员更好地理解代码。 3. 代码风格: 指定代码缩进、换行、注释、命名等细节。统一的代码风格可以减少开发人员之间的沟通障碍,提高代码的可读性和可维护性。 4. 错误处理:明确规定如何处理错误,使用错误码还是异常处理,以及如何记录错误信息。规范的错误处理可以增加代码的健壮性,降低系统的崩溃风险。 5. 并发处理:定义并发编程的规范,包括使用goroutine、channel和锁等的最佳实践。良好的并发处理规范可以避免死锁和竞态条件等问题,提高系统的性能和稳定性。 6. 单元测试:指导如何编写单元测试,包括测试用例的组织方式、覆盖率要求以及测试结果的处理等。规范的单元测试有助于发现代码的潜在问题,并且可以提供一种自动化验证代码正确性的手段。 7. 文档编写:规范文档的编写方式,包括文档结构、格式规范和文档的更新机制等。清晰、详尽的文档可以减少沟通成本,提高项目的文档管理效率。 通过遵循golang开发规范pdf,开发人员可以更加高效、一致地进行开发,减少出错和重复的工作。此外,规范开发还有助于多人协作和后期维护,提高整个项目的质量和可持续性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值