golang JSON

golang JSON read field from json import "github.com/tidwall/gjson" gjson.Get(jsonStr, "foo.bar.status").String() go json string 格式化 var str bytes.Buffer _ = json.Indent(&str, []byte(data), "", " ") fmt.Println("formated: ", str.String()) time.Time 序列化 https://www.cnblogs.com/chenqionghe/p/13409556.html json.Marshal(struct { *User Password bool `json:"password,omitempty"` }{ User: user, }) json.Marshal() // 序列化 + 格式化 resp2, _ := json.MarshalIndent(s, "", " ") json.Unmarshal() struct json tag https://colobu.com/2017/06/21/json-tricks-in-Go/ type Result struct { Count int `json:"count"` Data MyStruct `json:"data,omitempty"` } func main() { out := shellExec(shell) var result Result json.Unmarshal([]byte(out), &result) fmt.Println(result.Count) } gjson https://github.com/tidwall/gjson ...

2012-04-25 · 3 min · 553 words · -

Go context

Go context 什么是 context Go 1.7 标准库引入 context,中文译作“上下文”,准确说它是 goroutine 的上下文,包含 goroutine 的运行状态、环境、现场等信息。 context 主要用来在 goroutine 之间传递上下文信息,包括:取消信号、超时时间、截止时间、k-v 等。 随着 context 包的引入,标准库中很多接口因此加上了 context 参数,例如 database/sql 包。context 几乎成为了并发控制和超时控制的标准做法。 context.Context 类型的值可以协调多个 groutine 中的代码执行“取消”操作,并且可以存储键值对。最重要的是它是并发安全的。 与它协作的 API 都可以由外部控制执行“取消”操作,例如:取消一个 HTTP 请求的执行。 https://zhuanlan.zhihu.com/p/68792989 https://www.36kr.com/p/1721518997505

2012-03-19 · 1 min · 36 words · -

go math, 数学计算

go math, 数学计算 除法 package main import "fmt" func main() { fmt.Println(1/2) fmt.Println(2/2) fmt.Println(3/2) fmt.Println(4/2) } 求余, 取模 package main import "fmt" func main() { fmt.Println(1%2) fmt.Println(2%2) fmt.Println(3%2) fmt.Println(4%2) }

2012-02-16 · 1 min · 30 words · -

Go mem, Go 内存

Go mem, Go 内存 用户程序(Mutator)会通过内存分配器(Allocator)在堆上申请内存,而垃圾收集器(Collector)负责回收堆上的内存空间,内存分配器和垃圾收集器共同管理着程序中的堆内存空间。 https://draveness.me/golang/docs/part3-runtime/ch07-memory/golang-garbage-collector/

2011-12-25 · 1 min · 6 words · -

Go Synchronization, 同步

Go Synchronization, 同步 Go语言在设计上对同步 (Synchronization,数据同步和线程同步)提供大量的支持,比如 goroutine和channel同步原语,库层面有 sync:提供基本的同步原语 (比如Mutex、RWMutex、Locker)和 工具类 (Once、WaitGroup、Cond、Pool、Map) sync/atomic:提供变量的原子操作 (基于硬件指令 compare-and-swap)

2011-11-24 · 1 min · 13 words · -

控制协程(goroutine)的并发数量

控制协程(goroutine)的并发数量 ants https://github.com/panjf2000/ants/blob/master/README_ZH.md 利用 channel 的缓存区 可以利用信道 channel 的缓冲区大小来实现: // main_chan.go func main() { var wg sync.WaitGroup ch := make(chan struct{}, 3) for i := 0; i < 10; i++ { ch <- struct{}{} wg.Add(1) go func(i int) { defer wg.Done() log.Println(i) time.Sleep(time.Second) <-ch }(i) } wg.Wait() } https://geektutu.com/post/hpg-concurrency-control.html

2011-10-31 · 1 min · 49 words · -

gorun, golang, shell

gorun, golang, shell 用 golang 语言编写脚本 install gorun go get github.com/erning/gorun 能用 “./” 执行的 golang代码 /// 2>/dev/null ; gorun "$0" "$@" ; exit $? package main import ( "os/exec" "fmt" "os" ) func main() { println("Hello world!") var whoami []byte var err error var cmd *exec.Cmd cmd = exec.Command("whoami") if whoami, err = cmd.Output(); err != nil { fmt.Println(err) os.Exit(1) } // 默认输出有一个换行 fmt.Println(string(whoami)) } ./hello.go golang shell, 在 golang 中调用 shell func shellExec(command string) string { log.Printf("exec: %s\n", command) cmd := exec.Command("/bin/sh", "-c", command) var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { log.Printf("failed to exec: %s, err: %v", command, err) return "" } ourStr := out.String() log.Printf("exec response: \n%s", ourStr) return ourStr } func shellExecFmt(format string, params ...interface{}) string { return shellExec(fmt.Sprintf(format, params...)) } go 调用 shell https://blog.csdn.net/qq_36874881/article/details/78234005 ...

2011-09-18 · 2 min · 352 words · -

golang sync.Map

“golang sync.Map” func main() { m := sync.Map{} m.Store(1,1) go do(m) go do(m) time.Sleep(1*time.Second) fmt.Println(m.Load(1)) } func do (m sync.Map) { i := 0 for i < 10000 { m.Store(1,1) i++ } } 清空 sync.Map https://stackoverflow.com/questions/49355345/how-to-clean-a-sync-map //erase map: A zero sync.Map is empty and ready for use. map2 = sync.Map{} 在Go 1.6 之前, 内置的 map 类型是部分 goroutine 安全的, 并发的读没有问题, 并发的写可能有问题。自 go 1.6之后, 并发地读写 map 会报错, 这在一些知名的开源库中都存在这个问题, 所以go 1.9之前的解决方案是额外绑定一个锁, 封装成一个新的struct或者单独使用锁都可以。 ...

5 min · 908 words · -

golang 正则

“golang 正则” https://studygolang.com/articles/7256 func main() { fmt.Println(regexp.Match("H.* ", []byte("Hello World!"))) // true }

1 min · 13 words · -