Go语言基础语法学习笔记[2]



算术运算符:

+  - * /  % ++  --

注意:++ --这两个运算符不返回值,所以不能作为赋值或输出,例如下面的用法是非法的

var a int = 3

fmt.Println(a++)    //syntax error: unexpected ++, expectingcomma or )

var b = a++             //syntax error: unexpected ++ atend of statement

同时,也没有前置运算符++a或者--a这种用法

 

关系运算符:

==  != > <  >= <=

 

逻辑运算符:

&&  || !

 

位运算符:

&  | ^ <<  >>

 

赋值运算符:

=  += -= *=  /= %=  <<=  >>= &=  ^= |=

 

指针运算符:

&:返回变量的内存地址

*:返回指针指向的值

例如:

var a int = 3;

var p *int;

p = &a;             //p指向a的地址

fmt.Println(a, *p)  //a*p的值相同

*p = 10             //设置*p的值,也就同时改变了a的值

fmt.Println(a)      //a = 10

 

if语句:

if (condition) {

    // dosomething

}

if condition {

    // dosomething

}

注意,条件的小括号可以省略,大括号不能省略

else if, else举例:

if a < 10 {

   fmt.Println(a, "a < 10")

} else if a < 20 {

   fmt.Println(a, "10 <= a < 20")

} else {

   fmt.Println(a, "a >= 20")

}

 

switch语句:

switch var1 {

    case valA:

        ...

    case valB1,valB2, valB3:

        ...

    default:

        ...

}

 

type-switch的用法:

type People interface {

    eat()

    drink()

}

type Child interface {

    learn()

}

type Adult interface {

    work()

}

type Student struct {

}

type Teacher struct {

}

func (t Teacher) eat() {

   fmt.Println("Teacher eat")

}

func (t Teacher) drink() {

   fmt.Println("Teacher drink")

}

func (t Teacher) work() {

   fmt.Println("Teacher teaching")

}

func (t Student) eat() {

   fmt.Println("Student eat")

}

func (t Student) drink() {

   fmt.Println("Student drink")

}

func (t Student) learn() {

   fmt.Println("Student learning")

}

func DoSomething(someone People) {

    switch inst:= someone.(type) {

        caseChild:

           inst.learn()

        caseAdult:

           inst.work()

    }

}

var daniel People;

daniel = new(Teacher)

DoSomething(daniel)

daniel = new(Student)

DoSomething(daniel)

 

Output:

D:\Programming\GoWork>go run HelloWorld.go

Teacher teaching

Student learning

 

三个跟类型有关的操作符:

a.()返回2个值inst和ok,ok代表是否是TypeName类型,ok如果是true,inst就是转换后的类型

例如:

if inst, ok := daniel.(Child); ok {

    inst.learn()

}

a.(type):type是关键字,结合type-switch使用

TypeName(a):强制转换为TypeName类型

 

Go语言中只有一种循环语句,即for循环

for init; condition; post{}   //完整格式

for condition {}                     //类似于while循环

for {}                                        //死循环

循环控制语句有break, continue,goto(非常不推荐使用), goto的用法举例:

Test: for {

    if a > 10{

        gotoTest;

    }

}

 

函数:

函数定义:

func function_name( [parameter list]) [return_types] {

   //函数体
}

例如:

func divide(a, b float64) (float64, string) {

    if b == 0 {

        return0, "Cannot divide into 0"

    } else {

        return a/ b, "OK"

    }

}

if a ,b := divide(1.0, 0); b == "OK" {

   fmt.Println(a)

} else {

   fmt.Println(b)

}

值传递(默认):

func swap(a, b int) {

   //ab交换不会有任何效果

}

引用传递:

var a, b int = 1, 2

func (x, y *int) {

    var temp int= *x

    *x = *y

    *y = temp

} (&a, &b)

fmt.Println(a, b)

将函数作为值使用:

max := func(x, y int) int {

    if x > y{

        return x

    } else {

        return y

    }

}

fmt.Println(max(10, 8))

匿名函数(闭包)

func getSeq() func() int {

    i := 0

    returnfunc() int {

        i += 1

        return i

    }

}

num := getSeq()

for i := 1; i < 10; i++ {

   fmt.Println(num())

}

方法:

一个方法就是一个包含了接受者的函数,接受者可以是命名类型或者结构体类型的一个值或者是一个指针。所有给定类型的方法属于该类型的方法集。

func (variable_namevariable_data_type) function_name() [return_type]{
   /*
函数体*/
}

 

变量作用域:

函数内定义的变量称为局部变量

函数外定义的变量称为全局变量

函数定义中的变量称为形式参数

你可能感兴趣的:(Programming,程序设计,Golang)