This is a creation in Article, where the information may have evolved or changed.
The following main description of the Go Language program structure.
I. Definition specification of Name
This includes functions, variables, constants, types, statement labels, and the name of the package must begin with a letter or underscore, followed by any number of characters, numbers, and underscores, in Golang, where the name is case-sensitive, such as:
var age int8 and Var age int8 are two different variables, and the keywords and built-in constants, types, and functions in Golang cannot be used as names.
(1) Keywords:
- var and const: Declarations of variables and constants
- var varName type or VarName: = value
- Package and import: Importing
- Func: Used to define functions and methods
- Return: For returning from a function
- Defer Somecode: Executes before the function exits
- Go: for Parallel
- Select for different types of communication
- Interface for defining interfaces
- struct is used to define abstract data types
- Break, case, continue, for, Fallthrough, else, if, switch, goto, default Process Control
- Chan for Channel Communications
- Type is used to declare a custom type
- Map for declaring map type data
- Range for reading slice, map, channel data
(2) Built-in constants, types, and functions
Constant: true false iota Nil
Type: int int8 int16 int32 int64 uint uint8 uint16 uint32 UInt64 uintptr
float32 float64 complex128 complex64 bool byte Rune string error
function: Make Len cap new append copy close delete panic recover complex real imag
Golang's name is styled in the camel's style, similar to the language of Java.
Second, the variable
The usual way to define a variable is var name type = expression, where expression of type can omit one, such as:
var userName string
var userName = "Zhangsan"
1. Short variables
Typically used in function bodies, primarily for declaring and initializing local variables, defining the format: Name : = expression,name type has expression to determine, such as:
UserName: = "Zhangsan"//This userName is a character type
I: = 2//I on the side of this is an int type
Both a short variable and a generic variable support declaring multiple at once, such as:
UserName, I: = "Zhangsan", 2
var userName, i = "Zhangsan", 2
2. Pointers
The value of a pointer is the address of a variable, and a pointer indicates where the value is saved, such as:
var x int
Then the &x expression is a pointer to an integer variable whose type is an integer pointer (*int), if the value of &x is called P, then p is the address that contains X, the variable to which P points is written *p, and the expression *p is the value of the get variable.
Example:
1 var p *int = &xfmt. Println (*p) // output Here is 12// equivalent to x=2FMT. PRINTLN (x) // result is 2
3. New function
Another way to create a variable is to use the new function, format: New (t), t to represent the type, which represents the creation of a variable of type T, initialized to a 0 value of type T, and returns its address, the *t type, such as:
P: = new (int)//p is of type *int with an initial value of 0
Iii. Scope of variables
Use examples to illustrate
Package Shopimport ("FMT")varGlobalint;//A variable that is declared outside the body of a function and that starts with a capital letter belongs to a global variablevarJint;//functions are declared in vitro, and lowercase letters start with package-level variables//The lowercase letter begins with the function number of the packet-levelFunc Shopping () {k:=1 //variables in the function bodyFmt. Print (" Shop") for{m:=1 //The variables within the block are used only in this program block, which refers to a pair of {}FMT. Println (K,m)}}//the uppercase Start function is at the global levelfunc goshopping () {}