first go program
package main import "fmt" func main() { /* This is my first simple program */ fmt.Println("Hello,
World!") }
* First line of code package main The package name is defined . You must indicate in the first non comment line of the source file which package the file belongs to , as :package main.package
main Represents a program that can be executed independently , each Go The application contains a main Bag of .
* next row import "fmt" tell Go Compiler this program needs to use fmt package ( Function of , Or other elements ),fmt The package implements formatting
IO( input / output ) Function of .
* next row func main() Is the function that the program begins to execute .main Functions are required for every executable program , Generally speaking, it is the first function executed after startup ( If so
init() Function is executed first ).
* next row /*...*/ It's a comment , Will be ignored when the program is executed . Single line annotation is the most common form of annotation , You can use it anywhere //
Opening single line comment . Multiline comments are also called block comments , All of them have been /* start , And */ ending , Multiline annotations are commonly used for document descriptions of packages or for annotating chunked code fragments .
* next row fmt.Println(...) You can output strings to the console , At the end, the new line character is added automatically \n.
*
When identifier ( Include constants , variable , type , Function name , Structure fields and so on ) Start with a capital letter , as :Group1, The object using this form of identifier can be used by the code of the external package ( The client program needs to import this package first ), This is called export ( Like in object-oriented languages
public); If the identifier starts with a lowercase letter , Is not visible outside the package , But they are visible and available within the entire package ( Like in object-oriented languages protected ).
data type

Go The basic types of language are :

* bool
* string
* int,int8,int16,int32,int64
* uint,uint8,uint16,uint32,uint64,uintptr
* byte // uint8 Alias for
* rune // int32 Alias for For one Unicode code
* float32,float64
* complex64,complex128
variable

Go Language is a statically typed language , So variable (variable) There are clear types , The compiler also checks the correctness of variable types .

1) The general form of declaring variables is to use var keyword :var name type

Go Unlike many programming languages , It places the type of the variable after the name when it declares the variable . The advantage of this is that you can avoid C The vague form of declaration in language , for example :int* a, b; . Only
a It's a pointer and b no . If you want both variables to be pointers , You need to write them separately . And in the Go in , You can and easily declare them as pointer types :var a, b *int

Multivariable declaration
var ( a int b string c []float32 d func() bool e struct { x int } )
 

2) except var Out of keywords , You can also use a shorter variable definition and initialization syntax .

name := expression

It should be noted that , Short mode (short variable declaration) There are the following restrictions :

* Defining variables , Explicit initialization at the same time .
* Cannot provide data type .
* Can only be used inside a function .

and var Formal declaration statements are the same , Short variable declaration statements can also be used to declare and initialize a set of variables :

i, j := 0, 1

3) Determine the type of variable according to the value .
package main import "fmt" func main() { var d = true fmt.Println(d) }
Arithmetic operator
package main import "fmt" func main() { var a int = 21 var b int = 10 var c
int c = a + b fmt.Printf(" first line - c The value of is %d\n", c ) c = a - b fmt.Printf(" The second line - c
The value of is %d\n", c ) c = a * b fmt.Printf(" The third line - c The value of is %d\n", c ) c = a / b
fmt.Printf(" The fourth line - c The value of is %d\n", c ) c = a % b fmt.Printf(" The fifth line - c The value of is %d\n", c )
a++ fmt.Printf(" Line 6 - a The value of is %d\n", a ) a=21 // To facilitate testing ,a Here we re assign it as 21 a--
fmt.Printf(" Line 7 - a The value of is %d\n", a ) } first line - c The value of is 31 The second line - c The value of is 11 The third line - c The value of is
210 The fourth line - c The value of is 2 The fifth line - c The value of is 1 Line 6 - a The value of is 22 The seventh line - a The value of is 20
Bitwise Operators

Bit operators operate on binary bits of integers in memory .

The following table lists bit operators &, |, and ^ Calculation of :

 

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
 

assume A = 60; B = 13; Its binary number is converted to :
A = 0011 1100 B = 0000 1101 ----------------- A&B = 0000 1100 A|B = 0011 1101
A^B = 0011 0001
 

operator describe example
& The binary phase and the binary phase corresponding to the two numbers involved in the operation . (A & B) The result is 12, Binary is 0000 1100
| Binary phase or (A | B) The result is 61, Binary is 0011 1101
^ The binary corresponding to the two numbers involved in the operation is different or , When two corresponding bits are different , The result is 1. (A ^ B) The result is 49, Binary is 0011 0001
<< Shift left n Bits are times 2 Of n Power . Its function "<<" All the binary bits of the left-hand operands are shifted to the left by several bits , from "<<" The number on the right specifies the number of digits to move , High discard , Low compensation 0. A <<
2 The result is 240 , Binary is 1111 0000
  Assignment Operators

The following table lists all Go Assignment operators for languages .

operator describe example
= Simple assignment operators , Assign the value of an expression to an lvalue C = A + B take A + B The expression result is assigned to C
+= Add and assign C += A be equal to C = C + A
-= Subtract and then assign a value C -= A be equal to C = C - A
*= Multiply and then assign C *= A be equal to C = C * A
/= Assign after division C /= A be equal to C = C / A
%= Calculate the remainder and then assign the value C %= A be equal to C = C % A
<<= Assignment after left shift C <<= 2 be equal to C = C << 2
>>= Assignment after right shift C >>= 2 be equal to C = C >> 2
&= Bitwise and post assignment C &= 2 be equal to C = C & 2
^= Assignment after bitwise exclusive or C ^= 2 be equal to C = C ^ 2
|= Bitwise or post assignment C |= 2 be equal to C = C | 2
 

Other Operators

 

operator describe example
& Return variable storage address &a; The actual address of the variable will be given .
* Pointer variable . *a; Is a pointer variable
The following examples demonstrate the use of other operators :

example
package main import "fmt" func main() { var a int = 4 var b int32 var c
float32 var ptr *int /* Operator instance */ fmt.Printf(" The first 1 That's ok - a The variable type is = %T\n", a );
fmt.Printf(" The first 2 That's ok - b Variable type is = %T\n", b ); fmt.Printf(" The first 3 That's ok - c The variable type is = %T\n",
c ); /* & and * Operator instance */ ptr = &a /* 'ptr' Contains 'a' Address of variable */ fmt.Printf("a The value of is
%d\n", a); fmt.Printf("*ptr by %d\n", *ptr); }
The running results of the above examples :
The first 1 That's ok - a The variable type is = int The first 2 That's ok - b Variable type is = int32 The first 3 That's ok - c The variable type is = float32 a The value of is
4 *ptr by 4
Operator priority

Top down represents priority from high to low :

priority operator
5 * / % << >> & &^
4 + - | ^
3 == != < <= > >=
2 &&
1 ||
loop

ordinary for loop
package main import "fmt" func main() { sum := 0 for i := 0; i <= 10; i++ {
sum += i } fmt.Println(sum) }
 foreach

The loop in this format can be used for Strings , array , Slice and so on to iterate the output elements .
package main import "fmt" func main() { strings := []string{"google",
"runoob"} for i, s := range strings { fmt.Println(i, s) } numbers := [6]int{1,
2, 3, 5} for i,x:= range numbers { fmt.Printf(" The first %d position x Value of = %d\n", i,x) } }
 

function
func function_name( [parameter list] ) [return_types] { Function body }
* func: The function is defined by func Opening statement
* function_name: Function name , The function name and the parameter list together constitute the function signature .
* parameter
list: parameter list , The parameter is like a placeholder , When a function is called , You can pass values to parameters , This value is called the actual parameter . Parameter list specifies parameter type , order , And the number of parameters . Parameters are optional , In other words, the function can also contain no parameters .
* return_types: Return type , Function returns a list of values .return_types Is the data type of the column value . Some functions do not require a return value , In this case
return_types It's not necessary .
* Function body : Code set of function definitions . /* Function returns the maximum of two numbers */ func max(num1, num2 int) int { /* Declare local variables
*/ var result int if (num1 > num2) { result = num1 } else { result = num2 }
return result }
Return multiple values
package main import "fmt" func swap(x, y string) (string, string) { return y,
x } func main() { a, b := swap("Google", "Runoob") fmt.Println(a, b) }
Reference transmission
package main import "fmt" func main() { /* Defining local variables */ var a int = 100 var b
int= 200 fmt.Printf(" Before exchange ,a Value of : %d\n", a ) fmt.Printf(" Before exchange ,b Value of : %d\n", b ) /*
call swap() function * &a point a Pointer ,a Address of variable * &b point b Pointer ,b Address of variable */ swap(&a, &b)
fmt.Printf(" After exchange ,a Value of : %d\n", a ) fmt.Printf(" After exchange ,b Value of : %d\n", b ) } func
swap(x *int, y *int) { var temp int temp = *x /* preservation x Value on address */ *x = *y /* take y
Value assigned to x */ *y = temp /* take temp Value assigned to y */ }
array

statement :

The syntax is as follows :
var variable_name [SIZE] variable_type
The above is the definition of one-dimensional array . For example, the following defines an array balance Count Reg 10 The type is float32:
var balance [10] float32
initialization :
var balance = [5]float32{1000.0, 2.0, 3.4, 7.0, 50.0}
Initialization array {} The number of elements in cannot be greater than [] Number in .

If ignored [] The number in does not set the array size ,Go The language sets the size of the array based on the number of elements :
var balance = [...]float32{1000.0, 2.0, 3.4, 7.0, 50.0}
This instance is the same as the above one , Although the size of the array is not set .
balance[4] = 50.0
Initialize 2D array :
a = [3][4]int{ {0, 1, 2, 3} , /* The index of the first row is 0 */ {4, 5, 6, 7} , /* The index of the second line is 1 */ {8,
9, 10, 11}, /* The index of the third row is 2 */ }
be careful : In the last line of the code above } There must be a comma , Because of the last line } It can't be on its own , It can also be written like this :
a = [3][4]int{ {0, 1, 2, 3} , /* The index of the first row is 0 */ {4, 5, 6, 7} , /* The index of the second line is 1 */ {8,
9, 10, 11}} /* The index of the third row is 2 */
Pointer

and c equally
package main import "fmt" func main() { var a int= 20 /* Declare actual variables */ var ip *int
/* Declare pointer variables */ ip = &a /* The storage address of the pointer variable */ fmt.Printf("a The address of the variable is : %x\n", &a ) /*
The storage address of the pointer variable */ fmt.Printf("ip The pointer address stored in the variable : %x\n", ip ) /* Accessing values using pointers */
fmt.Printf("*ip Value of variable : %d\n", *ip ) } a The address of the variable is : 20818a220 ip Pointer address of variable storage :
20818a220 *ip Value of variable : 20
Null pointer judgment :
if(ptr != nil) /* ptr Is not a null pointer */ if(ptr == nil) /* ptr Is a null pointer */
Multiple pointers : 
package main import "fmt" func main() { var a int var ptr *int var pptr **int
a = 3000 /* Pointer ptr address */ ptr = &a /* Pointer ptr address */ pptr = &ptr /* obtain pptr Value of
*/ fmt.Printf(" variable a = %d\n", a ) fmt.Printf(" Pointer variable *ptr = %d\n", *ptr )
fmt.Printf(" Pointer variable to pointer **pptr = %d\n", **pptr) } variable a = 3000 Pointer variable *ptr = 3000
Pointer variable to pointer **pptr = 3000
 

 

Technology
©2019-2020 Toolsou All rights reserved,
java Four functional interfaces ( a key , simple ) It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) Browser kernel ( understand )HashMap Explain in detail Some East 14 Pay change 16 salary , Sincerity or routine ?html Writing about cherry trees , Writing about cherry trees