Basic syntax of golang
1. Naming rules
-
All naming of function name, variable name, constant name, type name, statement label and package name in go language follow a simple naming rule
-
Must be with a letter or underscore_ Start with any number of letters, numbers, or underscores
-
Upper and lower case letters are considered different in go language
-
There are 25 keywords in go language, which cannot be used for custom names
There are also more than 30 predefined names for built-in constants, types, and functions
Built-in Constants
true false iota nil
Built in type
int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex128 complex64 bool byte rune string error
Built in function
make len cap new append copy close delete complex real imag panic recover
2. Variables
Declare variable
Syntax format:
var Variable name variable type
Code example:
package main import "fmt" func main() { /* If the variable is not referenced, an error will be reported */ // 1. Direct declaration var v1 int var v2 int // 2. Declare multiple variables at once var v3, v4 int var ( v5 int v6 int ) fmt.Println(v1, v2, v3, v4, v5, v6) }
Variable initialization
Code example:
package main import "fmt" func main() { // 1. Direct initialization var v1 int = 1 // 2. Automatic derivation initialization var v2 = 2 // 3. Declare and initialize v3 := 3 fmt.Println(v1, v2, v3) }
Variable assignment
Code example:
package main import "fmt" func main() { // 1. Direct assignment var v1 int v1 = 1 // 2. := v2 := 2 // 3. Multiple assignment var v3, v4 int fmt.Println(v1, v2, v3, v4) }
Careful students should see that to some extent, variable initialization and assignment are the same thing. We will learn more later in the article
Anonymous variable
_ A single underscore indicates an anonymous variable. In order to prevent the variable from being referenced, an error is reported
package main import "fmt" func test() (int, int) { return 1,2 } func main() { // _ A single slide line indicates an anonymous variable, and no error is reported for reference v1, _ := test() fmt.Println(v1) }
3. Constants and enumerations
const keyword defines a constant
package main import "fmt" // Declared outside a function const filename = "123.txt" const ( username = "admin" password = "admin" ) func main() { // Declared inside a function const filename = "456.txt" fmt.Println(filename) }
iota constant generator and enumeration types
package main import "fmt" // enumeration func enums001() { const ( v1 = 0 v2 = 1 v3 = 2 ) fmt.Println(v1, v2, v3) // Output 0 1 2 } // Omittable enumeration func enums002() { const ( v1 = 0 v2 v3 = 1 v4 ) fmt.Println(v1, v2, v3, v4) // Output 0 0 1 1 } // Define self incrementing enumeration types // iota defaults to 0 // Downward self increasing func enums003() { const ( v1 = iota v2 v3 v4 ) fmt.Println(v1, v2, v3, v4) // Output 0 1 2 3 } // Compute enumeration func enums004() { // b equals 1 shift left // 1 < < (10 * 0) 1 < < 0 is equivalent to 1 * 2 ^ 0 const ( b = 1 << (10*iota) kb mb gb tb pb ) fmt.Println(b, kb, mb, gb, tb, pb) // Output results } func main() { //enums001() //enums002() //enums003() enums004() }
4. Basic data type
int8 is a signed type and unit8 is an unsigned type
Different types of data in go language cannot be added, which will lead to compilation errors.
Integer
Code example:
package main import ( "fmt" "reflect" ) func main() { var v1 int v1 = 123 v2 := 42 fmt.Println(reflect.TypeOf(v1)) fmt.Println(reflect.TypeOf(v2)) }
Output int type
float
Code example:
package main import ( "fmt" "reflect" ) func main() { var f1 float32 f1 = 12.5 f2 := 13.66 fmt.Println(reflect.TypeOf(f1)) fmt.Println(reflect.TypeOf(f2)) }
Boolean
Code example:
package main import ( "fmt" "reflect" ) func main() { // There are only two values: true and false var b1 bool b1 = false fmt.Println(b1) // You can use = = or= Get Boolean b2 := (1!=2) fmt.Println(reflect.TypeOf(b2)) // use! Negate Boolean types b3 := !b2 fmt.Println(b3) }
byte character type
Code example:
package main import ( "fmt" "reflect" ) func main() { // Single quotation mark assignment byte var ch byte ch = 'a' fmt.Println(ch) // Format output fmt.Printf("ch = %c\n", ch) // uint8 fmt.Println(reflect.TypeOf(ch)) }
character string
Code example:
package main import "fmt" func main() { // Strings are assigned with double quotes var s1 string s1 = "abc" fmt.Println(s1) // Takes the first character in the string ch := s1[0] fmt.Println(ch) // Backquotes are used to create native string literals (original format output) s2 := `hello xxxxx world ` fmt.Println(s2) }
5. Use of FMT package
Format parameter list
format | meaning |
---|---|
%% | A% literal |
%b | A binary integer value (Radix 2), or a (Advanced) floating-point number with exponent 2 represented by scientific counting |
%c | character. The input numbers can be converted into corresponding characters according to ASCII code |
%d | A decimal value (base 10) |
%e | A floating-point number or complex number represented by scientific notation e |
%E | A floating-point number or complex number represented by scientific notation E |
%f | A floating-point number or complex value expressed in standard notation |
%g | Floating point number or complex number represented by% e or% f, either of which is output in the most compact way |
%G | Floating point number or complex number represented by% E or% f, either of which is output in the most compact way |
%o | A number in octal (base 8) |
%p | The address of a value in hexadecimal (base 16), prefixed with 0x, and the letters are represented by lowercase a-f |
%q | Use Go syntax and escape when necessary. Strings or byte slices enclosed in double quotation marks [] byte, or numbers enclosed in single quotation marks |
%s | character string. Output the characters in the string until the empty characters in the string (the string ends with '\ 0', which is the empty character) |
%t | Boolean value output as true or false |
%T | The type of value output using Go syntax |
%U | An integer code point represented in Unicode notation. The default value is 4 numeric characters |
%v | The built-in or user-defined value output in the default format, or the user-defined value output in the String() mode of its type, if this method exists |
%x | Integer value expressed in hexadecimal (base is hexadecimal), and the numbers a-f are expressed in lowercase |
%X | Integer value expressed in hexadecimal (base is hexadecimal), and the numbers A-F are expressed in lowercase |
Output example:
package main import "fmt" func main() { // integer a := 16 // %b binary output fmt.Printf("%b\n", a) fmt.Printf("%%\n", a) }
Input example:
package main import "fmt" func main() { // Defines the variable that receives the input var age int fmt.Printf("Please enter your age: ") // Receive input fmt.Scan(&age) fmt.Printf("The age entered is: %d", age) }
6. Type alias
Type keyword custom type
package main import ( "fmt" "reflect" ) // Type custom type name original type type myint int func main() { var v1 myint v1 = 10 fmt.Println(reflect.TypeOf(v1)) } // Output result: main myint
7. Type conversion
go language does not allow implicit conversion
package main import ( "fmt" "reflect" ) func main() { var a byte a = 97 var b int b = int(a) // Output variable type fmt.Println(reflect.TypeOf(b)) }
8. Operator
Symbol | explain |
---|---|
+ | Add |
- | subtract |
* | Multiply |
/ | be divided by |
% | Seeking remainder |
++ | Self increasing |
-- | Self subtraction |
package main import "fmt" func main() { a := 10 b := 6 // + fmt.Println(a+b) // - fmt.Println(a-b) // * fmt.Println(a*b) // / fmt.Println(a/b) // % fmt.Println(a%b) // ++ fmt.Println(a) a++ fmt.Println(a) // - fmt.Println(b) b-- fmt.Println(b) }
Relational operator
Symbol | explain |
---|---|
== | be equal to |
!= | Not equal to |
> | greater than |
< | less than |
>= | Greater than or equal to |
<= | Less than or equal to |
package main import "fmt" func main() { a := 10 b := 6 // == fmt.Println(a==b) // != fmt.Println(a!=b) // > fmt.Println(a>b) // < fmt.Println(a<b) // >= fmt.Println(a>=b) // <= fmt.Println(a<=b) // == fmt.Println(a==b) }
Logical operator
Symbol | explain |
---|---|
&& | And |
|| | or |
! | wrong |
package main import "fmt" func main() { a := 10 b := 6 c := 6 // && fmt.Println(a > b && b >= c) // || fmt.Println(a > b || b > c) // ! var d bool fmt.Println(!d) }
Assignment operator
operator | describe | example |
---|---|---|
= | A simple assignment operator assigns the value of an expression to an lvalue | C = A + B assigns the result of the A + B expression to C |
+= | Add and assign | C += A equals C = C + A |
-= | Subtraction before assignment | C -= A equals C = C - A |
*= | Multiply and assign | C *= A equals C = C * A |
/= | Divide and assign | C /= A equals C = C / A |
%= | Assign value after remainder | C% = a equals C = C% a |
<<= | Left shift assignment | C < < 2 equals C = C < < 2 |
>>= | Assignment after shift right | C > > = 2 equals C = C > > 2 |
&= | Bitwise and post assignment | C & = 2 equals C = C & 2 |
^= | Assignment after bitwise XOR | C ^= 2 equals C = C ^ 2 |
|= | Bitwise or post assignment | C | 2 equals C = C | 2 |
Bit operation
operator | describe | example |
---|---|---|
& | The bitwise and operator "&" is a binocular operator. Its function is to sum the binary phase corresponding to each of the two numbers involved in the operation. | (A & B) result 12, binary 0000 1100 |
| | The bitwise OR operator '|' is a binocular operator. Its function is the binary phase or phase corresponding to each of the two numbers involved in the operation | (A | B) the result is 61 and the binary is 0011 1101 |
^ | The bitwise exclusive or operator '^' is a binocular operator. Its function is that the corresponding binaries of the two numbers involved in the operation are different or, when the two corresponding binaries are different, the result is 1. | (A ^ B) the result is 49 and the binary is 0011 0001 |
<< | The shift left operator "< <" is a binocular operator. Shifting n bits to the left is multiplied by 2 to the nth power. Its function shifts all the binary bits of the operand on the left of "< <" to the left by several bits, and the number on the right of "<" specifies the number of bits to move. The high bits are discarded and the low bits are supplemented by 0. | A < < 2, the result is 240, and the binary is 1111 0000 |
>> | The shift right operator "> >" is a binocular operator. Shifting n bits to the right is divided by 2 to the nth power. Its function is to shift all the binary bits of the operand on the left of "> >" to the right by several bits, and the number on the right of "> >" specifies the number of bits to be moved. | A > > 2 the result is 1 |
package main import "fmt" func main() { 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.Println(c) c = a | b /* 61 = 0011 1101 */ fmt.Println(c) c = a ^ b /* 49 = 0011 0001 */ fmt.Println(c) c = a << 2 /* 240 = 1111 0000 a*(2**2) */ fmt.Println(c) c = a >> 2 /* 15 = 0000 1111 a/(2*2) */ fmt.Println(c) }