2018年3月22日

5.13 json

摘要: ```go package main import ( "encoding/json" "fmt" "strings" ) const js = ` [{ "name":"Axel", "lastname":"Fooley" }, { "name":"Tim", "lastname":"Burton 阅读全文

posted @ 2018-03-22 01:13 cucy_to 阅读(113) 评论(0) 推荐(0) 编辑

5.11 rw zip file

摘要: ```go package main import ( "archive/zip" "bytes" "fmt" "io" "io/ioutil" "log" "os" ) func main() { var buff bytes.Buffer // Compress content zipW := zip.NewWriter(&buff) f, err := zip... 阅读全文

posted @ 2018-03-22 01:08 cucy_to 阅读(129) 评论(0) 推荐(0) 编辑

5.10 gob序列化

摘要: ```go package main import ( "bytes" "encoding/gob" "fmt" ) type User struct { FirstName string LastName string Age int Active bool } func (u User) Str 阅读全文

posted @ 2018-03-22 01:07 cucy_to 阅读(132) 评论(0) 推荐(0) 编辑

5.9 piping between writer and reader

摘要: ```go package main import ( "io" "log" "os" "os/exec" ) func main() { pReader, pWriter := io.Pipe() cmd := exec.Command("echo", "Hello Go!\nThis is example") cmd.Stdout = pWriter go func(... 阅读全文

posted @ 2018-03-22 01:05 cucy_to 阅读(159) 评论(0) 推荐(0) 编辑

5.7 io.MultiWriter(buf, f)

摘要: ```go package main import "io" import "bytes" import "os" import "fmt" func main() { buf := bytes.NewBuffer([]byte{}) f, err := os.OpenFile("sample.txt", os.O_CREATE|os.O_RDWR, os.ModePerm) if e... 阅读全文

posted @ 2018-03-22 01:02 cucy_to 阅读(374) 评论(0) 推荐(1) 编辑

5.7 读写 二进制数据

摘要: ```go package main import ( "bytes" "encoding/binary" "fmt" ) func main() { // Writing binary values buf := bytes.NewBuffer([]byte{}) if err := binary.Write(buf, binary.BigEndian, 1.004); er... 阅读全文

posted @ 2018-03-22 00:59 cucy_to 阅读(125) 评论(0) 推荐(0) 编辑

5.6 seeking a position within a file 查找文件中的位置

摘要: ```go package main import ( "errors" "fmt" "os" ) const lineLegth = 25 func main() { f, e := os.OpenFile("flatfile.txt", os.O_RDWR|os.O_CREATE, os.ModePerm) if e != nil { panic(e) } defe... 阅读全文

posted @ 2018-03-22 00:57 cucy_to 阅读(153) 评论(0) 推荐(0) 编辑

5.5 r/w a different charset

摘要: ```go package main import ( "fmt" "io/ioutil" "os" "golang.org/x/text/encoding/charmap" ) func main() { // Write the string // encoded to Windows-1252 encoder := charmap.Windows1252.NewEnc... 阅读全文

posted @ 2018-03-22 00:54 cucy_to 阅读(109) 评论(0) 推荐(0) 编辑

5.4 readfile to a string

摘要: ```go package main import "os" import "bufio" import "bytes" import "fmt" import "io/ioutil" func main() { fmt.Println("### Read as reader ###") f, err := os.Open("temp/file.txt") if err != ni... 阅读全文

posted @ 2018-03-22 00:51 cucy_to 阅读(116) 评论(0) 推荐(0) 编辑

5.3 os.open 读写文件

摘要: ```go package main import ( "fmt" "io" "io/ioutil" "os" ) func main() { f, err := os.Open("temp/file.txt") if err != nil { panic(err) } c, err := ioutil.ReadAll(f) if err != nil { pa... 阅读全文

posted @ 2018-03-22 00:48 cucy_to 阅读(295) 评论(0) 推荐(0) 编辑

5.2 标准输出和错误输出

摘要: ```go package main import ( "fmt" "io" "os" ) func main() { // Simply write string io.WriteString(os.Stdout, "This is string to standard output.\n") i 阅读全文

posted @ 2018-03-22 00:45 cucy_to 阅读(124) 评论(0) 推荐(0) 编辑

5.1 标准输入 NewScanner

摘要: ```go package main import ( "fmt" ) func main() { var name string fmt.Println("What is your name?") fmt.Scanf("%s\n", &name) var age int fmt.Println("What is your age?") fmt.Scanf("%d\n", &... 阅读全文

posted @ 2018-03-22 00:43 cucy_to 阅读(139) 评论(0) 推荐(0) 编辑

4.12 序列化

摘要: ```go package main import ( "encoding/json" "fmt" "time" ) func main() { eur, err := time.LoadLocation("Europe/Vienna") if err != nil { panic(err) } t 阅读全文

posted @ 2018-03-22 00:37 cucy_to 阅读(91) 评论(0) 推荐(0) 编辑

4.11 timeout

摘要: ```go package main import ( "fmt" "time" ) func main() { to := time.After(3 time.Second) list := make([]string, 0) done := make(chan bool, 1) fmt.Prin 阅读全文

posted @ 2018-03-22 00:36 cucy_to 阅读(107) 评论(0) 推荐(0) 编辑

3.10 计算代码运行的时长

摘要: ```go package main import ( "fmt" "sync" "time" ) func main() { t := time.NewTimer(3 time.Second) fmt.Printf("Start waiting at %v\n", time.Now().Forma 阅读全文

posted @ 2018-03-22 00:35 cucy_to 阅读(113) 评论(0) 推荐(0) 编辑

3.9 计算器 使用协程

摘要: ```go package main import ( "fmt" "os" "os/signal" "time" ) func main() { c := make(chan os.Signal, 1) signal.Notify(c) ticker := time.NewTicker(time. 阅读全文

posted @ 2018-03-22 00:32 cucy_to 阅读(115) 评论(0) 推荐(0) 编辑

3.8 时区转换

摘要: ```go package main import ( "fmt" "time" ) func main() { eur, err := time.LoadLocation("Europe/Vienna") if err != nil { panic(err) } t := time.Date(2000, 1, 1, 0, 0, 0, 0, eur) fmt.Printf... 阅读全文

posted @ 2018-03-22 00:31 cucy_to 阅读(95) 评论(0) 推荐(0) 编辑

3.7 两个时间差

摘要: ```go package main import ( "fmt" "time" ) func main() { l, err := time.LoadLocation("Asia/Shanghai") if err != nil { panic(err) } t := time.Date(2000, 1, 1, 0, 0, 0, 0, l) t2 := time.Da... 阅读全文

posted @ 2018-03-22 00:30 cucy_to 阅读(85) 评论(0) 推荐(0) 编辑

3.6 时间加减

摘要: ```go package main import ( "fmt" "time" ) func main() { l, err := time.LoadLocation("Asia/Shanghai") if err != nil { panic(err) } t := time.Date(2017, 11, 30, 11, 10, 20, 0, l) fmt.Print... 阅读全文

posted @ 2018-03-22 00:29 cucy_to 阅读(96) 评论(0) 推荐(0) 编辑

3.5 本地时间转换 获取星期几

摘要: ```go package main import ( "fmt" "time" ) func main() { t := time.Date(2017, 11, 29, 21, 0, 0, 0, time.Local) fmt.Printf("Extracting units from: %v\n", t) dOfMonth := t.Day() weekDay := t.W... 阅读全文

posted @ 2018-03-22 00:27 cucy_to 阅读(145) 评论(0) 推荐(0) 编辑

导航