商君

导航

2018年10月16日 #

Go Example--错误处理

摘要: ```go package main import ( "errors" "fmt" ) //定义一种错误类型 type argError struct { arg int prob string } //实现错误方法 func (e *argError) Error()string { return fmt.Sprintf("%d - %s",e.arg, e.prob) } ... 阅读全文

posted @ 2018-10-16 19:32 漫步者01 阅读(113) 评论(0) 推荐(0) 编辑

Go Example--接口

摘要: ```go package main import ( "math" "fmt" ) type geometry interface { area() float64 perim() float64 } //rect实现接口 type rect struct { width,height float64 } //cicle实现接口 type cicle struct { radi... 阅读全文

posted @ 2018-10-16 19:11 漫步者01 阅读(80) 评论(0) 推荐(0) 编辑

Go Example--方法

摘要: ```go package main import "fmt" //定义结构体 type rect struct { width,height int } //定义结构体指针的方法 func (r *rect) area() int { return r.width*r.height } //定义结构体的方法 func (r rect)perim() int { return 2*r... 阅读全文

posted @ 2018-10-16 15:53 漫步者01 阅读(149) 评论(0) 推荐(0) 编辑

Go Example--结构体

摘要: ```go package main import "fmt" //定义一个私有结构体 type person struct { name string age int } func main() { //结构体的初始化 fmt.Println(person{"Bob",20}) fmt.Println(person{name:"Alice",age:20}) fmt.Prin... 阅读全文

posted @ 2018-10-16 15:48 漫步者01 阅读(82) 评论(0) 推荐(0) 编辑

Go Example--指针

摘要: ```go package main import ( "fmt" ) func zeroval(ival int) { ival = 0 } func zeroptr(iptr *int) { *iptr = 0 } func main() { i:=1 fmt.Println("initial:",i) //函数是值传递,i发生了复制,所以不会修改原始i的值 zer... 阅读全文

posted @ 2018-10-16 15:43 漫步者01 阅读(96) 评论(0) 推荐(0) 编辑