This is a creation in Article, where the information may have evolved or changed.
1. Qualifying identifiers
The qualified identifier is an identifier qualified with the package name prefix. Both the package name and the identifier cannot be blank. A qualified identifier is used to access an identifier in another package and it must be imported. The identifier must be exported and declared in the package block of the package.
Math. sin//represents the Sin function in the math package
2. Function literals
A function literal can be assigned to a variable or called directly.
F: = func (x, y int) int {return x + y}func (ch chan int) {ch <-ACK} (Replychan)
The function literal of closures: they can refer to variables defined in a perimeter function. Those variables are shared between the perimeter function and the function literal, and will continue to exist as long as they are accessible.
3. Selector.
For the primary expression x, which is not a package name, the selector expression is,
X.f
-
- For a non-interface type
T
or *T
value x
, the x.f
f
T
field or method in the lightest depth is represented in. If there is not only one f
, the selector expression is illegal.
- For a variable of an interface type
I
x
, the x.f
real method that represents the x
name of the given value f
. If I
there is no method named in the method set f
, the selector is illegal.
- In other cases, all
x.f
are illegal.
- In the case of
x
a pointer or interface type and the value is nil
, the x.f
assignment, evaluation, or invocation produces a run-time panic.
Selectors automatically dereference pointers to structures. If x is a pointer to a structure, X.Y is the abbreviation for (*x). y; If the field Y is also a pointer to the structure, X.Y.Z is the abbreviation for (* (*x). Y). Z, and so on. If X contains an anonymous field of type *a, and A is also a struct type, X.F is the abbreviation for (*X.A). F.
P.z //(*p). Zp.y //((*P). T1). yp.x //(* (*P). T0). XP. M2 () //(*P). M2 () p.m1 () //((*P). T1). M1 () p.m0 () //((*P). T0). M0 ()
4. Subscript expression
Form: A[x]
5. Slicing
For arrays or strings, if subscript 0 <= low
<= high
<= len(a)
low
and high
that is within the bounds , otherwise 界外
. For slices, the upper bound is the capacity of the slice, cap(a)
not the length. Constant subscript must be non-negative and can be represented as a int
value of type. If the subscript is also constant, they must be satisfied low <= high
. a
nil
A run-time panic is raised if the subscript or its subscripts are out of bounds at run time.
A[low:high]
6. Type Assertion
For an interface type of expression x with type T, the primary expression x. (T), Note that x must be an interface type
var x interface{} = 7 //x has a dynamic type int with a value of 7i: = x. (int) //I have a type int with a value of 7type I interface {m ()}var y is: = y. (St Ring) //illegal: string does not implement I (missing method m) r: = Y. (io. Reader) //R has type IO. Reader and Y must implement both I and IO at the same time. Reader
Rather, if T
it is a non-interface type, the x.(T)
assertion x
is the same as the dynamic type T
. In this case, T
x
The (interface) type must be implemented unless its type assertion is invalid because it cannot be a x
value for the storage type T
. T
for an interface type, the x.(T)
asserted x
dynamic type implements the interface T
.
If the type assertion is true, the value of the expression is stored in x
the value, and its type is T
. If the type assertion is not true, a run-time panic occurs. In other words, even though x
the dynamic type can only be seen at runtime, the type in the correct program is known x.(T)
as T
.
If the type assertion
V, OK = x. (t) v, OK: = x. (t) var V, OK = x. (t)
The expression returns a value pair (x. (T), true) if the assertion is valid; otherwise, the expression returns (Z, false), where z is the 0 value of type T. This situation does not generate a run-time panic. Type assertion in this construct, its behavior is similar to a function call that returns a value with a Boolean value to indicate success.
7. Comparison operators
In any comparison, the first operand must be a type that can be assigned a second operand, and vice versa.
Equality operators ==
and !=
applies to comparable operands. Sequential operators <
, <=
>
and >=
applicable to ordered operands. The relationships and values for these comparison operations are defined as follows:
-
- Can be compared between Boolean values. If the two Boolean values are the same
true
or the same false
, they are equal.
- Typically, integer values can be compared or sorted.
- Depending on the definition of the IEEE-754 standard, floating-point values can be compared or sorted.
- Can be compared between complex values. For two complex values
u
v
, if and real(u) == real(v)
imag(u) == imag(v)
, they are equal.
- String values can be compared or sorted, depending on the lexical character of the byte.
- Pointer values can be compared. If two pointers point to the same values or their values are the same
nil
, they are equal. Pointers to variables that are obviously 0 size may or may not be equal.
- Channel values can be compared. If two channel values are
make
created or are the same as values through the same invocation (§ Create slices, mappings, and Channels) nil
, they are equal.
- Interface values can be compared. If two interface values have the same dynamic type and equal dynamic values, or the same
nil
values, they are equal.
- When the value of a non-interface type is comparable and implemented, the value of the
X
X
T
non-interface type is comparable to the X
x
values of the interface type T
t
. If t
the dynamic types are the same and the X
t
dynamic values are equal x
, they are equal.
- If all the fields of the two structure values are comparable, they can be compared. If their corresponding non-blank fields are equal, they are equal.
- If the values of the two array element types are comparable, the array values can be compared. If the corresponding elements are equal, they are equal.
8. Address operator
For an operand of type T x, the address operator &x generates a pointer of type *t to X. For an operand x with a pointer type of *t, the indirect pointer *x represents a value of type T pointing to X. If X is nil, attempting to evaluate *x will trigger a run-time panic.
&x&a[f (2)]&point{2, 3}*P*PF (x)
9. Receive operator
V1: = <-chv2 = <-CHF (<-ch) <-strobe //waits before the clock pulse and drops the received value
X, OK = <-chx, OK: = <-chvar x, OK = <-ch
The value of OK is true if the received value is emitted by a successful operation to the channel, or False if the value received is a value of 0 due to the channel being closed or empty.
10. Type Conversion
Type conversions are expressions of the form T (x), where T is a type, and X is an expression that can be converted to type T.
If the type starts with an operator *
, <-
or a keyword, func
you must add parentheses:
*point (P) //equivalent to * (Point (P)) (*point) (p) //P is converted to (*point) <-chan Int (c) //equivalent to <-(chan Int (c)) (<- Chan int) (c) //C is converted to (<-chan int) func () (x) //Function signature Func () x (func ()) (x) //X is converted to (func ())