- tags: Programming Language
柯里化
func max(a, b int) int {
if a > b {
return a
}
return b
}
func currying(a int) func(int) int {
return func(b int) int {
return max(a, b)
}
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func currying(a int) func(int) int {
return func(b int) int {
return max(a, b)
}
}
tags: Go, GDB Enable Core Dumps ulimit -S -c unlimited Confirm or Change The Location of Core Dumps sysctl -w kernel.core_pattern=/tmp/core.%e.%p # Or echo '/tmp/core.%e.%p' | tee /proc/sys/kernel/core_pattern Set GOTRACEBACK Environment Variable to Let Go Program Core Dumps when Panic export GOTRACEBACK=crash Run Go Program and Wait Segmentation Fault Use GDB to Debug gdb /path/to/goprogram /tmp/core-xx-xx Then use thread apply all bt to see all backtraces, include compiled C code. Note: -g option should be applied to the compiled C code to generate debug symbols. ...
tags: Go Elem() Returns Value type T struct { } t := &T{} v := reflect.New(reflect.TypeOf(t)).Elem() // type of v is `T` Interface() Returns Pointer type T struct { } t := &T{} v := reflect.New(reflect.TypeOf(t)).Interface() // type of v is `&T`
tags: Go source: “Wrap and Unwrap Errors in Go (Golang) | Gosamples.Dev.” Accessed October 12, 2022. https://gosamples.dev/wrap-unwrap-errors/. Overview Since Go1.13, there is a new feature about error add to go: Wrap & Unwrap errors. Let’s start from a simple example: package main import ( "errors" "fmt" ) var errRollingInTheDeep = errors.New("rolling in the deep") func doSomeActualJob() error { return errRollingInTheDeep } func someFrameworkCaller() error { // We use fmt.Errorf to wrap error. // Notice the "%w" formater here and content surround the "[...]". return fmt.Errorf("[someFrameworkCaller] %w", doSomeActualJob()) } func someEntrance() error { return fmt.Errorf("[someUpperCaller] %w", someFrameworkCaller()) } func main() { err := someEntrance() if errors.Is(err, errRollingInTheDeep) { fmt.Println("This should be") } else { panic("Oooops!") } fmt.Printf("The final error: %v\n", err) fmt.Printf("Unwrap: %v\n", errors.Unwrap(err)) } Its output: ...
tags: Go source: https://tip.golang.org/doc/fuzz/
tags: Go,Memory Model source: 更新Go内存模型 https://research.swtch.com/gomm
tags: Go 目标 当跟随这篇文章完成后将产出如下内容: 代码 http://gitlab.17zuoye.net/vgo/go-swagger-example 文档 http://swagger.17zuoye.net/?url=http%3A%2F%2F10.200.242.61%3A9090%2Fswagger.json 准备 Go1.14 及以上版本 安装 go-swagger :参见 官方文档。 接下来使用 gin 框架作为示例,如果之前没接触过可以先了解下该框架 创建一个项目 $ mkdir go-swagger-example $ cd go-swagger-example/ $ go mod init gitlab.17zuoye.net/vgo/go-swagger-example 开始使用 首先在你的 `main.go` 定义 go generate 像下面这样: //go:generate swagger generate spec -o ./swagger.yml package main func main() { println("Hello world!"); } 此时如果运行 go generate 在项目目录下就会生成一个 swagger.yml 文件: paths: {} swagger: "2.0" 使用单独的包托管 swagger 相关定义 在之前实践的过程中发现,如果在多个包中定义了相同名称的结构体会到只一个结构体覆盖另外一个结构体的定义。 所以为了解决这个问题,我把所有 swagger 相关的定义都放在同一个包下来避免相同名字的结构体。 创建 swagger/swagger.go 填充如下内容: // Package swagger defines API documentation. // // Swagger 演示后端接口 // // Schemes: http // Host: 10.200.242.35:8080 // BasePath: /api/ // Version: 0.1.0 // Contact: 王会<hui.wang.a@17zuoye.com> // // Consumes: // - application/json // // Produces: // - application/vnd.17zuoye.v1+json // // swagger:meta package swagger 上面文件通过注释来定义了一些接口相关的信息,包括: ...
tags: Go Channel Directions func demo(readOnlyStream <-chan bool, writeOnlyStream chan<- bool) { }