This is the 4th day of my participation in the August More Text Challenge

Directory:

  • 01. Why do you choose Go
  • 【 hand by hand teach you to write 】02
  • 03. Basic data types
  • 04. Operators and flow control
  • 05. Function
  • 06. Project management
  • 07. Compound types – Pointers
  • 08. Compound types – arrays and slices
  • 08. Complex types -map
  • 09. Complex types – Structures
  • 10. Object-oriented programming
  • 11. Exception handling
  • 12. Text processing
  • 13. Concurrent programming – concurrent entry

3. The operator

3.1 Arithmetic operators

The operator The term The sample The results of
+ add 10 + 5 15
Reduction of Ten to five 5
* take 10 5 50
/ In addition to 10/5 2
% Mod (mod) 10% 3 1
++ Rear increment, no front increment a=0; a++ a=1
Post-subtraction, no pre-subtraction a=2; a– a=1

 

3.2 Relational operators

The operator The term The sample The results of
= = Equivalent to 4 = = 3 false
! = Is not equal to 4 != 3 true
< Less than 4 < 3 false
> Is greater than 4 > 3 true
< = Less than or equal to 4 < = 3 false
> = Greater than or equal to 4 > = 1 true

3.3 Logical operators

The operator The term The sample The results of
! non ! a If a is false, then! A is true; If a is true, then! A false.
&& with a && b The result is true if both A and B are true, false otherwise.
| | or a b

 

3.4 bit operators

The operator The term instructions The sample
& Bitwise and The binary phase of each of the two numbers involved in the operation 60 and 13 is 12
| Bitwise or The binary phase or of each of the two numbers involved in the operation 60
^ Exclusive or The result is 1 when the corresponding binary digits of the two numbers involved in the operation are different or when the corresponding binary digits are different 60 to the 13th is 240
<< Shift to the left To the left n is 2 to the n. Discard the left and add a 0 to the right. 4 << 2 结果为16
>> Moves to the right If you move to the right n places, you divide by 2 to the n. Discard the right and fill in the left. 4 >> 2 The result is 1

 

3.5 Assignment operators

The operator instructions The sample
= Ordinary assignment C = a + b Assigns the result of the a + b expression to C
+ = Add them and assign them C plus a is the same thing as c is equal to c plus a
– = Subtract and assign C minus a is the same thing as c minus a
= Multiply and assign C equals a is the same thing as c equals c times a
/ = Divide and assign C over a is the same thing as c over A
% = Remainder and then assign C %= a is the same thing as c = c % A
< < = Assign after left shift C <<= 2 is equivalent to c = c << 2
> > = Assign after right shift C >>= 2 is equivalent to c = c >> 2
& = Assignment by bit and post C squared is the same thing as c squared
^ = Xor post assignment by bit C ^ 2 is the same thing as c ^ 2
= Assignment by bit or post

 

3.6 Other Operators

The operator The term The sample instructions
& Fetch the address operator &a The address of variable A
Value operator a The value of the memory to which the pointer variable A points

 

3.7 Operator priority

In Go, unary operators have the highest precedence, and binary operators operate from left to right.

The following table lists all the operators and their priorities, from top to bottom representing the highest to lowest priority:

priority The operator
7 ^!
6 *       /    %    <<    >>    &      &^
5 +      –     
4 = =! = < <= >= >
3 <-
2 &&
1 | |

4. Process control

Go supports three basic program execution constructs: sequential constructs, selection constructs, and loop constructs.

  • Sequential structure: the program is executed sequentially without skipping.
  • Selection structure: according to whether the conditions are met, the corresponding functions are selectively performed.
  • Loop structure: Executes a piece of code multiple times depending on whether the condition is met.

4.1 Selecting a Structure

4.4.1 if statement

4.1.1.1 the if

Var a int =3 if a==3 {// Conditional expression without parentheses FMT.Println("a==3")} b == 3 { fmt.Println("b==3") }Copy the code

4.1.1.2 the if… else

if a := 3; A ==4 {fmt.Println("a==4")} else {// Open curly braces must be on the same line as conditional statements or else fmt.Println("a! = 4 ")}Copy the code

4.1.1.3 the if… else if … else

if a := 3; a > 3 {
    fmt.Println("a>3")
} else if a < 3 {
    fmt.Println("a<3")
} else if a == 3 {
    fmt.Println("a==3")
} else {
    fmt.Println("error")
}
Copy the code

4.1.2 switch statement

In Go, switch is equivalent to a break at the end of each case by default. After the match is successful, other cases will not be automatically executed, but the whole switch will jump out. However, fallthrough can be used to enforce the following case code:

Var score int = 90 switch score {case 90: FMT.Println(" good ") //fallthrough case 80: Println(" good ") // FallThrough default: Println(" bad ")} // FallThrough default: Println(" bad ")}Copy the code

You can use any type or expression as a conditional statement:

//1 switch s1 := 90; S1 {// initialize statement; Case 80: Println(" good ") default: Println(" good ")} //2 var s2 int = 90 switch {// Println(" good ") default: Println(" good ")} //3 switch s3 := 90; Println(" excellent ") case s3 >= 80: Println(" good ") default: FMT.Println(" general ")}Copy the code

4.2 Loop Statements

2 the for

For initialization conditions; Judgment condition; Var I, sum int //1) initialize condition I := 1 //2) check whether condition is true, I <= 100, if true, execute loop, if false, exit loop //3) condition change I ++ //4) repeat 2, 3, 4

for i = 1; i <= 100; i++ {
    sum += i
}
fmt.Println("sum = ", sum)
Copy the code

4.2.2 range

The range keyword returns two values, the first being the element’s array index and the second the element’s value:

S := "ABC" for I := range s {string/array/slice/map is supported. FMT. Printf (" % \ n "c, s [I])} for _, c: the range of = s {/ / ignore index FMT) Printf (" % \ n" c, c)} for I, Printf("%d, %c\n", I, c)} for I := range s {fmt.Printf("%d, %c\n", I, c)} for I := range STR { Printf(" STR [%d]=%c\n", I, STR [I])} for I, _ := range STR {// The second value, discarded by default, Returns the position of the element (subscript) FMT. Printf (" STR [% d] = % c \ n ", I, STR [I])}Copy the code

4.3 Jump Statement

This will break and continue

There are two key operations inside the loop, break and continue. Break breaks the current loop and continue skips the current loop.

for i := 0; i < 5; I ++ {if 2 == I {//break //break to break the current loop continue //continue to skip the current loop} fmt.println (I)}Copy the code

Note: Break can be used for, switch, SELECT, and continue can only be used for loops.

4.3.2 goto

Use goto to jump to tags that must be defined within the current function:

func main() { for i := 0; i < 5; I ++ {for {fmt.Println(I) goto LABEL}} fmt.Println("this is test") LABEL: fmt.Println("it is over") }Copy the code