Go反序列化JSON格式化时间

默认得到的序列化后的结果是 {"t":"2018-11-25T20:04:51.362485618+08:00"}, 但如果我想得到 {"t":"2018-11-25 20:04:51"} 该怎么办呢? 方法一 实现 MarshalJSON 接口, 同时可能也需要反序列化, 所以还需要实现 UnmarshalJSON, 以下代码为实现 package main import ( "encoding/json" "fmt" "time" ) type Time struct { T time.Time `json:"t,omitempty"` } func (t *Time) MarshalJSON() ([]byte, error) { type alias Time return json.Marshal(struct { *alias T string `json:"t,omitempty"` }{ alias: (*alias)(t), T: t.T.Format("2006-01-02 15:04:05"), }) } func (t *Time) UnmarshalJSON(data []byte) error { type alias Time tmp := &struct { *alias T string `json:"t,omitempty"` }{ alias: (*alias)(t), } err := json....

十一月 25, 2018 · 2 分钟 · Mioto Yaku

Go byte 数组转 string

今天遇到个问题, 如何将 [32]byte 转 string // https://play.golang.org/p/JkK_B5609GN func main() { hs := sha256.Sum256([]byte("hahaha")) fmt.Println(hs) } // ===== // [190 23 140 5 67 235 23 245 243 4 48 33 201 229 252 243 2 133 229 87 164 252 48 156 206 151 255 156 166 24 41 18] ...

三月 11, 2018 · 1 分钟 · Mioto Yaku

Go Slice 原理解析

今天被一道题目恶心到了, 发现不研究这些东西可能真的活不下去了, 狠下心来读了一个多小时的源码, 写下些自己对 Slice 的见解吧. 先说说那个题目. // https://play.golang.org/p/2fA3BylTgtf // 请问 s1 和 s2 的值分别是? func main() { s1 := []int{1, 2, 3} s2 := s1[:0] s2 = append(s2, 4) fmt.Println(s1) fmt.Println(s2) } //========== // [4 2 3] // [4] Slice 定义 先看看 Slice 在 Go 底层的定义 // https://github.com/golang/go/blob/master/src/reflect/value.go#L1806 type sliceHeader struct { Data unsafe.Pointer // Array pointer Len int // slice length Cap int // slice capacity } 原理讲解 第一行 s1 := []int{1, 2, 3} 是将 [1, 2, 3] 的首地址 存入了 Data 中, 设置了 Len 为 3, 设置了 Cap 为 3....

三月 9, 2018 · 3 分钟 · Mioto Yaku

Go HTTP JSONRPC Service

就贴一段代码… package main import ( "bytes" "errors" "io" "log" "net/http" "net/rpc" "net/rpc/jsonrpc" ) type Args struct { A, B int } type Quotient struct { Quo, Rem int } type Arith int func (t *Arith) Multiply(args *Args, reply *int) error { *reply = args.A * args.B return nil } func (t *Arith) Divide(args *Args, quo *Quotient) error { if args.B == 0 { return errors.New("divide by zero") } quo.Quo = args....

三月 6, 2018 · 2 分钟 · Mioto Yaku

基于 Go 的 RESTful API 怎么设计权限控制

其实 RESTful API 实现权限控制的方法很多很多, 比如在每个 Handler 中进行判断, 但是这种写法会导致工作量无限增加, 万一增加了其他的角色还要不停的更改源码, 所以要以尽量优雅的方式来实现这个部分. 比如 Middleware 的方式. ...

二月 22, 2018 · 2 分钟 · Mioto Yaku

使用GoBase64标准包遇到的问题

在解析 jwt 中的 Playload 部分的 base64 时遇到了错误. 报错代码 enstr := "eyJBY2NvdW50SWQiOiIxIiwiQ2xpZW50IjoiIiwiRW1haWwiOiJ5YWt1Lm1pb3RvQGdtYWlsLmNvbSIsIk1hc3RlckZsYWciOnRydWUsImV4cCI6MTU0ODc0NTY5OSwidHlwZSI6ImVtcGxveWVlcyJ9" // {"AccountId":"1","Client":"","Email":"yaku.mioto@gmail.com","MasterFlag":true,"exp":1548745699,"type":"employees"} debytes, err := base64.StdEncoding.DecodeString(enstr) if err := nil { // ... // err output: illegal base64 data at input byte xxx } // ... ...

一月 29, 2018 · 1 分钟 · Mioto Yaku

Go32位系统计算大于4GB文件sha1遇到的问题

文件大于 4GB 以下方法一定行不通, 32位操作系统 最大的寻址空间就是 4GB package main import ( "crypto/sha1" "fmt" "io/ioutil" "log" ) func main() { bytes, err := ioutil.ReadFile("file.txt") if err != nil { log.Fatal(err) } h := sha1.New() h.Write(bytes) fmt.Printf("% x", h.Sum(nil)) } 以下方法可以算出大于 4GB 文件的 sha1, 但是如果直接表面理解代码, 给人的感觉是无法运行的 io.Copy(h, f) 这里给人的感觉也是一次性读取文件到 h 变量中, “给人一种把 整个文件读取到内存的感觉” package main import ( "crypto/sha1" "fmt" "io" "log" "os" ) func main() { f, err := os.Open("file.txt") if err != nil { log....

十一月 25, 2017 · 1 分钟 · Mioto Yaku

Negroni 源码分析

negroni 用了很久很久了, 一直觉得很不错, 目前为止核心源码只有 175 行, 很适合用来学习 Go 初始化 New 将传入的 handlers 构建成链表并保存的过程 type Handler interface { ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) } // middleware 是个单向链表 type middleware struct { handler Handler next *middleware } // Negroni type Negroni struct { middleware middleware // 单向链表 handlers []Handler // 用于存储所有传入的 handler } // New 就是用来构建 middleware 链表的方法 func New(handlers ...Handler) *Negroni { return &Negroni{ handlers: handlers, middleware: build(handlers), } } 这里把传入的 handlers 保存并传给了 build 方法....

八月 9, 2017 · 2 分钟 · Mioto Yaku