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.
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 |
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
1 2 3 4 5 6 7 |
|
Wrong identifier
1 2 3 4 5 6 7 8 9 |
|
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.
1 2 |
|
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.
1 |
|
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:
1 2 3 4 5 |
|
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.
1 2 3 |
|
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
1 2 3 4 |
|
Constant naming
Constants must be composed of all capital letters, and use underscores to separate words
1 |
|
If it is an enumeration type constant, it needs to be created first Corresponding type:
1 2 3 4 5 6 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
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!