简单博客

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. ...

杂念

February 10, 2022
Distractions

情绪绑定 # 先把一样好(好看、好听、好闻)的东西抛出来,收集大众的积极情绪,进而把大家的情绪控制。 当这样东西喜,你就跟着喜;当这样东西悲,你就跟着悲;当这样东西静,你就跟着静。当这样东西动,你却看不到了。 信仰缺失的年代,把自己交付给这样的东西,只为换到一丝“慰挤”。 精神上的追求太难找到共鸣了,不如转而追求物质上的欢喜。每天吃吃喝喝,打打闹闹,不以物喜,不以己悲,不是挺好。 歌好听,那就听,何必在意歌手爱天怼地。如果真的这么较真,最好听歌前做好背景调查,如果不慎那歌手竟信息不详,那只好叫耳朵过滤掉了。若实在忍不住,也不妨在确实之前先恩施一番,以传我宽大之名。当然,如有丝言片语,只要未到石锤之境,自然轻松忽略。毕竟,真假难辨,不如不辩。 万一真的发现了黑历史,这时就要斟酌一番了,继续爱如谦,抑或恨如龙,搞不好就被别人发现你居然喜欢“黑”歌手,承担巨大的社会压力,就得不偿失了。 但是,害怕很难成事,只会坏事。如果只因为怕,那如何能算英雄,或者竟连个孤勇者都算不上,实在于心难安。那何不拉拢一批共同情绪的人,把那位别人先打黑。 嘿,只需证明别人是错的,何苦花费心思证明自己是对呢! 嗯,情绪输出总算有着落了,难受的只要是别人,自己就永远开心了,谁管别人是亲是疏,是喜是恶呢。只要不管不顾,虎牛之力也拿我没辙。对别人施暴,哪怕隔着个屏幕,也能爽到嗨。 别人这时就难受了:我好心劝你们远离毒瘤,居然不识抬举,还要拉帮结派来搞我。真的是越想越气,越气越想。奈何对方人多势众,单拳难敌四手。 自诩孤高者,自然不屑于群斗,但被逼到墙角了,也不得不群起。但标准越高,规模自然越小,苦费心思,依然难以匹敌,最后只好在猪圈方圈里丢三骂四了。 聪明人居然不懂不聪明人的想法,为什么敢自认聪明呢? 懂的话,大抵不会自称聪明人,而要转称愚人了吧。不聪明人也不真的不聪明,只是知道往身上贴上聪明人标签,更多时候只捞得个劳苦功低、得不偿失,活得还不如马屁精。 说到马屁精,我就猜到马屁是香的,或至少在喜爱之人闻来别有一番风味。 马屁精自然是冤枉的,不过说了几句某人爱听的话,或者不小心成了习惯–见某人说某话,别人就来指责他,并打上马屁精标签,在圈里不断丢三骂四。只是不对你这样说话,你就这么生气,别人真的是坏。 谁怪你不是某人呢?你若竟是某人,想听几句某话,那还不难。只怕你成了某人,你还嫌少呢! 马屁精也不全是敌人,是非精、八卦精等“朋友”是大大的有。而且,精的本事也不能小,至少要在亦敌亦友的关系转变中拿捏得准确无误。不然闹出“人门前弄是非,精面前摆事实”的笑话来,就颜面无存了。 精,未成人之前,或竟不做人,选择做精,自然是如老鼠过街,人人喊打。 既然是精,那就必须没有情绪,笑脸迎臭脸自然是家常便饭。但只要熬出头,拥有一星半点某人之像,好生活自然而至,竟也开始享受到了某话。 路漫漫兮,修就是了。怎么修的,你就别管了。好好的丢三骂四还不够,还敢来管修的事,怕不是吃饱了思起淫欲来。 精在那里,你不骂,你敢往这边看,你怕不是想吃大过年不想吃的饭了。就不怕,我饭都不给你吃,把你饿成精。 原来精是饿出来的!

Rust与安全

January 28, 2022
Rust
Safe

有一些东西,做了一些事情。 有什么东西,做了什么呢? 有文件、结构体、特征、类型,调用了函数、方法,读了文件/读了body,算了结果,写了文件/答了请求。 IO or 计算。 或者说,更强调IO,还是计算。 内存安全 # 并发安全 #

wasm运行时wasmtime

January 28, 2022
Wasi, Wasm, Runtime
Wasmtime

源码 # # 下载 git clone git@github.com:bytecodealliance/wasmtime.git # 子模块 git submodule update --init --recursive # 安装 cargo build 如果忘了拉子模块,vscode的rust-analyzer会报错,导致智能提示等功能失效。 不过整个初始化过程还是有点长,等了好久才能正常使用。 阅读 # 从build.rs开始,首先映入眼帘的是use anyhow::Context;: /// Provides the `context` method for `Result`. /// /// This trait is sealed and cannot be implemented for types outside of /// `anyhow`. 这是一个为其它类型(anyhow::Result)引入context方法的特征啊,多么伟大,在anyhow包外面的类型就不要想着去实现它了,你们高攀不起的。 再看anyhow::Context的定义: // lib.rs:598 pub trait Context<T, E>: context::private::Sealed { // 继承了Sealed,那它又是怎么样的、做什么的呢? /// Wrap the error value with additional context. -- 给error值包装上下文信息 fn context<C>(self, context: C) -> Result<T, Error> where C: Display + Send + Sync + 'static; // 能展示,并发安全,全局可见的类型值 /// Wrap the error value with additional context that is evaluated lazily /// only once an error does occur. ...

容器镜像加密

January 26, 2022
Container, Docker, Image
Encrypt

如果我在创建镜像时把源码也打包了进去,要怎么防止别人通过这个镜像把源码给窃取了呢? 加密 镜像加密 源码加密:在COPY源码进去之前先加密;这种适合服务器不是自己的,并且在局域网里的(接过医院系统的应该都懂吧);留这样一份加密源码也只是在方便有bug时可以快速修复的同时,还可以稍微保护一下源码; 先使用zip压缩源码:`zip -q -r code.zip ./code`; 再使用gpg加密:`gpg --yes --batch --passphrase=123 -c ebpf.zip`; -- 通过`--yes --batch --passphrase`三个选项避免键盘交互,最后生成`ebpf.zip.gpg`。 后续进入容器后,使用gpg解密:`gpg -o ebpf2.zip -d ebpf.zip.gpg`; 再使用unzip解压:`unzip -d ebpf2 ebpf2.zip`。 在镜像构建后,还要防止docker history -H cb0b42c0cb03 --no-trunc=true查看镜像构建历史时,泄露秘钥等信息。– 可使用多阶段构建:在前一阶段使用密钥加密源码,后一阶段复制加密源码,从而避免密钥泄露。因为一般只需要把后一阶段构建出来的镜像分发出去就好了,而查看后一阶段构建出来的镜像的构建历史,是看不到密钥信息的(查看前一阶段的构建历史才会看到)。 dockerfile COPY before mkdir will get a no such file or directory error # error: ```dockerfile # … RUN mkdir -p /abc COPY –from=builder /opt/efg /abc/efg ``` 没有指定创建/abc/efg目录,会导致后续想读取该目录内容时报错:no such file or directory success: ```dockerfile # … ...