Go language study essay 2019-10-29
1. Variable
Method 1
Define a variable just like this:
var identifier type
For example:
var a int=1
You can also define multiple variables in a row,just like this:
var identifier1,identifier2 type
For example:
var a,b,c int = 1,2,3
Notice
If you don’t initial the variable,the variable’s value will be default.
Default value:
- number->0
- bool->false
- string->""
- the following type is nil:
- *int
- []int
- map[string] int
- chan int
- func(string) int
- error
Method 2
You can also define a variable without type,for example:
var a = 1
The system will automatically determine the type of variable.
Method 3
You can omit the keyword:var
For example:
a :=1
Global variable
var (
a int
b string
c bool
)
2.Constant
Constant is an identifier that will not be modified while the programe is running.
THe data type of constant only can be bool,int,float,plural and string.
Define a constant just like this:
const identifier [type] = value
For example:
const a int = 1
You can also define multiple constant in a row,for example:
const a,b,c = 1,2,3
iota
iota is a special constant which can be modified by compiler,you can think of it as the constant’s index.
const (
a = iota //a=0
b = iota //b=1
c = iota //c=2
)
You can abbreviate it as
const (
a = iota //a=0
b //b=1
c //c=2
)
3.Operator
Define variable a and b:
a,b :=2,1
- Arithmetic operator
operator | description | example |
---|---|---|
+ | add | a+b=3 |
- | subtract | a-b=1 |
* | multiply | a*b=2 |
/ | divide | a/b=2 |
% | remainder | a%b=0 |
++ | increase | a++ is 3 |
– | self decrement | a-- is 1 |
- Relational operator
operator | description | example |
---|---|---|
== | check if the left and right variable is equal | a=b is false |
!= | check if the left and right variable is not equal | a!=b is true |
> | check if the left great than the right | a>b is true |
< | check if the left less than the right | a<b is false |
>= | check if the left greater or equal to the right | a>=b is true |
<= | check if the left less or equal to the right | a<=b is false |
- Logical Operators
operator | description | example |
---|---|---|
&& | logical and | true&&false is false |
|| | logical or | true||false is true |
! | logical non | !true is false |
- Bit operator
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 |
a<<n is a*2^n
a>>n is a/2^n
For example:
60<<2 = 60*2^2=240
60>>2 = 60/2^2=15
- Assignment operator
operator | description | example |
---|---|---|
= | assignment | c=1 |
+= | add and assignment | c+=1 is c=c+1 |
-= | subtract and assignment | c-=1 is c=c-1 |
*= | multiply and assignment | c*=1 is c=c*1 |
/= | divide and assignment | c/=1 is c=c/1 |
%= | remainder and assignment | c%=1 is c=c%1 |
<<= | shift left and assignment | c<<=1 is c=c<<1 |
>>= | shift right and assignment | c>>=1 is c=c>>1 |
&= | bitwise and and assignment | c&=1 is c=c&1 |
^= | bitwise XOR and assignmentn | c^=1 is c=c^1 |
|= | bitwise or and assignment | c|=1 is c=c|1 |
- Other operator
operator | description | example |
---|---|---|
& | return variable address | &a |
* | Pointer | *a |
- Priority of operator
priority | operator |
---|---|
5 | * / % << >> & &^ |
4 | + - | ^ |
3 | == != < <= > >= |
2 | && |
1 | || |