1. 程式人生 > >Golang命名規範和開發規範

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文件的程式設計師絕對都是一個負責任的好程式
員!