简单博客

泛型

May 30, 2022
go, TypeScript
generic

Go泛型 # 试看: // 准确的描述出了参数和返回值的类型,非常方便 func Add(x, y int) int // 但也限制了Add函数的参数类型--只能接收`int` // Add(0.1, 0.2) // can't do that // 那怎么办呢?再写一个针对float64的呗 func AddFloat64(x, y float64) float64 AddFloat64(0.1, 0.2) // it's ok // 如果还要支持其它类型呢?再加一个吗,每多一种类型,就多加一个。。。 func AddInt8(x, y int8) int8 func AddInt32(x, y int32) int32 func AddFloat32(x, y float32) float32 // more... // emm. // how about interface{}? func AddAny(x, y interface{}) interface{} { switch x.(type) { case int: case int8: case int32: case float32: case float64: // more. ...

Find out which Go version built your binary

May 27, 2022
go
version

根据二进制文件找出应用构建时使用的Go版本 # 使用 dlv: dlv exec ./app > p runtime.buildVerion 或者,在代码里调用runtime.Version(): func main() { fmt.Println("go version:", runtime.Version()) } 参照

k8s是怎么维持pod的运行的呢?

May 13, 2022
k8s
pod

k8s是怎么维持pod的运行的呢? # 当接收了yaml配置的信息后,是怎么维持pod根据声明一直运行的呢? 让我们沿着命令执行的过程来一睹为快:kubectl apply -f pod.yaml. 源码位置:cmd/kubectl/kubectl.go -> staging/src/k8s.io/kubectl/pkg/cmd/cmd.go -> staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go 最终的执行方法: func (o *ApplyOptions) Run() error { // 预处理 if o.PreProcessorFn != nil { klog.V(4).Infof("Running apply pre-processor function") if err := o.PreProcessorFn(); err != nil { return err } } // Enforce CLI specified namespace on server request. if o.EnforceNamespace { o.VisitedNamespaces.Insert(o.Namespace) } // Generates the objects using the resource builder if they have not // already been stored by calling "SetObjects()" in the pre-processor. ...

Go enum

May 12, 2022
go
enum

Go enum # Go是没有内置枚举类型的,那么,当需要使用枚举时,该怎么办呢? 枚举说白了,就是一连串互斥的值,每个值代表一样事物或一个类型。 比如,现在需要一个颜色枚举,可以这样定义: const ( Red = "Red" // 红色 Blue = "Blue" // 蓝色 Green = "Green" // 绿色 ) 也有这样定义的: type Color string // 定义一个特定类型 // 枚举常量均声明为该类型 const ( Red Color = "Red" // 红色 Blue Color = "Blue" // 蓝色 Green Color = "Green" // 绿色 ) 这样做的好处是可以通过这个类型来更明显的标记出枚举字段来: type Car struct { Name string Color Color // 颜色字段声明为Color类型,在阅读代码的时候就能知道这个字段正常的可选值范围 } 但是,上面的做法都需要面临一个问题,就是我需要一个返回全部枚举值的集合时,需要这样做: func All() []Color { return []Color{ Red, Blue, Green, } } func (color Color) Name() string { switch color { case Red: return "红色" case Blue: return "蓝色" case Green: return "绿色" } return "" } 当在定义处新增值时,All和Name也要同步添加,对于开发人员来说,非常容易遗漏。 ...

Go1.18 comparable

April 22, 2022
go
comparable

Go 1.18 预定义接口类型 # 先看一个提案: proposal: spec: permit values to have type “comparable” – 允许值拥有comparable类型,我的理解是,现在的comparable只能用作泛型里的类型参数的约束,不能像普通类型那样使用,如下: type Set[E comparable] []E // 可以用做类型参数的约束 // 使用go1.18编译,报错:interface is (or embeds) comparable var A comparable // 变量不可以使用`comparable`类型 那么,结合例子就能更好地理解这个提案了。 这个提案的主要目的就是让例子里的var A comparable成立,也就是允许comparable作为变量的类型,跟其它普通的接口类型(var E error)一样。 // proposal: spec: permit values to have type "comparable" // As part of adding generics, Go 1.18 introduces a new predeclared interface type comparable. That interface type is implemented by any non-interface type that is comparable, and by any interface type that is or embeds comparable. ...

KMP

March 22, 2022
algorithm, KMP
workspace

KMP字符串匹配算法 精确匹配 状态机 给定一个pattern,查找其在另一字符串s出现的最早位置。(找不到则返回-1) func index(s string, pattern string) int { return -1 } 状态推移 func index(s string, pattern string) int { n := len(s) m := len(pattern) // 根据pattern构造dp var dp [n][m]int // 在s上应用dp,判断pattern位置 return -1 }

霜之哀伤

February 18, 2022
Frostmourne

当有人说要在屋里开个窗,一定惹得大伙不开心,无人同意;若要在屋里凿个洞,就有人来协调,愿意开窗了。 看到了吗?这里面有提议的人,有反对的人,有开始反对后面协调的人。看似只有这几种人,实则还有一种人,哪边人多站哪边。恶则落井下石,善则“好言相劝”。 一盆散沙,就算反对,也难以“碍事”。聪明人早就明白这个道理。只要能裹挟着一群人,与自己利益捆绑,那么就能为己所用。至于“所用”是何物,自然无关紧要,只要“为己”即可。 同样地,要击溃捆绑,自然需要强大的力量,也就是另一群人。

goroutine vs tokio

February 16, 2022
go, goroutine, tokio
concurrent

Reddit讨论贴 Go uses a different strategy for blocking systemcalls. It does not run them on a threadpool - it moves all the other goroutines that are queued to run on the current thread to a new worker thread, then runs the blocking systemcall on the current thread. This minimizes context switching. You can do this in tokio as well, using task::block_in_place. If I change your code to use that instead of tokio::fs, it gets a lot closer to the go numbers. ...

go runtime chan

February 11, 2022
go, runtime, chan
chan

src/runtime/chan.go: // Invariants: // At least one of c.sendq and c.recvq is empty, // except for the case of an unbuffered channel with a single goroutine // blocked on it for both sending and receiving using a select statement, // in which case the length of c.sendq and c.recvq is limited only by the // size of the select statement. // // For buffered channels, also: // c.qcount > 0 implies that c. ...

go work

February 10, 2022
go, work
workspace

go1.18将要推出workspace模式,此举是为了方便在本地开发多个不同module时的依赖管理。 命令说明: $ go help work Go workspace provides access to operations on workspaces. Note that support for workspaces is built into many other commands, not just 'go work'. See 'go help modules' for information about Go\'s module system of which workspaces are a part. A workspace is specified by a go.work file that specifies a set of module directories with the "use" directive. These modules are used as root modules by the go command for builds and related operations. ...