Golang Naming conventions

Source: Internet
Author: User
Tags webhook

Golang Naming conventions

Image

Filename

    • The main portal file for the entire app or package should be or be the same as the main.go app name shorthand. For example: Gogs The main portal file is named gogs.go .

Function or method

    • If a function or method is a judgment type (the return value is primarily a bool type), the name should begin with, or, for a Has Is Can Allow judgmental verb:

      func HasPrefix(name string, prefixes []string) bool { ... }func IsEntry(name string, entries []string) bool { ... }func CanManage(name string) bool { ... }func AllowGitHook() bool { ... }

Constant

    • Constants need to be made up of all uppercase letters and use underscore participles:

      const APP_VER = "0.7.0.1110 Beta"
    • If you are a constant of an enumeration type, you need to create the appropriate type first:

      type Scheme stringconst (    HTTP  Scheme = "http"    HTTPS Scheme = "https")
    • In cases where the functionality of the module is more complex and the constant name is easily confused, the full prefix can be used to better differentiate the enumeration type:

      type PullRequestStatus intconst (    PULL_REQUEST_STATUS_CONFLICT PullRequestStatus = iota    PULL_REQUEST_STATUS_CHECKING    PULL_REQUEST_STATUS_MERGEABLE)

Variable

  • Variable naming basically follows the corresponding English expression or shorthand.

  • In a relatively simple environment (with a small number of objects and strong targeting), you can abbreviate some names from full words to a single letter, for example:

    • usercan be shortened tou
    • userIDcan be shorteneduid
  • If the variable type is bool type, the name should be Has ,, Is Can or begin with Allow :

    var isExist boolvar hasConflict boolvar canManage boolvar allowGitHook bool
  • The previous rule also applies to the structure definition:

    // Webhook represents a web hook object.type Webhook struct {    ID           int64 `xorm:"pk autoincr"`    RepoID       int64    OrgID        int64    URL          string `xorm:"url TEXT"`    ContentType  HookContentType    Secret       string `xorm:"TEXT"`    Events       string `xorm:"TEXT"`    *HookEvent   `xorm:"-"`    IsSSL        bool `xorm:"is_ssl"`    IsActive     bool    HookTaskType HookTaskType    Meta         string     `xorm:"TEXT"` // store hook-specific attributes    LastStatus   HookStatus // Last delivery status    Created      time.Time  `xorm:"CREATED"`    Updated      time.Time  `xorm:"UPDATED"`}

Variable Naming conventions

Variable names generally follow the Hump method, but when you encounter a particular noun, you need to follow these rules:

    • If the variable is private and the noun is the first word, use lowercase, as in apiClient .
    • Other circumstances should use the original wording of the noun, such as,, APIClient repoID UserID .

Some common nouns are listed below:

// A GonicMapper that contains a list of common initialisms taken from golang/lintvar LintGonicMapper = GonicMapper{    "API":   true,    "ASCII": true,    "CPU":   true,    "CSS":   true,    "DNS":   true,    "EOF":   true,    "GUID":  true,    "HTML":  true,    "HTTP":  true,    "HTTPS": true,    "ID":    true,    "IP":    true,    "JSON":  true,    "LHS":   true,    "QPS":   true,    "RAM":   true,    "RHS":   true,    "RPC":   true,    "SLA":   true,    "SMTP":  true,    "SSH":   true,    "TLS":   true,    "TTL":   true,    "UI":    true,    "UID":   true,    "UUID":  true,    "URI":   true,    "URL":   true,    "UTF8":  true,    "VM":    true,    "XML":   true,    "XSRF":  true,    "XSS":   true,}
Image
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.