An article takes you to understand the basic operators and process control of go language

Time:2021-11-30

An article takes you to understand the basic operators and process control of go language

preface

Hello, everyone. I’m on August. This time, let’s continue to learn the basics of gooperatorandProcess controlBravo.

operator

The name operator sounds mysterious. In fact, we usually see a lot, such as =, +, / and so on.

  • Operators are mainly divided into
  • Arithmetic operator
  • Relational operator
  • Logical operator
  • Bitwise Operators
  • Assignment Operators

Let’s briefly understand the following ba.

Arithmetic operator

As the name suggests, arithmetic operators are mainly used in arithmetic, addition, subtraction, multiplication and division

Simply understand the following.

operator describe
+ Add
subtract
* Multiply
/ be divided by
% Seeking remainder
++ Self addition

Relational operator

 

operator describe
== Check whether the two values are equal. If they are equal, return true; otherwise, return false
!= Check whether the two values are not equal. If not, return true; otherwise, return false
> Check whether the left value is greater than the right value. If yes, return true; otherwise, return false
>= Check whether the left value is greater than or equal to the right value. If yes, return true; otherwise, return false
< Check whether the left value is less than the right value. If yes, return true; otherwise, return false
<= Check whether the left value is less than or equal to the right value. If yes, return true; otherwise, return false
 

 

Logical operator

operator describe
&& Logical and operator. True if both operands are true, otherwise false
|| Logical or operator. True if both operands have a true, otherwise false
! Logical not operator. False if the condition is true, otherwise true

Note: logical operators are very important and are often used in development.

Bitwise Operators

Bit operators have some bottom layers, which will be used in special cases

operator describe
& The binary phase and corresponding to each of the two numbers involved in the operation. (only when both digits are 1 is 1)
| The binary phase or corresponding to each of the two numbers involved in the operation. (if one of the two is 1, it will be 1)
^ The corresponding binaries of the two numbers involved in the operation are different or, when the corresponding binaries are different, the result is 1. (1 if two digits are different)
<< Shifting n bits to the left is multiplied by 2 to the nth power. “a<<>< span=””><>
>> Shifting n bits to the right is divided by 2 to the nth power. “A > > b” shifts all binary bits of a to the right by b bits.

Assignment Operators

Assignment operators are also used more in development.

operator describe
= A simple assignment operator assigns the value of an expression to an lvalue
+= Add and assign
-= Subtraction before assignment
*= Multiply and assign
/= Divide and assign
%= Assign value after remainder
<<= Left shift assignment
>>= Assignment after shift right
&= Bitwise and post assignment
|= Bitwise or post assignment
^= Assignment after bitwise XOR

Process control

In go, there are the following types of process control.

  • if

    • if
    • switch case
  • for

    • Standard for
    • forrange
  • Goto (use with caution)

if

grammar

  1. //Mode one, an if
  2. If condition{
  3. //Execute statement
  4. }
  5. //Method 2: if the condition fails to execute else
  6. If condition{
  7. //If successful statement
  8. }else{
  9. //If unsuccessful statement
  10. }
  11. //Mode 3, with else if
  12. If condition 1{
  13. //If successful statement
  14. }Else if condition 2{
  15. //If unsuccessful, continue the else if condition
  16. }else{
  17. //None of the above is successful. Execute else
  18. }

example

  1. packagemain
  2.  
  3. import”fmt”
  4.  
  5. funcmain(){
  6. Vargender = “male”
  7.  
  8. Ifgender = = “male”{
  9. Fmt.println (“male”)
  10. }Elseifgender = = “female”{
  11. Fmt.println (“female”)
  12. }else{
  13. FMT. Println (“nothing???”)
  14. }
  15. }

switch case

Switch case and if are essentially the same thing, but in some scenarios, switch is more concise than if.

code

  1. packagemain
  2.  
  3. import”fmt”
  4.  
  5. funcmain(){
  6. varweek=3
  7. switchweek{
  8. case1:
  9. Fmt.println (“Monday”)
  10. case2:
  11. Fmt.println (“Tuesday”)
  12. case3:
  13. Fmt.println (“Wednesday”)
  14. case4:
  15. Fmt.println (“Thursday”)
  16. case5:
  17. Fmt.println (“Friday”)
  18. case6:
  19. Fmt.println (“Saturday”)
  20. case7:
  21. Fmt.println (“Sunday”)
  22. Default: / / if none of the above is executed, default will be executed
  23. FMT. Println (“Saturday”)
  24. }
  25. }

Note: in go, switch has no case penetration.

Go’s switch case can have multiple values.

  1. packagemain
  2.  
  3. import”fmt”
  4.  
  5. funcmain(){
  6. varweek=3
  7. switchweek{
  8. case1,2,3,4,5:
  9. Fmt.println (“work”)
  10. case6:
  11. Fmt.println (“sleep in on Saturday”)
  12. case7:
  13. FMT. Println (“travel on Sunday”)
  14. default:
  15. FMT. Println (“flying???”)
  16. }
  17. }

for

Standard for loop

grammar

  1. For initial conditions; Judgment conditions; End condition{
  2. sentence
  3. }

code

  1. packagemain
  2.  
  3. import”fmt”
  4.  
  5. funcmain(){
  6. fori:=0;i<10;i++{
  7. fmt.Println(i)
  8. }
  9. }

Infinite loop

Execute carefully!!!

  1. packagemain
  2.  
  3. import”fmt”
  4.  
  5. funcmain(){
  6. for{
  7. fmt.Println(“666”)
  8. }
  9. }

for range

For ragne can easily loop arrays, slices, strings, maps, channels, etc.

Traversal array example

  1. packagemain
  2.  
  3. import”fmt”
  4.  
  5. funcmain(){
  6. varstudent_ List = […] string {“Zhang San”, “Li Si”, “Wang Wu”}
  7. forindex,v:=rangestudent_list{
  8. //Index is the subscript and V is the value of each
  9. fmt.Println(index,v)
  10. }
  11. }

Note:

  1. If array, slice, string
  2. Index is the subscript and V is the value
  3. If it’s a map
  4. Index is the key and V is the pair (value)
  5. If it’s a channel
  6. There is only one value, which is the value in the channel

Supplement:

In go, there is only a for loop and no while.

The for loop works in two ways.

goto

Goto can jump unconditionally between codes through tags, which creates a situation. If you use too many GOTEs, it will cause logical confusion and jump around.

Therefore, in the development, we should use it with caution and do not use it unless we have to.

Example

  1. packagemain
  2.  
  3. import”fmt”
  4.  
  5. funcmain(){
  6. fori:=0;i<10;i++{
  7. forj:=0;j<10;j++{
  8. ifj==2{
  9. //Jump directly to the breaktag tag tag below
  10. gotobreakTag
  11. }
  12. fmt.Printf(“%v-%v\n”,i,j)
  13. }
  14. }
  15. //Label to jump to
  16. breakTag:
  17. Fmt.println (“end for loop”)

summary

Above, we learned the basic operators and process control of go. If there is a task problem during the operation, remember to leave a message in the discussion area below, and we will solve the problem at the first time.

I’m Manon. If you think it’s good, remember to praise it. Thank you for watching.

Recommended Today

Game case | application evolution and practice of service mesh in happy games

author Chen Zhiwei, Tencent level 12 background expert engineer, is now responsible for the public background technology research and development and team management of happy game studio. Rich experience in micro service distributed architecture and game background operation and maintenance research and development. preface The background of happy game studio is a distributed micro service […]