Golang命名規范和開發規范


命名

文件命名

文件命名一律采用小寫,不用駝峰式,盡量見名思義,看見文件名就可以知道這個文件下的大概內容。
其中測試文件以_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文件不僅是對自己代碼的一個梳理,更是讓別人在接手你的代碼時能幫助快速上手的有效資料。所以每一個寫好README文檔的程序員絕對都是一個負責任的好程序
員!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM