03 2018 档案

MySQL5.7通用二进制包安装
摘要:5.7 安装 下载 https://downloads.mysql.com/archives/community/ 官网下载比较慢,国内镜像站可以加速下载,下载完成校验md5是否一致 http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/MySQL-5.7/ 阅读全文

posted @ 2018-03-27 01:09 cucy_to 阅读(184) 评论(0) 推荐(0) 编辑

10.7 Propagating errors with errgroup
摘要:go package main import ( "bufio" "context" "fmt" "log" "strings" "golang.org/x/sync/errgroup" ) const data = `line one line two with more words error: 阅读全文

posted @ 2018-03-27 01:06 cucy_to 阅读(214) 评论(0) 推荐(0) 编辑

10.6 Getting the fastest result from multiple sources
摘要:go package main import ( "context" "fmt" "sync" "time" ) type SearchSrc struct { ID string Delay int } func (s SearchSrc) Search(ctx context.Context) 阅读全文

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

10.5 Synchronizing goroutines with WaitGroup
摘要:go package main import "sync" import "fmt" func main() { wg := sync.WaitGroup{} for i := 0; i 阅读全文

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

10.4 Pooling resources across multiple goroutines 集中访问资源
摘要:go package main import "sync" import "fmt" import "time" type Worker struct { id string } func (w Worker) String() string { return w.id } var globalCo 阅读全文

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

10.3 Running a code block only once 只运行一次代码块
摘要:go package main import ( "fmt" "sync" "time" ) var names = []interface{}{"Alan", "Joe", "Jack", "Ben", "Ellen", "Lisa", "Carl", "Steve", "Anton", "Yo" 阅读全文

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

10.2 创建并发访问 Creating map for concurrent access
摘要:go package main import ( "fmt" "sync" ) var names = []string{"Alan", "Joe", "Jack", "Ben", "Ellen", "Lisa", "Carl", "Steve", "Anton", "Yo"} func main( 阅读全文

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

10.1 Synchronizing 同步 access to a resource with Mutex
摘要:```go package main import ( "fmt" "sync" ) var names = []string{"Alan", "Joe", "Jack", "Ben", "Ellen", "Lisa", "Carl", "Steve", "Anton", "Yo"} type Sy 阅读全文

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

9.13 form
摘要:```go package main import ( "fmt" "net/http" ) type StringServer string func (s StringServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { fmt.Printf("Prior ParseForm: %v\n", req.Form... 阅读全文

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

9.12 https
摘要:```go Prepare the private key and self-signed X-509 certificate. For this purpose, the OpenSSL utility could be used. By executing the command openssl genrsa -out server.key 2048, the private key deri... 阅读全文

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

9.11 优雅的停止http服务
摘要:```go package main import ( "context" "fmt" "log" "net/http" "os" "os/signal" "time" ) func main() { mux := http.NewServeMux() mux.HandleFunc("/", fun 阅读全文

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

9.10 cookie
摘要:```go package main import ( "fmt" "log" "net/http" "time" ) const cookieName = "X-Cookie" func main() { log.Println("Server is starting...") http.HandleFunc("/set", func(w http.ResponseWrite... 阅读全文

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

9.9 重定向
摘要:```go package main import ( "fmt" "log" "net/http" ) func main() { log.Println("Server is starting...") http.Handle("/secured/handle", http.RedirectHa 阅读全文

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

9.8 模板文件
摘要:```go ➜ recipe08 cat template.tpl Hi, I'm {{.}}! ``` ```go package main import "net/http" import "html/template" import "fmt" func main() { fmt.Println("Server is starting..."... 阅读全文

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

9.7 static file
摘要:``` ➜ recipe07 tree . . ├── html │ └── page.html ├── static.go └── welcome.txt 1 directory, 3 files ``` ```go package main import ( "net/http" ) func main() { fileSrv := http.FileServer(htt... 阅读全文

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

9.6 http中间件
摘要:```go package main import ( "io" "log" "net/http" ) type User string func (u User) toString() string { return string(u) } type AuthHandler func(u User 阅读全文

posted @ 2018-03-26 23:52 cucy_to 阅读(170) 评论(0) 推荐(0) 编辑

9.5 处理http 请求
摘要:```go package main import ( "fmt" "net/http" ) func main() { mux := http.NewServeMux() mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { ... 阅读全文

posted @ 2018-03-26 23:38 cucy_to 阅读(107) 评论(0) 推荐(0) 编辑

9.4 简单httpserver
摘要:```go package main import ( "fmt" "net/http" ) type SimpleHTTP struct{} func (s SimpleHTTP) ServeHTTP(rw http.ResponseWriter, r *http.Request) { fmt.Fprintln(rw, "Hello world") } func main() { ... 阅读全文

posted @ 2018-03-26 23:32 cucy_to 阅读(118) 评论(0) 推荐(0) 编辑

9.3 多客户端TCP
摘要:```go package main import ( "bufio" "fmt" "io" "net" ) func main() { l, err := net.Listen("tcp", ":8080") if err != nil { panic(err) } ID := 0 for { fmt.Println("Waiting for client...... 阅读全文

posted @ 2018-03-26 23:30 cucy_to 阅读(106) 评论(0) 推荐(0) 编辑

9.2 udp server
摘要:```go package main import ( "fmt" "log" "net" ) func main() { pc, err := net.ListenPacket("udp", ":7070") if err != nil { log.Fatal(err) } defer pc.Close() buffer := make([]byte, 2048) ... 阅读全文

posted @ 2018-03-26 21:48 cucy_to 阅读(109) 评论(0) 推荐(0) 编辑

资源竞争
摘要:出现原因 GO type DB struct { mutex sync.Mutex store map[string][3]float64 } func (db DB) nearest(target [3]float64) string { var filename string db.mutex. 阅读全文

posted @ 2018-03-24 12:32 cucy_to 阅读(374) 评论(0) 推荐(0) 编辑

channel
摘要:死锁 go package main import "fmt" func callerA(c chan string) { c 阅读全文

posted @ 2018-03-24 11:12 cucy_to 阅读(159) 评论(0) 推荐(0) 编辑

9.1 tcpserver
摘要:```go package main import ( "bufio" "fmt" "io" "net" ) func main() { l, err := net.Listen("tcp", ":8080") if err != nil { panic(err) } for { fmt.Println("Waiting for client...") conn... 阅读全文

posted @ 2018-03-24 00:06 cucy_to 阅读(104) 评论(0) 推荐(0) 编辑

7.11 json rpc
摘要:```go package main import ( "log" "net" "net/rpc" "net/rpc/jsonrpc" ) type Args struct { A, B int } type Result int type RpcServer struct{} func (t RpcServer) Add(args *Args, result *Result)... 阅读全文

posted @ 2018-03-24 00:04 cucy_to 阅读(80) 评论(0) 推荐(0) 编辑

7.10 smtp send email
摘要:```go package main import ( "crypto/tls" "fmt" "net/smtp" ) func main() { var email string fmt.Println("Enter username for smtp: ") fmt.Scanln(&email) var pass string fmt.Println("Enter p... 阅读全文

posted @ 2018-03-24 00:03 cucy_to 阅读(137) 评论(0) 推荐(0) 编辑

7.9 restful api
摘要:```go package main import ( "encoding/json" "fmt" "io" "io/ioutil" "net/http" "strconv" "strings" ) const addr = "localhost:7070" type City struct { I 阅读全文

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

7.8 http redirected
摘要:```go package main import ( "fmt" "net/http" ) const addr = "localhost:7070" type RedirecServer struct { redirectCount int } func (s *RedirecServer) ServeHTTP(rw http.ResponseWriter, req *htt... 阅读全文

posted @ 2018-03-24 00:00 cucy_to 阅读(184) 评论(0) 推荐(0) 编辑

7.7 设置http首部
摘要:```go package main import ( "fmt" "net/http" ) func main() { header := http.Header{} // Using the header as slice header.Set("Auth-X", "abcdef1234") header.Add("Auth-X", "defghijkl") fmt.P... 阅读全文

posted @ 2018-03-23 23:58 cucy_to 阅读(137) 评论(0) 推荐(0) 编辑

7.6 request form post
摘要:```go package main import ( "fmt" "io/ioutil" "net/http" "net/url" "strings" ) type StringServer string func (s StringServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { req.Parse... 阅读全文

posted @ 2018-03-23 23:57 cucy_to 阅读(105) 评论(0) 推荐(0) 编辑

7.5 URL 解析
摘要:```go package main import ( "encoding/json" "fmt" "net/url" ) func main() { u := &url.URL{} u.Scheme = "http" u.Host = "localhost:80" u.Path = "index.html" u.RawQuery = "id=1&name=John" u.... 阅读全文

posted @ 2018-03-23 23:55 cucy_to 阅读(142) 评论(0) 推荐(0) 编辑

7.4 http request post get
摘要:```go package main import ( "fmt" "io/ioutil" "net/http" "net/url" "strings" ) type StringServer string func (s StringServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { req.Parse... 阅读全文

posted @ 2018-03-23 23:52 cucy_to 阅读(140) 评论(0) 推荐(0) 编辑

7.3 ip host反解析
摘要:```go package main import ( "fmt" "net" ) func main() { // Resolve by IP addrs, err := net.LookupAddr("127.0.0.1") if err != nil { panic(err) } for _, addr := range addrs { fmt.Println... 阅读全文

posted @ 2018-03-23 23:50 cucy_to 阅读(113) 评论(0) 推荐(0) 编辑

7.2 tcpclient 基本web
摘要:```go package main import ( "bufio" "context" "fmt" "io" "net" "net/http" "time" ) type StringServer string func (s StringServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { rw.... 阅读全文

posted @ 2018-03-23 23:49 cucy_to 阅读(100) 评论(0) 推荐(0) 编辑

7.1 获取所有网卡ip地址
摘要:```go package main import ( "fmt" "net" ) func main() { // Get all network interfaces interfaces, err := net.Interfaces() if err != nil { panic(err) } for _, interf := range interfaces ... 阅读全文

posted @ 2018-03-23 23:46 cucy_to 阅读(254) 评论(0) 推荐(0) 编辑

6.10 获取用户基目录
摘要:```go package main import ( "fmt" "log" "os/user" ) func main() { usr, err := user.Current() if err != nil { log.Fatal(err) } fmt.Println("The user home directory: " + usr.HomeDir) } /*... 阅读全文

posted @ 2018-03-23 23:44 cucy_to 阅读(142) 评论(0) 推荐(0) 编辑

6.9 文件内容对比
摘要:```go package main import ( "bufio" "crypto/md5" "fmt" "io" "os" ) var data = []struct { name string cont string perm os.FileMode }{ {"test1.file", "Hello\nGolang is great", 0666}, {"test... 阅读全文

posted @ 2018-03-23 23:42 cucy_to 阅读(141) 评论(0) 推荐(0) 编辑

6.8 文件过滤
摘要:```go package main import ( "fmt" "os" "path/filepath" ) func main() { for i := 1; i 阅读全文

posted @ 2018-03-23 23:40 cucy_to 阅读(95) 评论(0) 推荐(0) 编辑

6.7 创建目录
摘要:```go package main import ( "os" ) func main() { f, err := os.Create("created.file") if err != nil { panic(err) } f.Close() f, err = os.OpenFile("created.byopen", os.O_CREATE|os.O_APPEND,... 阅读全文

posted @ 2018-03-23 23:38 cucy_to 阅读(80) 评论(0) 推荐(0) 编辑

6.6 修改文件权限
摘要:```go package main import ( "fmt" "os" ) func main() { f, err := os.Create("test.file") if err != nil { panic(err) } defer f.Close() // Obtain current permissions fi, err := f.Stat() ... 阅读全文

posted @ 2018-03-23 23:36 cucy_to 阅读(125) 评论(0) 推荐(0) 编辑

6.5 列出当前目录所有文件
摘要:```go package main import ( "fmt" "io/ioutil" "os" "path/filepath" ) func main() { fmt.Println("List by ReadDir") listDirByReadDir(".") fmt.Println() fmt.Println("List by Walk") listDirBy... 阅读全文

posted @ 2018-03-23 23:34 cucy_to 阅读(449) 评论(0) 推荐(1) 编辑

6.4 协程写文件
摘要:```go package main import ( "fmt" "io" "os" "sync" ) type SyncWriter struct { m sync.Mutex Writer io.Writer } func (w *SyncWriter) Write(b []byte) (n int, err error) { w.m.Lock() defe... 阅读全文

posted @ 2018-03-23 23:31 cucy_to 阅读(273) 评论(0) 推荐(0) 编辑

6.3 写文件
摘要:```go package main import ( "io" "os" "strings" ) func main() { f, err := os.Create("sample.file") if err != nil { panic(err) } defer f.Close() _, err = f.WriteString("Go is awesome!\n... 阅读全文

posted @ 2018-03-23 23:28 cucy_to 阅读(75) 评论(0) 推荐(0) 编辑

6.2 创建空目录
摘要:```go package main import "io/ioutil" import "os" import "fmt" func main() { tFile, err := ioutil.TempFile("", "gostdcookbook") if err != nil { panic(err) } // The called is responsible for h... 阅读全文

posted @ 2018-03-23 23:26 cucy_to 阅读(226) 评论(0) 推荐(0) 编辑

6.1 os 获取文件状态
摘要:```go package main import ( "fmt" "os" ) func main() { f, err := os.Open("test.file") if err != nil { panic(err) } fi, err := f.Stat() if err != nil { panic(err) } fmt.Printf("File n... 阅读全文

posted @ 2018-03-23 23:24 cucy_to 阅读(100) 评论(0) 推荐(0) 编辑

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 阅读(133) 评论(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 阅读(376) 评论(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 阅读(110) 评论(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 阅读(297) 评论(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 阅读(108) 评论(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 阅读(87) 评论(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 阅读(146) 评论(0) 推荐(0) 编辑

3.4 计算机时间元年
摘要:```go package main import ( "fmt" "time" ) func main() { // Set the epoch from int64 t := time.Unix(0, 0) fmt.Println(t) // Get the epoch // from Time instance epoch := t.Unix() fmt.Prin... 阅读全文

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

4.3 字符串格式转换成时间格式
摘要:```go package main import ( "fmt" "time" ) func main() { // If timezone is not defined // than Parse function returns // the time in UTC timezone. t, err := time.Parse("2/1/2006", "31/7/2015... 阅读全文

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

4.2 时间格式转为字符串格式
摘要:```go package main import ( "fmt" "time" ) func main() { tTime := time.Date(2017, time.March, 5, 8, 5, 2, 0, time.Local) // The formatting is done // with use of reference value // Jan 2 15:... 阅读全文

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

4.1 今天日期 time.Now
摘要:```go package main import ( "fmt" "time" ) func main() { today := time.Now() fmt.Println(today) } /* 2018-03-22 00:18:44.558974 +0800 CST m=+0.000385597 */ ``` 阅读全文

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

3.12 Checksum md5
摘要:```go package main import ( "crypto/md5" "fmt" "io" "os" ) var content = "This is content to check" func main() { checksum := MD5(content) checksum2 := FileMD5("content.dat") fmt.Printf("C... 阅读全文

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

3.9
摘要:```go package main import ( "fmt" "math/cmplx" ) func main() { // complex numbers are // defined as real and imaginary // part defined by float64 a := complex(2, 3) fmt.Printf("Real part: ... 阅读全文

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

3.8 随机数
摘要:```go package main import ( crypto "crypto/rand" "fmt" "math/big" "math/rand" ) func main() { sec1 := rand.New(rand.NewSource(10)) sec2 := rand.New(ra 阅读全文

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

3.6 二进制,十进制,十六进制转换
摘要:```go package main import ( "fmt" "strconv" ) const bin = "10111" const hex = "1A" const oct = "12" const dec = "10" const floatNum = 16.123557 func main() { // Converts binary value into hex ... 阅读全文

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

3.5
摘要:```go package main import ( "fmt" ) var integer int64 = 32500 var floatNum float64 = 22000.456 func main() { // Common way how to print the decimal // number fmt.Printf("%d \n", integer) // ... 阅读全文

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

3.4 最大浮点数?
摘要:```go package main import ( "fmt" "math" "math/big" ) const PI = const diameter = 3.0 const precision = 400 func main() { pi, _ := new(big.Float).SetP 阅读全文

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

3.3 四舍五入
摘要:```go package main import ( "fmt" "math" ) var valA float64 = 3.55554444 func main() { // Bad assumption on rounding // the number by casting it to // integer. intVal := int(valA) fmt.Print... 阅读全文

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

3.2
摘要:```go package main import ( "fmt" "math" ) const da = 0.29999999999999998889776975374843459576368331909180 const db = 0.3 func main() { daStr := fmt.S 阅读全文

posted @ 2018-03-21 23:59 cucy_to 阅读(120) 评论(0) 推荐(0) 编辑

3.1 二进制,浮点 十六进制互转十进制
摘要:```go package main import ( "fmt" "strconv" ) const bin = "00001" const hex = "2f" const intString = "12" const floatString = "12.3" func main() { // Decimals res, err := strconv.Atoi(intStrin... 阅读全文

posted @ 2018-03-21 23:56 cucy_to 阅读(282) 评论(0) 推荐(0) 编辑

2.12 指定缩进
摘要:```go package main import ( "fmt" "strconv" "strings" "unicode" ) func main() { text := "Hi! Go is awesome." text = Indent(text, 6) fmt.Println(text) text = Unindent(text, 3) fmt.Println... 阅读全文

posted @ 2018-03-21 23:53 cucy_to 阅读(104) 评论(0) 推荐(0) 编辑

2.11 whitespace 去掉空格
摘要:```go package main import ( "fmt" "math" "regexp" "strconv" "strings" ) func main() { stringToTrim := "\t\t\n Go \tis\t Awesome \t\t" trimResult := strings.TrimSpace(stringToTrim) fmt.Pr... 阅读全文

posted @ 2018-03-21 23:52 cucy_to 阅读(133) 评论(0) 推荐(0) 编辑

2.10 csv
摘要:data.csv un_comment.csv 阅读全文

posted @ 2018-03-21 23:50 cucy_to 阅读(100) 评论(0) 推荐(0) 编辑

2.9 字母大小写
摘要:```go package main import ( "fmt" "strings" "unicode" ) const email = "ExamPle@domain.com" const name = "isaac newton" const upc = "upc" const i = "i" const snakeCase = "first_name" func main(... 阅读全文

posted @ 2018-03-21 23:44 cucy_to 阅读(130) 评论(0) 推荐(0) 编辑

2.8 decode encode
摘要:cat /Users/zrd/Downloads/gostandardlib/Chapter02/Strings\ and\ Things/recipe08/win1250.txt Gda�sk% decode 阅读全文

posted @ 2018-03-21 23:43 cucy_to 阅读(79) 评论(0) 推荐(0) 编辑

2.7 正则匹配字符串
摘要:```go package main import ( "fmt" "regexp" ) const refString = `[{ \"email\": \"email@example.com\" \"phone\": 555467890}, { \"email\": \"other@domain 阅读全文

posted @ 2018-03-21 23:24 cucy_to 阅读(101) 评论(0) 推荐(0) 编辑

2.6 字符串替换
摘要:正则替换 阅读全文

posted @ 2018-03-21 23:21 cucy_to 阅读(99) 评论(0) 推荐(0) 编辑

2.5 tabwriter 格式化输出
摘要:```go package main import ( "fmt" "os" "text/tabwriter" ) func main() { w := tabwriter.NewWriter(os.Stdout, 15, 0, 1, ' ', tabwriter.AlignRight) fmt.Fprintln(w, "username\tfirstname\tlastname\... 阅读全文

posted @ 2018-03-21 23:17 cucy_to 阅读(175) 评论(0) 推荐(0) 编辑

2.4 Buffer
摘要:性能测试 go package bench import ( "bytes" "testing" ) const testString = "test" func BenchmarkConcat(b testing.B) { var str string b.ResetTimer() for n : 阅读全文

posted @ 2018-03-21 23:05 cucy_to 阅读(111) 评论(0) 推荐(0) 编辑

2.3 字符串链接
摘要:## ```go package main import ( "fmt" "strings" ) const selectBase = "SELECT * FROM user WHERE %s " var refStringSlice = []string{ " FIRST_NAME = 'Jack' ", " INSURANCE_NO = 333444555 ", " EFFEC... 阅读全文

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

2.2 去除字符串特别字符
摘要:遍历带空格的字符串 指定分割的字符串 去除特别符号 正则去掉特别字符串 阅读全文

posted @ 2018-03-21 22:34 cucy_to 阅读(114) 评论(0) 推荐(0) 编辑

2.1 字符串查询
摘要:```go package main import ( "fmt" "strings" ) const refString = "Mary had a little lamb" func main() { lookFor := "lamb" contain := strings.Contains(refString, lookFor) fmt.Printf("The \"%s\"... 阅读全文

posted @ 2018-03-21 22:22 cucy_to 阅读(102) 评论(0) 推荐(0) 编辑

存储数据_文件读写
摘要:数据存储 存储在内存中 读写文件 CSV gob 二进制文件 阅读全文

posted @ 2018-03-21 19:14 cucy_to 阅读(135) 评论(0) 推荐(0) 编辑

template模板
摘要:模板引擎 无逻辑模板引擎, 嵌入逻辑模板引擎 简单的模板 goweb模板引擎需要两个步骤,1对文本的模板源进行语法分析,创建一个经过语法分析的模板结构,其中模板源可以是一个字符串,也可以是模板文件包含的内容;(2)经过执行语法分析的模板,将ResponseWriter和模板所需的动态数据传给模板引擎 阅读全文

posted @ 2018-03-21 16:07 cucy_to 阅读(637) 评论(0) 推荐(0) 编辑

request response
摘要:Request 结构 组成: URL字段 Header字段 空行 Body字段 Form字段、postform字段,multipartForm字段 请求Url ·结构· URL格式一般为 request Header request Body Form POST FILE 方法2 response 阅读全文

posted @ 2018-03-21 10:46 cucy_to 阅读(123) 评论(0) 推荐(0) 编辑

1.12 配置文件读取
摘要:config.json go package main import ( "encoding/json" "fmt" "os" ) type Client struct { consulIP string connString string } func (c Client) String() st 阅读全文

posted @ 2018-03-17 23:16 cucy_to 阅读(119) 评论(0) 推荐(0) 编辑

1.11 程序优雅退出
摘要:```go package main import ( "fmt" "io" "log" "os" "os/signal" "syscall" "time" ) var writer os.File func main() { // The file is opened as // a log fi 阅读全文

posted @ 2018-03-17 23:11 cucy_to 阅读(98) 评论(0) 推荐(0) 编辑

1.10 read.wirte from stdin stdout the child process
摘要:stdout 标准输入到标准输出 阅读全文

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

1.9 进程pid,运行耗时 运行退出状态
摘要:```go package main import ( "fmt" "os/exec" "runtime" "time" ) func main() { var cmd string if runtime.GOOS == "windows" { cmd = "timeout" } else { cmd = "sleep" } proc := exec.Comman... 阅读全文

posted @ 2018-03-17 22:42 cucy_to 阅读(148) 评论(0) 推荐(0) 编辑

1.8 执行命令
摘要:2 阅读全文

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

1.7获取os信号
摘要:```go package main import ( "fmt" "os" "os/signal" "syscall" ) func main() { // Create the channel where the received // signal would be sent. The Not 阅读全文

posted @ 2018-03-17 22:22 cucy_to 阅读(120) 评论(0) 推荐(0) 编辑

1.6 获取程序运行的pid
摘要:```go package main import ( "fmt" "os" "os/exec" "strconv" ) func main() { pid := os.Getpid() fmt.Printf("Process PID: %d \n\n", pid) prc := exec.Command("ps", "-p", strconv.Itoa(pid), "-v"... 阅读全文

posted @ 2018-03-17 22:16 cucy_to 阅读(198) 评论(0) 推荐(0) 编辑

1.5 获取文件路径
摘要:```go package main import ( "fmt" "os" "path/filepath" ) func main() { ex, err := os.Executable() if err != nil { panic(err) } // Path to executable file fmt.Println("绝对路径: ", ex) // 绝对路径... 阅读全文

posted @ 2018-03-17 22:11 cucy_to 阅读(134) 评论(0) 推荐(0) 编辑

1.4 用os环境变量里获取值
摘要:1 2 不存在则报错 3 设置获取删除环境变量 阅读全文

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

1.3命令行参数获取 flag
摘要:```go package main import ( "flag" "fmt" "log" "os" "strings" ) // Custom type need to implement // flag.Value interface to be able to // use it in fl 阅读全文

posted @ 2018-03-17 21:55 cucy_to 阅读(875) 评论(0) 推荐(0) 编辑

1.2获取命令行参数
摘要:```go package main import ( "fmt" "os" ) func main() { args := os.Args // all command line args println(args) // The first argument, zero item from array, // is the name of the called binar... 阅读全文

posted @ 2018-03-17 21:41 cucy_to 阅读(133) 评论(0) 推荐(0) 编辑

1.1获取go运行版本信息
摘要:```go package main import ( "log" "runtime" ) const info = ` Application %s starting. The binary was build by GO: %s` func main() { log.Printf(info, " 阅读全文

posted @ 2018-03-17 21:25 cucy_to 阅读(1250) 评论(0) 推荐(0) 编辑

11.exporting 导出
摘要:Declare and access exported identifiers Pkg Declare and access exported identifiers Main Declare unexported identifiers and restrictions Pkg ( Declare 阅读全文

posted @ 2018-03-17 19:10 cucy_to 阅读(169) 评论(0) 推荐(0) 编辑

10.functions
摘要:Return multiple values go // Sample program to show how functions can return multiple values while using // named and struct types. package main impor 阅读全文

posted @ 2018-03-17 18:57 cucy_to 阅读(141) 评论(0) 推荐(0) 编辑

9.interface
摘要:method set go package main import "fmt" // Sample program to show how polymorphic behavior with interfaces. // 示例程序演示多态行为与接口的关系。 // reader is an inter 阅读全文

posted @ 2018-03-17 17:38 cucy_to 阅读(156) 评论(0) 推荐(0) 编辑

8. Embedding 嵌入
摘要:Declaring Fields Embedding types Embedded types and interfaces Outer and inner type interface implementations 外部和内部类型接口实现。 练习 阅读全文

posted @ 2018-03-17 01:19 cucy_to 阅读(144) 评论(0) 推荐(0) 编辑

7.pointers
摘要:Sharing data I Sharing data II Escape Analysis 溢出分析 练习 阅读全文

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

6.struct_types
摘要:Declare, create and initialize struct types Anonymous struct types Named vs Unnamed types 命名类型和匿名类型 结构体类型排列 阅读全文

posted @ 2018-03-16 23:47 cucy_to 阅读(177) 评论(0) 推荐(0) 编辑

5.map
摘要:Declare, initialize and iterate Map literals and delete Map key restrictions key类型 阅读全文

posted @ 2018-03-16 23:12 cucy_to 阅读(127) 评论(0) 推荐(0) 编辑

4.slice
摘要:Reference Types Taking slices of slices Appending slices go // Sample program to show how to grow a slice using the built in function append // and ho 阅读全文

posted @ 2018-03-16 20:57 cucy_to 阅读(148) 评论(0) 推荐(0) 编辑

3.constants
摘要:... iota go // Sample program to show how iota works. package main import "fmt" func main() { const ( A1 = iota // 0 : Start at 0 B1 = iota // 1 : Inc 阅读全文

posted @ 2018-03-16 00:40 cucy_to 阅读(227) 评论(0) 推荐(0) 编辑

2.arrays
摘要:```go package main import "fmt" func main() { // Declare an array of five strings that is initialized // to its zero value. var strings [5]string stri 阅读全文

posted @ 2018-03-15 23:53 cucy_to 阅读(103) 评论(0) 推荐(0) 编辑

1.go变量
摘要:练习 阅读全文

posted @ 2018-03-15 23:04 cucy_to 阅读(221) 评论(0) 推荐(0) 编辑

导航

点击右上角即可分享
微信分享提示