golang 注释总结

about golang

Posted by cslqm on November 28, 2019

原则:

  • 一般名字是什么,注释第一个单词就是什么。
  • 只要是注释就必须是完整的句子。

const 中每一个元素都加注释

1
2
3
4
5
6
7
8
9
10
11
12
13
const (
	// LocalStore defines Local store mode.
	LocalStore VolumeStoreMode = 1

	// RemoteStore defines remote store mode.
	RemoteStore VolumeStoreMode = 2

	// CreateDeleteInCentral defines operate storage on gateway.
	CreateDeleteInCentral VolumeStoreMode = 4

	// UseLocalMetaStore defines store metadata on local host.
	UseLocalMetaStore VolumeStoreMode = 8
)

break continue 出现时尽量加注释

1
2
3
4
5
6
7
8
9
10
11
12
for _, p := range pluginList {
	_, ok := t.drivers[p.Name]
	if ok {
		// the driver has existed, ignore it.
		continue
	}

	d := NewRemoteDriverWrapper(p.Name, p)

	t.drivers[p.Name] = d
	driverList = append(driverList, d)
}

函数名有 Get 注释就写 return 什么

1
2
3
4
// GetAll returns all volume drivers.
func GetAll() ([]Driver, error) {
	return backendDrivers.GetAll()
}

注释是完整句子,但是不用强制包括第一个单词

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Create creates a volume.
func (proxy *remoteDriverProxy) Create(name string, opts map[string]string) error {
	var req = remoteVolumeCreateReq{
		Name: name,
		Opts: opts,
	}

	var resp remoteVolumeCreateResp

	if err := proxy.client.CallService(remoteVolumeCreateService, &req, &resp, true); err != nil {
		return err
	}

	if resp.Err != "" {
		return errors.New(resp.Err)
	}

	return nil
}


注释倾向

  1. 有返回值是,注释倾向描述返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Path returns local volume's path.
func (p *Local) Path(ctx context.Context, v *types.Volume) (string, error) {
	log.With(ctx).Debugf("Local volume mount path: %s", v.Name)

	if mp := v.Path(); mp != "" {
		return mp, nil
	}

	mountPath := v.Option("mount")
	if mountPath == "" {
		mountPath = path.Join(defaultDataPath, v.Name)
		if p.DataPath != "" {
			mountPath = path.Join(p.DataPath, v.Name)
		}
	}

	v.SetPath(mountPath)

	return mountPath, nil
}

  1. 仅返回 Error,倾向于描述操作
1
2
3
4
5
6
// Options returns local volume's options.
func (p *Local) Options() map[string]types.Option {
	return map[string]types.Option{
		"mount": {Value: "", Desc: "local directory"},
	}
}

简单结构体一般不具体描述各个元素

1
2
3
4
5
6
7
8
9
// Network defines the network struct.
type Network struct {
	Name string
	ID   string
	Type string
	Mode string

	Network libnetwork.Network
}

复杂结构体描述每一个元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Config refers to daemon's whole configurations.
type Config struct {
	sync.Mutex `json:"-"`

	//Volume config
	VolumeConfig volume.Config `json:"volume-config,omitempty"`

	// Network config
	NetworkConfig network.Config `json:"network-config,omitempty"`

	// Whether enable cri manager.
	IsCriEnabled bool `json:"enable-cri,omitempty"`

	// CRI config.
	CriConfig criconfig.Config `json:"cri-config,omitempty"`

	// Server listening address.
	Listen []string `json:"listen,omitempty"`

	// Debug refers to the log mode.
	Debug bool `json:"debug,omitempty"`

	// ContainerdAddr refers to the unix socket path of containerd.
	ContainerdAddr string `json:"containerd,omitempty"`

	// DefaultRegistry is daemon's default registry which is to pull/push/search images.
	DefaultRegistry string `json:"default-registry,omitempty"`

	// DefaultRegistryNS is daemon's default registry namespace used in pull/push/search images.
	DefaultRegistryNS string `json:"default-registry-namespace,omitempty"`

	// Home directory.
	HomeDir string `json:"home-dir,omitempty"`

	// ContainerdPath is the absolute path of containerd binary,
	// /usr/local/bin is the default.
	ContainerdPath string `json:"containerd-path,omitempty"`

	// TLS configuration
	TLS client.TLSConfig `json:"TLS,omitempty"`

	// Default OCI Runtime
	DefaultRuntime string `json:"default-runtime,omitempty"`

	// Enable lxcfs
	IsLxcfsEnabled bool `json:"enable-lxcfs,omitempty"`

	// LxcfsBinPath is the absolute path of lxcfs binary
	LxcfsBinPath string `json:"lxcfs,omitempty"`

	// LxcfsHome is the absolute path of lxcfs
	LxcfsHome string `json:"lxcfs-home,omitempty"`

	// ImagxeProxy is a http proxy to pull image
	ImageProxy string `json:"image-proxy,omitempty"`

	// QuotaDriver is used to set the driver of Quota
	QuotaDriver string `json:"quota-driver,omitempty"`

	// Configuration file of pouchd
	ConfigFile string `json:"config-file,omitempty"`

	// CgroupParent is to set parent cgroup for all containers
	CgroupParent string `json:"cgroup-parent,omitempty"`

	// Labels is the metadata of daemon
	Labels []string `json:"label,omitempty"`

	// EnableProfiler indicates whether pouchd setup profiler like pprof and stack dumping etc
	EnableProfiler bool `json:"enable-profiler,omitempty"`

	// Pidfile keeps daemon pid
	Pidfile string `json:"pidfile,omitempty"`

	// Default log configuration
	DefaultLogConfig types.LogConfig `json:"default-log-config,omitempty"`

	// RegistryMirrors is a list of registry URLs that act as a mirror for the default registry.
	RegistryMirrors []string `json:"registry-mirrors,omitempty"`

	// oom_score_adj for the daemon
	OOMScoreAdjust int `json:"oom-score-adjust,omitempty"`

	// runtimes config
	Runtimes map[string]types.Runtime `json:"add-runtime,omitempty"`

	// DefaultNamespace is passed to containerd.
	DefaultNamespace string `json:"default-namespace,omitempty"`

	// Snapshotter is passed to containerd, default to overlayfs
	Snapshotter string `json:"snapshotter,omitempty"`

	// AllowMultiSnapshotter allows multi snapshotter, default false
	AllowMultiSnapshotter bool `json:"allow-multi-snapshotter,omitempty"`

	// CgroupDriver sets cgroup driver for all containers
	CgroupDriver string `json:"cgroup-driver,omitempty"`

	// InsecureRegistries sets insecure registries to allow to pull
	// insecure registries.
	InsecureRegistries []string `json:"insecure-registries,omitempty"`

	// EnableBuilder enable builder functionality
	EnableBuilder bool `json:"enable-builder,omitempty"`

	// MachineMemory is the memory limit for a host.
	MachineMemory uint64 `json:"-"`
}