商君

导航

2018年10月19日 #

Go Example--通道选择器

摘要: ```go package main import ( "fmt" "time" ) func main() { c1 := make(chan string) c2 := make(chan string) go func() { time.Sleep(time.Second 1) c1 阅读全文

posted @ 2018-10-19 15:13 漫步者01 阅读(93) 评论(0) 推荐(0) 编辑

Go Example--通道方向

摘要: ```go package main import "fmt" func main() { pings := make(chan string, 1) pongs := make(chan string, 1) ping(pings, "passwd message") pong(pings, po 阅读全文

posted @ 2018-10-19 15:07 漫步者01 阅读(130) 评论(0) 推荐(0) 编辑

2018年10月17日 #

Go Example--通道同步

摘要: ```go package main import ( "fmt" "time" ) func main() { //缓存通道 done := make(chan bool,1) go worker(done) //等待读取chan,当chan没数据时会阻塞 阅读全文

posted @ 2018-10-17 19:58 漫步者01 阅读(85) 评论(0) 推荐(0) 编辑

Go Example--缓存通道

摘要: ```go package main import "fmt" func main() { //缓存通道 msg := make(chan string,2) msg 阅读全文

posted @ 2018-10-17 19:54 漫步者01 阅读(104) 评论(0) 推荐(0) 编辑

Go Example--通道

摘要: ```go package main import "fmt" func main() { //string类型通道 messages := make(chan string) //往通道写入数据 go func() {messages 阅读全文

posted @ 2018-10-17 10:20 漫步者01 阅读(61) 评论(0) 推荐(0) 编辑

Go Example--协程

摘要: ```go package main import "fmt" func main() { //main gorouting中调用f函数 f("direct") //重新建一个goroutine执行f函数 go f("goroutine") //重新建一个goroutine执行函数 go func( 阅读全文

posted @ 2018-10-17 10:18 漫步者01 阅读(93) 评论(0) 推荐(0) 编辑

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) 编辑