This is a creation in Article, where the information may have evolved or changed.
Catalogue [−]
- Label Scope
- Null identifier and pre-defined identifier
- Output identifiers
- Iota
- function declaration
- Method declaration
This article describes the declaration and scope of Go.
Declarations are used for constants, variables, types, functions, labels, and packages.
Each non-null identifier (identifier) must be declared. Identifiers in the same code block can only be declared once. Identifiers cannot be declared both in a file code block and in a package code block.
The empty identifier is an underscore "_", which can be used as an anonymous placeholder.
The scope of a declared identifier is summarized as follows:
- The scope of a pre-declared identifier is a global code block such as
int , and true so on
- The identifier of the top layer (outside the function) that declares a constant, type, variable, or function (non-method) that is scoped to the package code block
- The scope of the package name of an input package is the file code block of this file
- The scope of the identifier used to represent the method receiver, function parameter, and result variable is the function body
- The scope of a constant or variable declared within a function is scoped after Constspec, Varspec (that is, after the identifier declaration), ending at the end of the code block containing its most inner layer
- The type identifier declared within a function starts at TypeSpec and ends at the end of the code block that contains its most inner layer
Identifiers in code blocks can be declared again in the internal code block, but the internal identifiers and external identifiers represent different objects, which must be careful.
Package clause is not a statement statement. The package name cannot appear in any scope , often we declare a variable for convenience, like the package name, such as when the package is introduced to net/http declare a variable is http , this is not the case, this will cause the variable name will be hidden (shadow) package name.
Label Scope
Label scopes can be used for, break continue and goto statements, which are also defined in other languages, although they are not recommended for widespread use.
1234567 |
I: = 0 back: i++fmt. Println (i)if i < 5 { goto Back} |
It is illegal to define an unused label, which is the same as a variable within a function that is not used.
tags do not conflict with other identifiers with the same name.
The scope of a label is only at the end of the function body where it is declared, and it does not function as an inner nesting.
Null identifier and pre-defined identifier
The null identifier is mentioned earlier, and the following is an application of some empty identifiers.
123456789 |
Import "Net/http") Const "Hello World" var _ = type _ []inttypestruct{}type Interface{} |
The predefined identifiers are described in the first chapter.
Output identifiers
There are no public, protect, or private keywords in the go language, and if you want to access the identifiers of the current package in other packages,
Identifiers should have the following two conditions, which are not the same as other programming languages:
1. The first character of the identifier name should be Unicode upper case letter (Lu)
2, and the identifier is declared in the package code block, or it is a field name or method name.
Let me give you an example.
Create a directory p in the same directory as the current main program, create a file t.go in it, and define a variable of two package scopes:
1234 |
Package Pvar' Hello World 'var' Hello World ' |
Then in the main program to try to access the package p under the two variables, you will find only access Str , str error, said no export:
123456789101112 |
Package mainimport ("FMT""./P")func Main () {FMT. Println (P.STR) fmt. Println (P.STR)} |
Functions, constants, types, structs, interface, methods all follow this rule. So if you want to export an identifier, capitalize the first letter.
Note that the first letter that is required here is the Unicode upper case letters, which are Unicode classified as LU characters, and for Unicode characters, you need to know which uppercase characters, such as the Greek character is also uppercase and lowercase, Chinese is not case:
12 |
var "123" //Not output var π= 3.1415926//Output |
Here you can see a list of Unicode uppercase characters.
Iota
In a constant definition,iota represents a continuous integer constant of unspecified type. Whenever the const character of a reserved word appears, it is reset to 0, and each subsequent constant definition will add one. It is often used to define a set of types that resemble enumerations, such as month, week, color, and so on.
1234567891011121314151617181920 |
Const(//Iota is reset to 0C0 =Iota //C0 = = 0C1 =Iota //C1 = = 1C2 =Iota //C2 = = 2)Const(//Iota reset to 0A =1<<Iota //A = = 1b =1<<Iota //b = = 2c =3 //C = = 3 (Iota Although not used, but will increase)D =1<<Iota //d = = 8)Const(//Iota reset to 0U =Iota* the //U = = 0 (untyped integer constant)Vfloat64=Iota* the //V = = 42.0 (float64 constant)W =Iota* the //W = = (untyped integer constant))Constx =Iota //X = = 0 (iota reset to 0)Consty =Iota //Y = = 0 (iota reset to 0) |
If you use an expression list, the value of the iota of the same expression is the same, because only new constspec are encountered to increase.
123456 |
Const (bit0, mask0 = 1Iota, 1<<Iota - 1 //Bit0 = = 1, Mask0 = = 0, iota = 0bit1, mask1 //Bit1 = = 2, Mask1 = 1, Iota = 1_, _ // Skips iota = = 2bit3, MASK3 //bit3 = 8, Mask3 = 7, iota =3) |
There is no enum type in the Go language, so the enumeration type is typically defined in the following way:
1234567891011121314151617181920 |
type int var names = [...] string {"Monday""Tuesday""Wednesday""Thursday""Friday""Saturday" "Sunday"} Const Iota + 1tuesdaywednesdaythursdayfridaysaturdaysunday)funcstring { if w > 0 && W < 8 {return names[w-1]}return
"Illegal Week name"} |
function declaration
A function can be declared as a signature, or a method body can be defined.
A function without a method body can only be declared in an interface, or in a package code block, when the method is externally implemented, such as assembly language.
If the function declares a return type, the method body must have a corresponding return statement.
An anonymous function is also called a function literal, which does not contain a function name and can be used to assign a value to a variable (for example, to a variable within or outside a function), or to a direct invocation (such as a Go statement).
123 |
func int int return x + y}funcchanint) {ch <-ACK} (Replychan)gofunc Chan int) {ch <-ACK} (Replychan) |
Method declaration
A method declares a function declaration, but it contains a recipient receiver.
In front of the method name to declare an additional parameter, this parameter is single, immutable, as receiver,
It is of type T or *T , T is called receiver base type.
Note T It cannot be a pointer type or a bitter type, and must be declared under the same package as the method. You want to define a method for a type in a standard library under your own package.
The method name is displayed only in the selector of type T or *t.
Receiver name cannot be the same as parameter name or return parameter:
1234 |
//Error func int int) {return 0} |
Method name is unique.
For a struct type, the method name and field name must be unique.
The properties and selector of the method are described in the next chapter.