Go language study essay 2019-10-29

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
operatordescriptionexample
+adda+b=3
-subtracta-b=1
*multiplya*b=2
/dividea/b=2
%remaindera%b=0
++increasea++ is 3
self decrementa-- is 1
  • Relational operator
operatordescriptionexample
==check if the left and right variable is equala=b is false
!=check if the left and right variable is not equala!=b is true
>check if the left great than the righta>b is true
<check if the left less than the righta<b is false
>=check if the left greater or equal to the righta>=b is true
<=check if the left less or equal to the righta<=b is false
  • Logical Operators
operatordescriptionexample
&&logical andtrue&&false is false
||logical ortrue||false is true
!logical non!true is false
  • Bit operator
pqp & qp | qp ^ q
00000
01011
11110
10011

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
operatordescriptionexample
=assignmentc=1
+=add and assignmentc+=1 is c=c+1
-=subtract and assignmentc-=1 is c=c-1
*=multiply and assignmentc*=1 is c=c*1
/=divide and assignmentc/=1 is c=c/1
%=remainder and assignmentc%=1 is c=c%1
<<=shift left and assignmentc<<=1 is c=c<<1
>>=shift right and assignmentc>>=1 is c=c>>1
&=bitwise and and assignmentc&=1 is c=c&1
^=bitwise XOR and assignmentnc^=1 is c=c^1
|=bitwise or and assignmentc|=1 is c=c|1
  • Other operator
operatordescriptionexample
&return variable address&a
*Pointer*a
  • Priority of operator
priorityoperator
5* / % << >> & &^
4+ - | ^
3== != < <= > >=
2&&
1||
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值