Home  >  Article  >  Backend Development  >  How many predefined identifiers are there in Go language?

How many predefined identifiers are there in Go language?

青灯夜游
青灯夜游Original
2023-01-31 17:33:474211browse

There are 36 predefined identifiers in the Go language, which mainly include basic data types and built-in functions, namely: append, bool, byte, cap, close, complex, complex64, complex128, uint16, copy, FALSE , float32, float64, imag, int, iota, len, make, new, nil, panic, real, recover, TRUE, uint, etc.

How many predefined identifiers are there in Go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Predefined identifiers in the go language

Identifiers refer to the names used by the Go language to name various variables, methods, functions, etc. A character sequence, the identifier consists of several letters, underscore_, and numbers, and the first character must be a letter. In layman's terms, any name that can be defined by yourself can be called an identifier.

In the Go language, in addition to user-defined identifiers, there are also some special identifiers called predefined identifiers, as shown in the following table:

append bool byte cap close complex complex64 complex128 uint16
copy false float32 float64 imag int int8 int16 uint32
int32 int64 iota len make new nil panic uint64
print println real recover string true uint uint8 uintptr

There are 36 predefined identifiers in total, which mainly include basic data types and built-in functions in the Go language. These predefined identifiers cannot be used as identifiers.

Extended knowledge:

User-defined identifier

Composition of identifiers

  • Identifiers consist of numbers, letters and underscores (_). 123 abc _

  • can only start with a letter and an underscore (_). abc123 _sysVar 123abc

  • Identifiers are case-sensitive. name Name NAME

Examples of identifier naming

Correct naming

package main

func main() {
    var name string
    var age int
    var _sys int
}

Wrong identifier

package main

import "fmt"

func main() {
        var 1name string
        var &age int
        var !email
}

go language naming convention

Go is a case-sensitive language

Naming rules involve the naming of variables, constants, global functions, structures, interfaces, methods, etc. The Go language has the following restrictions from a grammatical level: any name that needs to be exposed must start with a capital letter, and any name that does not need to be exposed should start with a lowercase letter.

When the name (including constants, variables, types, function names, structure fields, etc.) starts with a capital letter, such as: GetUserName, then the object using this form of identifier can be externally packaged The code used (the client program needs to import this package first), this is called an export (like public in object-oriented languages); if the name starts with a lowercase letter, it is not visible outside the package, but they are used throughout the package The internals are visible and available (like private in object-oriented languages)

Package name

Keep the name of the package consistent with the directory, and try to use something meaningful The package name should be short, meaningful, and try not to conflict with the standard library. Package names should be lowercase words and do not use underscores or mixed case.

package dao
package service

File naming

Try to use meaningful file names that are short and meaningful. They should be lowercase words and use underscores to separate each word.

customer_dao.go

Structure naming

Use camel case naming method, the first letter is uppercase or lowercase according to access control

struct declaration and initialization format uses multiple lines, For example:

type CustomerOrder struct {
    Name string 
    Address string
}
order := CustomerOrder{"tom", "北京海淀"}

Interface naming

Basic naming rules and the above structure type

The structure name of a single function has "er" as the suffix , such as Reader, Writer.

type Reader interface {
     Read(p []byte) (n int, err error)
}

Variable naming

Similar to structures, variable names generally follow the camel case method, and the first letter is uppercase or lowercase according to the access control principle, but when encountering unique nouns, The following rules need to be followed:

If the variable is private and the unique noun is the first word, use lowercase, such as appService. If the variable type is bool type, the name should start with Has, Is, Can or Allow

var isExist bool
var hasConflict bool
var canManage bool
var allowGitHook bool

Constant naming

Constants must be composed of all capital letters, and use underscores to separate words

const APP_URL = "https://www.duoke360.com"

If it is an enumeration type constant, it needs to be created first Corresponding type:

type Scheme string

const (
    HTTP  Scheme = "http"
    HTTPS Scheme = "https"
)

Error handling

The principle of error handling is that any call that returns err cannot be discarded. Do not use _ discard, all must be processed. When receiving an error, either return err, or use log to record it and return as soon as possible: once an error occurs, return immediately. Try not to use panic unless you know what you are doing. If the error description is in English, it must be lowercase and does not require punctuation at the end. , use an independent error stream for processing

// 错误写法
if err != nil {
    // 错误处理
} else {
    // 正常代码
}

// 正确写法
if err != nil {
    // 错误处理
    return // 或者继续
}
// 正常代码

Unit test

The unit test file name naming convention is example_test.go. The function name of the test case must start with Test, for example : TestExample Every important function must first write a test case. The test case and the regular code are submitted together to facilitate regression testing.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of How many predefined identifiers are there in Go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn