摘要:```go package main import ( "fmt" "sync" ) //WaitGroup用于等待一组线程的结束。父线程调用Add方法来设定应等待的线程的数量。每个被等待的线程在结束时应调用Done方法。同时,主线程里可以调用Wait方法阻塞至所有线程结束 func main()
阅读全文
摘要:```go package main import ( "encoding/json" "fmt" "os" ) type Response1 struct { Page int Fruits []string } type Response2 struct { Page int Fruits []
阅读全文
摘要:```go
package main import ( "fmt" "os"
) type point struct { x, y int
} func main() { p := point{1, 2} fmt.Printf("%v\n", p) fmt.Printf("%+v\n", p) fmt.Printf("%#v\n", p) fmt.Printf("%T\n",...
阅读全文
摘要:```go
package main import ( "fmt" s "strings"
) var p = fmt.Println func main() { //strings标准库包含的函数 p("Contains: ", s.Contains("test", "es")) p("Count:", s.Count("test", "t")) p("HasPrefix", ...
阅读全文
摘要:```go
package main import ( "fmt" "strings"
) func Index(vs []string, t string) int { for i, v := range vs { if v == t { return i } } return -1
} func Include(vs []string, t string) boo...
阅读全文
摘要:```go
package main import ( "fmt" "os"
) func main() { f := createFile("/tmp/defer.txt") //在函数退出时调用defer defer closeFile(f) writeFile(f)
} func createFile(p string) *os.File { fmt.Println("c...
阅读全文
摘要:```go
package main import "os" func main() { //panic会中断程序执行,在此处一直往上抛panic,需要上游的recover来捕获 panic("a problem") _, err := os.Create("/tmp/file") if err != nil { panic(err) }
}
```
阅读全文
摘要:```go package main import ( "fmt" "sort" ) //定义类型别名 type ByLength []string func (s ByLength) Len() int { return len(s) } func (s ByLength) Swap(i, j i
阅读全文
摘要:```go
package main import ( "fmt" "sort"
) func main() { strs := []string{"c", "a", "b"} //排序函数 sort.Strings(strs) fmt.Println("Strings:", strs) ints := []int{7, 2, 4} //排序函数 sort.Ints(ints...
阅读全文
摘要:```go package main import ( "fmt" "math/rand" "sync/atomic" "time" ) type readOp struct { key int resp chan int } type writeOp struct { key int val in
阅读全文
摘要:```go package main import ( "fmt" "math/rand" "runtime" "sync" "sync/atomic" "time" ) func main() { var state = make(map[int]int) var mutex = &sync.Mu
阅读全文
摘要:```go package main import ( "fmt" "runtime" "sync/atomic" "time" ) func main() { var ops uint64 = 0 for i := 0; i
阅读全文
摘要:```go package main import ( "fmt" "time" ) func main() { requests := make(chan int, 5) for i := 1; i
阅读全文
摘要:```go package main import ( "fmt" "time" ) func main() { jobs :=make(chan int,100) results := make(chan int,100) //启动3个协程 for w:=1;w
阅读全文
摘要:```go
package main import ( "time" "fmt"
) func main() { // 定时器 是当你想要在未来某一刻执行一次时使用的 - 打点器 // 则是当你想要在固定的时间间隔重复执行准备的。这里是一个打点器的例子, // 它将定时的执行,直到我们将它停止。 ticker := time.NewTicker(time.Millisecond*5...
阅读全文
摘要:```go package main import ( "fmt" "time" ) func main() { //定时器2s timer1 := time.NewTimer(time.Second 2) //读取通道,阻塞2s
阅读全文
摘要:```go package main import ( "fmt" ) func main() { queue := make(chan string, 2) queue
阅读全文
摘要:```go package main import ( "fmt" ) func main() { jobs := make(chan int, 5) done := make(chan bool) go func() { for { //读取通道方式, val,ok :=
阅读全文
摘要:```go package main import ( "fmt" ) func main() { messages := make(chan string) signals := make(chan bool) //常规的通过通道发送和接收数据是阻塞的。然而,我们可以使用带一个 default子句
阅读全文
摘要:```go package main import ( "fmt" "time" ) func main() { c1 := make(chan string, 1) go func() { time.Sleep(time.Second 2) c1
阅读全文