This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Recently, I have been learning the go language principle and practice with the small volume. Because there is no go language foundation, I followed the rookie tutorial to learn the basic grammar of go language foundation – rookie tutorial. This is the first of two basic notes on go.

Download and install the GO interpreter

https://golang.org/dl/
Copy the code

1 hello world

hello.go

package main

import "fmt"

func main(a) {
	/* This is my first simple program */
	fmt.Println("Hello, World!")}Copy the code
  • To run the program
#Methods a
go run hello.go
#Way 2
go bulid hello.go
./hello.exe
Copy the code
  • A running program compiled to a different operating system
    • mac
    $ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build test.go
    $ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go
    Copy the code
    • linux
    $ CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build test.go
    $ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go
    Copy the code
    • windows
    $ SET CGO_ENABLED=0SET GOOS=darwin3 SET GOARCH=amd64 go build test.go
    $ SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build  test.go
    Copy the code

2 Data Types

package main

import "fmt"

func main(a) {
	var a bool = true       / / a Boolean
	var b int = 123         / / plastic
	var c float32 = 123.123 // 32-bit floating point type
	// var d byte = 1 // byte
	var ip *int  / / pointer
	var f string / / character
	ip = &b

	f = "Rookie Tutorial"
	fmt.Print(a, b, c, ip, f)
}

Copy the code

result

true 
123 
123.123 
0xc000012088Novice tutorialCopy the code

Three variables

  • variable
package main
import "fmt"
func main(a) {

    Declare a variable and initialize it
    var a = "RUNOOB"
    fmt.Println(a)

    // Zero if not initialized
    var b int
    fmt.Println(b)

    // bool Zero is false
    var c bool
    fmt.Println(c)
}
Copy the code

result

RUNOOB
0
false
Copy the code

4 constants

  • const
package main

import "fmt"

func main(a) {
   const LENGTH int = 10
   const WIDTH int = 5  
   var area int
   const a, b, c = 1.false."str" // Multiple assignments

   area = LENGTH * WIDTH
   fmt.Printf("面积为 : %d", area)
   println(a)println(a, b, c)  
}
Copy the code

result

Area: 50 1 false STRCopy the code
  • Built-in functions in constant expressions
package main

import "unsafe"
const (
    a = "abc"
    b = len(a)
    c = unsafe.Sizeof(a)
)

func main(a){
    println(a, b, c)
}
Copy the code

result

abc 3 16
Copy the code
  • Iota constant count
/ / instance
package main

import "fmt"

func main(a) {
    const (
            a = iota   / / 0
            b          / / 1
            c          / / 2
            d = "ha"   // ioTA += 1
            e          //"ha" iota += 1
            f = 100    //iota +=1
            g          //100 iota +=1
            h = iota   //7, restore count
            i          / / 8
    )
    fmt.Println(a,b,c,d,e,f,g,h,i)
}
Copy the code

result

0 1 2 ha ha 100 100 7 8
Copy the code

5 operators

  • Arithmetic operator
package main

import "fmt"

func main(a) {

   var a int = 21
   var b int = 10
   var c int

   c = a + b
   fmt.Printf("The value of the first line -c is %d\n", c )
   c = a - b
   fmt.Printf("The value of the second line -c is %d\n", c )
   c = a * b
   fmt.Printf("The value of the third line -c is %d\n", c )
   c = a / b
   fmt.Printf("Line 4 -c has the value %d\n", c )
   c = a % b
   fmt.Printf("The value of line 5 -c is %d\n", c )
   a++
   fmt.Printf("Line 6 - the value of a is %d\n", a )
   a=21   // For testing purposes, a is reassigned to 21
   a--
   fmt.Printf("Line 7 - the value of a is %d\n", a )
}
Copy the code

result

The value of -c in the first line is 31 in the second line is 11 in the third line is 210 in the fourth line is 2 in the fifth line is 1 in the sixth line is 22 in the seventh line is 20Copy the code
  • Comparison operator
package main

import "fmt"

func main(a) {
   var a int = 21
   var b int = 10

   if( a == b ) {
      fmt.Printf("The first row - a is equal to b\n")}else {
      fmt.Printf("First line - a does not equal b\n")}if ( a < b ) {
      fmt.Printf("Line 2 - A is less than b\n")}else {
      fmt.Printf("Second line - a is not less than b\n")}if ( a > b ) {
      fmt.Printf("Line 3 - A is greater than b\n")}else {
      fmt.Printf("Line 3 - A is not greater than b\n")}/* Lets change value of a and b */
   a = 5
   b = 20
   if ( a <= b ) {
      fmt.Printf("Line 4 - a is less than or equal to b\n")}if ( b >= a ) {
      fmt.Printf("Row 5 - b is greater than or equal to a\n")}}Copy the code

result

Row 1 minus A is not equal to b row 2 minus A is not less than b row 3 minus A is greater than b row 4 minus A is less than or equal to b row 5 minus b is greater than or equal to aCopy the code
  • Logical operator
package main

import "fmt"

func main(a) {
   var a bool = true
   var b bool = false
   if ( a && b ) {
      fmt.Printf("First line - condition true\n")}if ( a || b ) {
      fmt.Printf("Second line - condition true\n")}/* Change the values of a and b */
   a = false
   b = true
   if ( a && b ) {
      fmt.Printf("Line 3 - condition true\n")}else {
      fmt.Printf("Line 3 - condition false\n")}if ( !(a && b) ) {
      fmt.Printf("Line 4 - condition true\n")}}Copy the code

result

Line 2 - condition true line 3 - condition false line 4 - condition trueCopy the code
  • An operator
package main

import "fmt"

func main(a) {

   var a uint = 60      /* 60 = 0011 1100 */  
   var b uint = 13      /* 13 = 0000 1101 */
   var c uint = 0          

   c = a & b       /* 12 = 0000 1100 */
   fmt.Printf("The value of the first line -c is %d\n", c )

   c = a | b       /* 61 = 0011 */
   fmt.Printf("The value of the second line -c is %d\n", c )

   c = a ^ b       /* 49 = 0011 0001 */
   fmt.Printf("The value of the third line -c is %d\n", c )

   c = a << 2     /* 240 = 1111 0000 */
   fmt.Printf("Line 4 -c has the value %d\n", c )

   c = a >> 2     /* 15 = 0000 1111 */
   fmt.Printf("The value of line 5 -c is %d\n", c )
Copy the code

result

The value of -c in the first line is 12 the value of -c in the second line is 61 the value of -c in the third line is 49 the value of -C in the fourth line is 240 and the value of -c in the fifth line is 15Copy the code
  • The assignment operator
package main

import "fmt"

func main(a) {
   var a int = 21
   var c int

   c =  a
   fmt.Printf("Line 1 - = operator instance, c value = %d\n", c )

   c +=  a
   fmt.Printf("Line 2 - += operator instance, c value = %d\n", c )

   c -=  a
   fmt.Printf("Line 3 -= operator instance, c value = %d\n", c )

   c *=  a
   fmt.Printf("Line 4 - *= operator instance, c value = %d\n", c )

   c /=  a
   fmt.Printf("Line 5 - /= operator instance, c value = %d\n", c )

   c  = 200;

   c <<=  2
   fmt.Printf("Line 6 - <<= operator instance, c value = %d\n", c )

   c >>=  2
   fmt.Printf("Line 7 - >>= operator instance, c value = %d\n", c )

   c &=  2
   fmt.Printf("Line 8 - &= operator instance with c value = %d\n", c )

   c ^=  2
   fmt.Printf("Line 9 - ^= operator instance, c value = %d\n", c )

   c |=  2
   fmt.Printf("Line 10 - | = operator as an example, the value of c = % d \ n", c )

}
Copy the code

result

Line 1 -= operator instance, c value = 21 Line 2 - += operator instance, C value = 42 Line 3 - -= operator instance, c value = 21 line 4 - *= operator instance, Line 6 - <<= instance of the operator, line 7 - >>= instance of the operator, line 8 - &= instance of the operator, line 7 - >>= instance of the operator, line 8 - &= instance, Value of c = 0 line 9 - ^ = operator instance, c = 2 value line 10 - | = operator as an example, the value of c = 2Copy the code
  • Other operators
package main

import "fmt"

func main(a) {
   var a int = 4
   var b int32
   var c float32
   var ptr *int

   /* Operator instance */
   fmt.Printf("Line 1 - a variable of type = %T\n", a );
   fmt.Printf("Line 2 -b variable type = %T\n", b );
   fmt.Printf("Line 3 -c variable type = %T\n", c );

   The /* & and * operator instances */
   ptr = &a     /* 'PTR' contains the address of the 'a' variable */
   fmt.Printf("Value of a is %d\n", a);
   fmt.Printf("* PTR for % d \ n", *ptr);
}
Copy the code

result

Line 1 -a variable type = int Line 2 -b variable type = int32 Line 3 -C variable type = float32 The value of A is 4 * PTR is 4Copy the code
  • Operator priority
package main

import "fmt"

func main(a) {
   var a int = 20
   var b int = 10
   var c int = 15
   var d int = 5
   var e int;

   e = (a + b) * c / d;      // (30 * 15) / 5
   fmt.Printf("(a + b) * c/d = %d\n",  e );

   e = ((a + b) * c) / d;    // (30 * 15) / 5
   fmt.Printf("((a + b) * c)/d = %d\n" ,  e );

   e = (a + b) * (c / d);   / / (30) * (15/5)
   fmt.Printf("(a + b) * (c/d) = %d\n",  e );

   e = a + (b * c) / d;     //  20 + (150/5)
   fmt.Printf("A + (b * c)/d = %d\n" ,  e );  
}
Copy the code

result

The value of (a + b) * c/d is 90. The value of (a + b) * c/d is 90. The value of (a + b) * (c/d) is 90Copy the code

6 Conditional Statements

  • If the else statement
package main

import "fmt"

func main(a) {
   /* Local variable definition */
   var a int = 100;
 
   /* Determine the Boolean expression */
   if a < 20 {
       /* If the condition is true, the following statement */ is executed
       fmt.Printf("A less than 20 \ n" );
   } else {
       /* If the condition is false, the following statement */ is executed
       fmt.Printf("A not less than 20\n" );
   }
   fmt.Printf("A value is: %d\n", a);

}
Copy the code

result

A is not less than 20. The value of a is 100Copy the code
  • A switch statement
/ / instance
package main

import "fmt"

func main(a) {
   /* Define local variables */
   var grade string = "B"
   var marks int = 90

   switch marks {
      case 90: grade = "A"
      case 80: grade = "B"
      case 50.60.70 : grade = "C"
      default: grade = "D"  
   }

   switch {
      case grade == "A" :
         fmt.Printf("Good! \n" )    
      case grade == "B", grade == "C" :
         fmt.Printf("Good \ n" )      
      case grade == "D" :
         fmt.Printf("Pass \ n" )      
      case grade == "F":
         fmt.Printf("Fail \n" )
      default:
         fmt.Printf("Poor \ n" );
   }
   fmt.Printf("Your level is %s\n", grade );      
}
Copy the code

result

Excellent! You're an ACopy the code
// Example 2 Type Switch
package main

import "fmt"

func main(a) {
   var x interface{}
     
   switch i := x.(type) {
      case nil:  
         fmt.Printf(" x 的类型 :%T",i)                
      case int:  
         fmt.Printf("X is int")                      
      case float64:
         fmt.Printf("X is float64")          
      case func(int) float64:
         fmt.Printf("X is of type func(int)")                      
      case bool.string:
         fmt.Printf("X is a bool or string" )      
      default:
         fmt.Printf("Unknown type")}}Copy the code

result

Type of x :<nil>Copy the code
// Example 3 fallthrough
package main

import "fmt"

func main(a) {

    switch {
    case false:
            fmt.Println("1, case condition statement false")
            fallthrough
    case true:
            fmt.Println("2, case condition statement true")
            fallthrough
    case false:
            fmt.Println("3, case condition statement false")
            fallthrough
    case true:
            fmt.Println("4, case condition statement true")
    case false:
            fmt.Println("5, case condition statement false")
            fallthrough
    default:
            fmt.Println("6. Default case")}}Copy the code

result

The case condition statement is set to true. The case condition statement is set to falseCopy the code
  • The select statement
package main

import "fmt"

func main(a) {
   var c1, c2, c3 chan int
   var i1, i2 int
   select {
      case i1 = <-c1:
         fmt.Printf("received ", i1, " from c1\n")
      case c2 <- i2:
         fmt.Printf("sent ", i2, " to c2\n")
      case i3, ok := (<-c3):  // same as: i3, ok := <-c3
         if ok {
            fmt.Printf("received ", i3, " from c3\n")}else {
            fmt.Printf("c3 is closed\n")}default:
         fmt.Printf("no communication\n")}}Copy the code

result

no communication
Copy the code

7 Loop Statements

  • The for loop
/ / instance
package main

import "fmt"

func main(a) {
        sum := 0
        for i := 0; i <= 10; i++ {
                sum += i
        }
        fmt.Println(sum)
}
Copy the code

result

55
Copy the code
2 / / instance
package main

import "fmt"

func main(a) {
        sum := 1
        for ; sum <= 10; {
                sum += sum
        }
        fmt.Println(sum)

        // Write it like this, more like a While statement
        for sum <= 10{
                sum += sum
        }
        fmt.Println(sum)
}
Copy the code

result

16
16
Copy the code
// Example three wireless loop
package main

import "fmt"

func main(a) {
        sum := 0
        for {
            sum++ // Go on forever
        }
        fmt.Println(sum) // Cannot output
}
Copy the code

result

Copy the code
  • Break to terminate
  • Continue skips the current loop and proceeds to the next round
  • Goto goes to the marker
/ / instance goto
package main

import "fmt"

func main(a) {
   /* Define local variables */
   var a int = 10

   / * * / cycles
   LOOP: for a < 20 {
      if a == 15 {
         /* Skip the iteration */
         a = a + 1
         goto LOOP
      }
      fmt.Printf("A value is: %d\n", a)
      a++    
   }  
}
Copy the code

result

The value of a is: 10. The value of A is: 11. The value of A is: 12. The value of A is: 13. The value of A is: 14Copy the code

8 Language Functions

package main

import "fmt"

func main(a) {
   /* Define local variables */
   var a int = 100
   var b int = 200
   var ret int

   /* Call the function and return the maximum value */
   ret = max(a, b)

   fmt.Printf( "Maximum value: %d\n", ret )
}

/* Returns the maximum value of two numbers */
func max(num1, num2 int) int {
   /* Define local variables */
   var result int

   if (num1 > num2) {
      result = num1
   } else {
      result = num2
   }
   return result
}
Copy the code

result

The maximum value is 200Copy the code
  • Return multiple values
package main

import "fmt"

func swap(x, y string) (string.string) {
   return y, x
}

func main(a) {
   a, b := swap("Google"."Runoob")
   fmt.Println(a, b)
}
Copy the code

result

Runoob Google
Copy the code

Scope of language variables

package main

import "fmt"

/* Declare the global variable */
var a int = 20;

func main(a) {
   /* Declare a local variable */ in the main function
   var a int = 10
   var b int = 20
   var c int = 0

   fmt.Printf("main()函数中 a = %d\n",  a);
   c = sum( a, b);
   fmt.Printf("main()函数中 c = %d\n",  c);
}

/* Function definition - Add two numbers */
func sum(a, b int) int {
   fmt.Printf("Sum () a = %d\n",  a);
   fmt.Printf("In sum() b = %d\n",  b);

   return a + b;
}
Copy the code

result

A is equal to 10 in the main() function and sum() is equal to 20 in the sum() function and c is equal to 30 in the main() functionCopy the code