This example summarizes the go Language basic data type. Share to everyone for your reference. Specifically as follows:
1, comments (as with C + +)
Line Comment://Block Comment:/* ...
2, identifiers
It can be said that, in addition to the beginning of the number is not allowed, the beginning of the symbol is not allowed, the keyword is not allowed, other combinations of Unicode characters can be. "_33" can also be an identifier, "we" or an identifier. Identifiers are also case-sensitive.
(1) An identifier that begins with an uppercase letter is public. (This is very interesting)
(2), any other identifiers are private.
(3), the null identifier "_" is a placeholder for assignment operations, discard, ignore a value. This is usually done in this way:
The Go method typically returns 2 values, a normal return, and an error identifier. such as FMT. PRINTLN (x) returns 2 values, one is the number of bytes printed, one is the corresponding error value, then Count,_ = FMT. PRINTLN (x) This line of code ignores the corresponding error value.
3, constant--with the Const keyword declaration
(1) The type can be inferred automatically,
Such as:
Copy Code code as follows:
(2) You can specify the type explicitly,
Such as:
Copy Code code as follows:
Const a int16 = 6//int16 is an orthopedic type
(Ps:go language constants, variable definitions are in the following formats:
Keyword constant (variable) name type = value)
(3) You can declare multiple constants at a time,
such as: const (a = 0; b = 2), this is called a group declaration. At this point, the first constant is set to 0, and subsequent constants are set by default to the expression of the previous constant.
(PS: But want to use const (a B) to indicate a=0,b=0 such is not OK, a constant grouping definition, the first constant must be assigned, or iota)
(PPS: Note that the go language generally does not need a semicolon to represent the separation, the compiler automatically adds a semicolon at the end of each line, and of course, adding a semicolon is not wrong, but an IDE such as Liteide will usually help you remove the semicolon and then wrap it for you).
(4) The keyword iota represents a continuous, untyped integer constant, iota defaults to 0, and increments incrementally.
That
Copy Code code as follows:
Both B and C are iota at this time, so a is 0,b for 1,c 2.
(5) In a row of multiple assignments, the Iota does not affect each other. Each time the iota appears, its value is 0, such as:
Copy Code code as follows:
Const (
I, j, k = 2 * iota, iota, Iota + 2
M, N, L
)
At this point, the values of I and J are 0,k values of 2,m, N, L are 2, 1, 3, respectively.
(Ps:go language supports multiple values in one row)
4, variable
There are 2 ways to define a variable:
(1) One is to use the keyword VAR,
For example:
Copy Code code as follows:
var i int//This will automatically set the default value of 0, if it is a string, the default is null
Or:
Copy Code code as follows:
var i = 8//declaration is assigned at the same time, and its type is deduced automatically
Or:
Copy Code code as follows:
var k int = 16//indicating type, declaring and assigning value
Or:
Copy Code code as follows:
var (a int; b int; c int)//group declaration, similar to constant.
(2) The other is a quick variable declaration, which uses the: = operator, which declares and initializes a variable that automatically infers the type. However, this declaration has a limit, it can only be used inside the function, outside the function will be an error.
Such as:
Copy Code code as follows:
Name: = "Chandler Qian"//Automatically inferred type is string
It is worth noting that: = operator is declared and initialized, that is, in the same scope, this variable must not be declared, or it is an error. As follows:
Copy Code code as follows:
K, B: = 7, 8
Fmt. Printf ("Before k=%d,b=%d\n", K, B)
If k: = 1; K!=-1 {
B: = 3
Fmt. Printf ("Inner k=%d,b=%d\n", K, B)
}
Fmt. Printf ("After k=%d,b=%d\n", K, B)
In the following if action, K, B re-use: = Declare assignment, but no problem, because the if scope, they disappeared.
The results are:
Before k=7,b=8
Inner k=1,b=3
After k=7,b=8
And the If statement changes to this:
Copy Code code as follows:
if k = 1; K!=-1 {
B: = 3
Fmt. Printf ("Inner k=%d,b=%d\n", K, B)
}
So the final output is:
Before k=7,b=8
Inner k=1,b=3
After k=1,b=8
Visible, "=" is an assignment that is global, and, ": =" creates a variable and assigns a value within its scope.
(3) The type of the shape literal is automatically inferred as int, and the floating-point literal is automatically inferred as float64, and the plural literal is automatically inferred as complex128
5, Boolean
The go language will strictly filter the values that are compared using the comparison operator (<, <=, = =,!=, >=, >). The two values must be of the same type or implement the same interface. Such as:
Copy Code code as follows:
Func test0 () {
var a int = 5
var b float32 = 4.4
If a > B {
Fmt. Println (">")
}
}
Because the A and B types do not match, compile errors: Invalid operation:a > B (mismatched types int and float32), look at the following:
Copy Code code as follows:
Func test1 () {
var b float32 = 4.4
If B > 7 {
Fmt. Println (">")
}
}
This is possible, although the type does not match, but B is compared to the type-free integer value constant 7. Numeric constants of no type can be used for any comparison.
I hope this article will help you with your go language program.