|
1 | 1 | # Go Code Convention
|
2 | 2 |
|
| 3 | +## Copyright |
| 4 | + |
| 5 | +For any open source project, there must be a LICENSE file in the repository root to claim the rights. |
| 6 | + |
| 7 | +Here are two examples of using [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) and MIT License. |
| 8 | + |
| 9 | +### Apache License, Version 2.0 |
| 10 | + |
| 11 | +This license requires to put following content at the beginning of every file: |
| 12 | + |
| 13 | +``` |
| 14 | +// Copyright [yyyy] [name of copyright owner] |
| 15 | +// |
| 16 | +// Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 17 | +// not use this file except in compliance with the License. You may obtain |
| 18 | +// a copy of the License at |
| 19 | +// |
| 20 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | +// |
| 22 | +// Unless required by applicable law or agreed to in writing, software |
| 23 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | +// License for the specific language governing permissions and limitations |
| 26 | +// under the License. |
| 27 | +``` |
| 28 | + |
| 29 | +Replace `[yyyy]` with the creation year of the file. Then use personal name for personal projects, or organization name for team projects to replace `[name of copyright owner]`. |
| 30 | + |
| 31 | +### MIT License |
| 32 | + |
| 33 | +This license requires to put following content at the beginning of every file: |
| 34 | + |
| 35 | +``` |
| 36 | +// Copyright [yyyy] [name of copyright owner]. All rights reserved. |
| 37 | +// Use of this source code is governed by a MIT-style |
| 38 | +// license that can be found in the LICENSE file. |
| 39 | +``` |
| 40 | + |
| 41 | +Replace `[yyyy]` with the creation year of the file. |
| 42 | + |
| 43 | +### Other notes |
| 44 | + |
| 45 | +- Other types of license can follow the template of above two examples. |
| 46 | + |
| 47 | +- If a file has been modified by different individuals and/or organizations, and multiple licenses are compatiable, then change the first line to multiple lines and sort them in the order of time: |
| 48 | + |
| 49 | + ``` |
| 50 | + // Copyright 2011 Gary Burd |
| 51 | + // Copyright 2013 Unknwon |
| 52 | + ``` |
| 53 | + |
| 54 | +- Spefify which license is used for the project in bottom of the README file: |
| 55 | + |
| 56 | + ``` |
| 57 | + ## License |
| 58 | + |
| 59 | + This project is under the MIT License. See the [LICENSE](LICENSE) file for the full license text. |
| 60 | + ``` |
| 61 | + |
3 | 62 | ## Project structure
|
4 | 63 |
|
5 | 64 | For server-side services and CLI applications, they generally follow the same style of project structure as follows:
|
@@ -189,4 +248,144 @@ import (
|
189 | 248 | func (r *UserResolver) Email(ctx context.Context) (string, error) {
|
190 | 249 | ```
|
191 | 250 |
|
192 |
| - |
| 251 | +
|
| 252 | +## Naming rules |
| 253 | +
|
| 254 | +### Directory |
| 255 | +
|
| 256 | +- Aovid using underscores (`_`) in directory names, use hyphens (`-`) instead. |
| 257 | +
|
| 258 | +### File |
| 259 | +
|
| 260 | +- Avoid using hyphens (`-`) in file names, use underscores (`_`) instead. |
| 261 | +- The file contains the entry point of the application or package should be named as `main.go` or same as the package. |
| 262 | +
|
| 263 | +### Function and method |
| 264 | +
|
| 265 | +- If the main purpose of the function or method is returning a `bool` type value, the name of function or method should starts with `Has`, `Is`, `Can` or `Allow`, etc. |
| 266 | +
|
| 267 | + ```go |
| 268 | + func HasPrefix(name string, prefixes []string) bool { ... } |
| 269 | + func IsEntry(name string, entries []string) bool { ... } |
| 270 | + func CanManage(name string) bool { ... } |
| 271 | + func AllowGitHook() bool { ... } |
| 272 | + ``` |
| 273 | +
|
| 274 | +### Constants |
| 275 | +
|
| 276 | +- Constant should use camel cases except for coined terms and brand names (see later): |
| 277 | +
|
| 278 | + ```go |
| 279 | + const appVersion = "0.13.0+dev" |
| 280 | + ``` |
| 281 | +
|
| 282 | +- If you need enumerated type, you should define the corresponding type first: |
| 283 | +
|
| 284 | + ```go |
| 285 | + type Scheme string |
| 286 | + |
| 287 | + const ( |
| 288 | + HTTP Scheme = "http" |
| 289 | + HTTPS Scheme = "https" |
| 290 | + ) |
| 291 | + ``` |
| 292 | +
|
| 293 | +- If functionality of the module is relatively complicated and easy to mixed up with constant name, you can add prefix to every constant for the enumerated type: |
| 294 | +
|
| 295 | + ```go |
| 296 | + type PullRequestStatus int |
| 297 | + |
| 298 | + const ( |
| 299 | + PullRequestStatusConflict PullRequestStatus = iota |
| 300 | + PullRequestStatusChecking |
| 301 | + PullRequestStatusMergable |
| 302 | + ) |
| 303 | + ``` |
| 304 | +
|
| 305 | +### Variables |
| 306 | +
|
| 307 | +- A variable name should follow general English expression or shorthand. |
| 308 | +
|
| 309 | +- In relatively simple (less objects and more specific) context, variable name can use simplified form as follows: |
| 310 | +
|
| 311 | + - `userID` to `uid` |
| 312 | + - `repository` to `repo` |
| 313 | +
|
| 314 | +- If variable type is `bool`, its name should start with `Has`, `Is`, `Can` or `Allow`, etc. |
| 315 | +
|
| 316 | + ```go |
| 317 | + var isExist bool |
| 318 | + var hasConflict bool |
| 319 | + var canManage bool |
| 320 | + var allowGitHook bool |
| 321 | + ``` |
| 322 | +
|
| 323 | +- Same rules also apply for defining structs: |
| 324 | +
|
| 325 | + ```go |
| 326 | + // Webhook represents a web hook object. |
| 327 | + type Webhook struct { |
| 328 | + ID int64 |
| 329 | + RepoID int64 |
| 330 | + OrgID int64 |
| 331 | + URL string |
| 332 | + ContentType HookContentType |
| 333 | + Secret string |
| 334 | + Events string |
| 335 | + *HookEvent |
| 336 | + IsSSL bool |
| 337 | + IsActive bool |
| 338 | + HookTaskType HookTaskType |
| 339 | + Meta string |
| 340 | + LastStatus HookStatus |
| 341 | + CreatedAt time.Time |
| 342 | + UpdatedAt time.Time |
| 343 | + } |
| 344 | + ``` |
| 345 | +
|
| 346 | +### Coined term and brand name |
| 347 | +
|
| 348 | +When you encounter coined terms and brand names, naming should respect the original or the most widely accepted form for the letter case. For example, use GitHub" not "Github", use "API" not "Api", use "ID" not "Id". |
| 349 | +
|
| 350 | +When the coined term and brand name is the first word in a variable or constant name, keep them having the same cases. For example, `apiClient`, `SSLCertificate`. |
| 351 | +
|
| 352 | +Here is a list of words which are commonly identified as unique nouns: |
| 353 | +
|
| 354 | +```go |
| 355 | +// A GonicMapper that contains a list of common initialisms taken from golang/lint |
| 356 | +var LintGonicMapper = GonicMapper{ |
| 357 | + "API": true, |
| 358 | + "ASCII": true, |
| 359 | + "CPU": true, |
| 360 | + "CSS": true, |
| 361 | + "DNS": true, |
| 362 | + "EOF": true, |
| 363 | + "GUID": true, |
| 364 | + "HTML": true, |
| 365 | + "HTTP": true, |
| 366 | + "HTTPS": true, |
| 367 | + "ID": true, |
| 368 | + "IP": true, |
| 369 | + "JSON": true, |
| 370 | + "LHS": true, |
| 371 | + "QPS": true, |
| 372 | + "RAM": true, |
| 373 | + "RHS": true, |
| 374 | + "RPC": true, |
| 375 | + "SLA": true, |
| 376 | + "SMTP": true, |
| 377 | + "SSH": true, |
| 378 | + "TLS": true, |
| 379 | + "TTL": true, |
| 380 | + "UI": true, |
| 381 | + "UID": true, |
| 382 | + "UUID": true, |
| 383 | + "URI": true, |
| 384 | + "URL": true, |
| 385 | + "UTF8": true, |
| 386 | + "VM": true, |
| 387 | + "XML": true, |
| 388 | + "XSRF": true, |
| 389 | + "XSS": true, |
| 390 | +} |
| 391 | +``` |
0 commit comments