商君

导航

随笔分类 -  Go基础实例学习

1 2 3 下一页

通过代码学习Golang基础知识
Go - WaitGroup
摘要:```go package main import ( "fmt" "sync" ) //WaitGroup用于等待一组线程的结束。父线程调用Add方法来设定应等待的线程的数量。每个被等待的线程在结束时应调用Done方法。同时,主线程里可以调用Wait方法阻塞至所有线程结束 func main() 阅读全文

posted @ 2018-11-15 20:15 漫步者01 阅读(130) 评论(0) 推荐(0)

Go Example--json
摘要:```go package main import ( "encoding/json" "fmt" "os" ) type Response1 struct { Page int Fruits []string } type Response2 struct { Page int Fruits [] 阅读全文

posted @ 2018-10-31 20:31 漫步者01 阅读(119) 评论(0) 推荐(0)

Go Example--格式化字符串
摘要:```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",... 阅读全文

posted @ 2018-10-26 20:22 漫步者01 阅读(91) 评论(0) 推荐(0)

Go Example--strings
摘要:```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", ... 阅读全文

posted @ 2018-10-26 20:13 漫步者01 阅读(73) 评论(0) 推荐(0)

Go Example--组合函数
摘要:```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... 阅读全文

posted @ 2018-10-26 20:02 漫步者01 阅读(72) 评论(0) 推荐(0)

Go Example--defer
摘要:```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... 阅读全文

posted @ 2018-10-26 14:28 漫步者01 阅读(84) 评论(0) 推荐(0)

Go Example--panic
摘要:```go package main import "os" func main() { //panic会中断程序执行,在此处一直往上抛panic,需要上游的recover来捕获 panic("a problem") _, err := os.Create("/tmp/file") if err != nil { panic(err) } } ``` 阅读全文

posted @ 2018-10-23 16:07 漫步者01 阅读(104) 评论(0) 推荐(0)

Go Example--自定义排序
摘要:```go package main import ( "fmt" "sort" ) //定义类型别名 type ByLength []string func (s ByLength) Len() int { return len(s) } func (s ByLength) Swap(i, j i 阅读全文

posted @ 2018-10-23 15:57 漫步者01 阅读(420) 评论(0) 推荐(0)

Go Example--排序
摘要:```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... 阅读全文

posted @ 2018-10-23 15:46 漫步者01 阅读(120) 评论(0) 推荐(0)

Go Example--状态协程
摘要:```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 阅读全文

posted @ 2018-10-23 15:41 漫步者01 阅读(234) 评论(0) 推荐(0)

Go Example--锁
摘要:```go package main import ( "fmt" "math/rand" "runtime" "sync" "sync/atomic" "time" ) func main() { var state = make(map[int]int) var mutex = &sync.Mu 阅读全文

posted @ 2018-10-22 19:57 漫步者01 阅读(110) 评论(0) 推荐(0)

Go Example--原子计数器
摘要:```go package main import ( "fmt" "runtime" "sync/atomic" "time" ) func main() { var ops uint64 = 0 for i := 0; i 阅读全文

posted @ 2018-10-22 19:44 漫步者01 阅读(359) 评论(0) 推荐(0)

Go Example--限速
摘要:```go package main import ( "fmt" "time" ) func main() { requests := make(chan int, 5) for i := 1; i 阅读全文

posted @ 2018-10-22 19:35 漫步者01 阅读(124) 评论(0) 推荐(0)

Go Example--工作池
摘要:```go package main import ( "fmt" "time" ) func main() { jobs :=make(chan int,100) results := make(chan int,100) //启动3个协程 for w:=1;w 阅读全文

posted @ 2018-10-19 17:15 漫步者01 阅读(92) 评论(0) 推荐(0)

Go Example--打点器
摘要:```go package main import ( "time" "fmt" ) func main() { // 定时器 是当你想要在未来某一刻执行一次时使用的 - 打点器 // 则是当你想要在固定的时间间隔重复执行准备的。这里是一个打点器的例子, // 它将定时的执行,直到我们将它停止。 ticker := time.NewTicker(time.Millisecond*5... 阅读全文

posted @ 2018-10-19 16:52 漫步者01 阅读(155) 评论(0) 推荐(0)

Go Example--定时器
摘要:```go package main import ( "fmt" "time" ) func main() { //定时器2s timer1 := time.NewTimer(time.Second 2) //读取通道,阻塞2s 阅读全文

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

Go Example--通道遍历
摘要:```go package main import ( "fmt" ) func main() { queue := make(chan string, 2) queue 阅读全文

posted @ 2018-10-19 16:27 漫步者01 阅读(129) 评论(0) 推荐(0)

Go Example--关闭通道
摘要:```go package main import ( "fmt" ) func main() { jobs := make(chan int, 5) done := make(chan bool) go func() { for { //读取通道方式, val,ok := 阅读全文

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

Go Example--通道非阻塞
摘要:```go package main import ( "fmt" ) func main() { messages := make(chan string) signals := make(chan bool) //常规的通过通道发送和接收数据是阻塞的。然而,我们可以使用带一个 default子句 阅读全文

posted @ 2018-10-19 15:45 漫步者01 阅读(128) 评论(0) 推荐(0)

Go Example--超时处理
摘要:```go package main import ( "fmt" "time" ) func main() { c1 := make(chan string, 1) go func() { time.Sleep(time.Second 2) c1 阅读全文

posted @ 2018-10-19 15:22 漫步者01 阅读(110) 评论(0) 推荐(0)

1 2 3 下一页