摘要:
package main import ( "fmt" "math" ) type Abser interface { Abs() float64 Abs2() float64 } func main() { var a Abser f := MyFloat(-math.Sqrt2) v := Ve 阅读全文
摘要:
package main import ( "fmt" "sync" "time" ) // SafeCounter is safe to use concurrently. type SafeCounter struct { mu sync.Mutex v map[string]int } // 阅读全文
摘要:
package main import ( "golang.org/x/tour/tree" "fmt" ) // Walk walks the tree t sending all values // from the tree to the channel ch. func Walk(t *tr 阅读全文
摘要:
select语句让goroutine等待多个通信操作。 select会一直阻塞,直到其中一个case可以运行,然后执行那个case。如果有多个就绪,它就随机选择一个。 package main import "fmt" func fibonacci(c, quit chan int) { x, y 阅读全文
摘要:
无缓冲通道(阻塞通道) 写入后立即阻塞,需要另一个协程读取通道的数据后,才能继续执行。 通道操作符 ch <- v // Send v to channel ch. v := <-ch // Receive from ch, and // assign value to v. 无缓冲通道 ch := 阅读全文
摘要:
package main import ( "golang.org/x/tour/pic" "image/color" "image" ) type Image struct{} func (i Image) ColorModel() color.Model { return color.RGBAM 阅读全文
摘要:
package main import ( "io" "os" "strings" ) type rot13Reader struct { r io.Reader } func (rot rot13Reader) Read (b []byte) (int, error){ n, error := r 阅读全文
摘要:
package main import "golang.org/x/tour/reader" type MyReader struct{} // TODO: Add a Read([]byte) (int, error) method to MyReader. func (m MyReader) R 阅读全文
摘要:
Go程序用error表示错误状态。 error类型是一个类似fmt.Stringer的内置接口 type error interface { Error() string } (与fmt.Stringer, fmt包在打印值错误的值时查找error接口。) 函数通常返回一个error,调用代码应该通 阅读全文
摘要:
package main import ( "fmt" "strings" "strconv" ) type IPAddr [4]byte func (ip IPAddr) String() string{ array := make([]string, len(ip)) for i, b := r 阅读全文