go语言初体验（流程控制、range遍历、函数、结构体、面向对象）

Go

// main

package main

import (

"fmt"

)

func main() {

x := 2

switch x {

case 1:

fmt.Print("beifeng 1")

case 2:

fallthrough

case 3:

fmt.Print("beifeng 2")

default:

fmt.Print("beifeng 3")

}

}

// main.go

package main

import (

"fmt"

)

func main() {

x := "zhangsan"

for _, v := range x {

fmt.Printf("%c\n", v)

}

}

*传指针的值，&指针的地址

3.1 函数传值与传指针

// func_1

package main

import (

"fmt"

)

func swap(a, b int) (int, int) {

return b, a

}

*a = *a + 1

return a

}

func main() {

a := 1

fmt.Printf("%d", a)

}

3.2 匿名函数

/ main.go

package main

import (

"fmt"

)

func main() {

type sum func(x, y int) int

var f sum = func(x, y int) int {

return x + y

}

fmt.Println(f(3, 4))

}

3.3 函数中传入切片参数

// fun_2

package main

import (

"fmt"

)

//求和函数，传入一个动态整型数组

func getSum(num []int) int {

//初始化sum变量

sum := 0

//遍历数组，求和

for i := 0; i < len(num); i++ {

sum += num[i]

}

//返回sum值

return sum

}

func main() {

//初始化num整型切片变量

num := []int{1, 2, 3, 4, 5}

fmt.Print(getSum(num))

}

3.4 defer函数

defer延迟的意思；后进先去

// defer.go

package main

import (

"fmt"

)

func main() {

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

defer fmt.Println(i)

}

}

3.5 pannic

Go的异常处理机制

Panic 捕获异常，程序挂

// panic.go

package main

import (

"fmt"

)

func main() {

defer func() {

fmt.Println("After panic from defer !") //panic之后 defer里面的依然可以得到执行

}()

panic("I am panicing!")

fmt.Println("After panic!") //panic后面得不到执行

}

Go语言没有private、protected、public这样的关键字。

// main.go

package main

import (

"fmt"

)

type Person struct {

name string

age  int

}

type Student struct {

Person

age        int

speciality string

}

func main() {

student := Student{Person{"zhangsan", 25}, 30, "maths"}

fmt.Printf("%v %v", student.Person.age, student.age)

}

Golang:"A method is a function with an implicit first argument, called a receiver.“

point :=&Point{}

point :=&Point{x:100, y:100}

point := Point{}

// main

package main

import (

"fmt"

)

/*func compare(a, b int) bool {

return a < b

}*/

//定义一个结构体Point

type Point struct {

px float32

py float32

}

//接收者point，接收类型结构体指针Point，方法setXY(),传入参数px,py

func (point *Point) setXY(px, py float32) {

//定义接收者属性

point.px = px

point.py = py

}

//接收者point 接收类型结构体指针Point，方法getxy()，返回两个float32数据类型的值

func (point *Point) getXY() (float32, float32) {

//返回x,y值

return point.px, point.py

}

//定义一个结构体 Integer

type Integer struct {

//定义结构体的属性

value int

}

func main() {

//初始化一个类

point := new(Point)

point.setXY(1.23, 4.56)

px, py := point.getXY()

fmt.Print(px, py)

}

5.1 面向对象之继承、接口、接口赋值、any类型、类型查询

Go语言中任何对象实例都满足空接口interface{}，interface{}可以接收所有数据类型

package main

import "fmt"

//定义一个Person结构体

type Person struct {

name string

age  int

}

//接收者person，接收结构体类型Person, 方法getNameAndAge() 返回string,int值

func (person Person) getNameAndAge() (string, int) {

return person.name, person.age

}

type Student struct {

Person

speciality string

}

func (student Student) getSpeciality() string {

return student.speciality

}

//定义一个Animal接口，实现的方法有Fly() Run()

type Animal interface {

Fly()

Run()

}

type Animal2 interface {

Fly()

}

type Bird struct {

}

//通过接收者实现接口的方法

//接收者bird实现Fly（）方法

func (bird Bird) Fly() {

fmt.Println("Bird is flying!!!!")

}

//接收者bird实现Run()方法

func (bird Bird) Run() {

fmt.Println("Bird is running!!!!")

}

func main() {

student := new(Student)

student.name = "zhangsan"

student.age = 26

name, age := student.getNameAndAge()

student.speciality = "computer"

speciality := student.getSpeciality()

fmt.Println(name, age, speciality)

//定义animal接口

var animal Animal

//定义animal2接口

var animal2 Animal2

//初始化一个对象bird

bird := new(Bird)

//将对象实例bird赋值给animal接口

animal = bird

//将animal接口赋值给animal2接口

animal2 = animal

//

//animal = bird //把类实例直接赋值给接口

//animal2 = bird

animal2.Fly()

animal2.Fly()

animal.Fly()

animal.Run()

//定义一个any类型的变量v1

var v1 interface{}

//赋值

v1 = "zhangsan"

switch v1.(type) {

case int:

case float32:

case float64:

fmt.Println("this is float64")

case string:

fmt.Println("this is string")

}

}

TA 点赞

237

987

• 推荐
• 评论
• 收藏
• 共同学习，写下你的评论

100积分直接送

0/150