商君

导航

2018年10月30日 #

go-json处理的问题

摘要: 1、通过Decoder来解析json串 go package main import ( "encoding/json" "fmt" "io" "log" "strings" ) func main() { const jsonStream = ` {"Name":"Ed","Text":"Knoc 阅读全文

posted @ 2018-10-30 19:24 漫步者01 阅读(444) 评论(0) 推荐(0) 编辑

Go断言

摘要: golang的语言中提供了断言的功能。golang中的所有程序都实现了interface{}的接口,这意味着,所有的类型如string,int,int64甚至是自定义的struct类型都就此拥有了interface{}的接口,这种做法和java中的Object类型比较类似。那么在一个数据通过func 阅读全文

posted @ 2018-10-30 11:38 漫步者01 阅读(347) 评论(0) 推荐(0) 编辑

2018年10月26日 #

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 阅读(84) 评论(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 阅读(68) 评论(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 阅读(66) 评论(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 阅读(75) 评论(0) 推荐(0) 编辑

2018年10月23日 #

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 阅读(101) 评论(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 阅读(415) 评论(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 阅读(111) 评论(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 阅读(227) 评论(0) 推荐(0) 编辑