go embed 使用小记

​ go embed 是go 1.16 开始添加的特性,允许嵌入文件及文件夹,在Go程序中进行使用。官方还为此添加了embed.FS的对象。下面将常用的使用场景进行简单列举: 嵌入单个文件 官方的例子 嵌入文件并绑定到字符串变量 import _ "embed" //go:embed hello.txt var s string print(s) 嵌入文件并绑定到字节变量 import _ "embed" //go:embed hello.txt var b []byte print(string(b)) 嵌入文件并绑定到文件对象 import "embed" //go:embed hello.txt var f embed.FS data, _ := f.ReadFile("hello.txt") print(string(data)) 嵌入目录 嵌入时,可以在多行或者一行输入要嵌入的文件和文件夹。 package server import "embed" // content holds our static web server content. //go:embed image/* template/* //go:embed html/index.html var content embed.FS 在匹配文件夹时,embed会嵌入包括子目录下的所有除.和_开头的文件(递归),所以上面的代码大致等价于下面的代码: // content is our static web server content....

June 23, 2022 · 1 min · czyt

Git小技巧

Windows下GIT的几个小技巧 记住git密码 使用下面命令可以设置记住git密码,但推荐使用ssh进行操作。 git config credential.helper store 设置换行符转换 在windows下开发时,迁出的代码是CRLF会导致编译的sh脚本不能正确执行: git config --global core.autocrlf false Git推送到多个服务器 要实现一次push到多个远程仓库 本机git仓库A https://aaaaa.git 要同步push的远程git仓库B https://bbbbb.git 通过git remote add添加 先使用git remote -v查看远程仓库的情况 ,然后添加一个git仓库 git remote add b https://bbbbb.git 再次查看远程仓库情况,如果需要push,则需要push两次 通过git remote set-url 添加 如果按上面添加过remote分支,需要先git remote rm b,使用下面命令添加即可。 git remote set-url --add a https://bbbbb.git 查看远程仓库情况,看看是否已经是两个push地址了 。这个只需push一次就行了 修改配置文件 打开 .git/config 找到 [remote “github”],添加对应的 url 即可,效果如下。这种方法其实和方法二是一样的。 [remote "a"] url = https://aaaaa.git fetch = +refs/heads/*:refs/remotes/a/* url = https://bbbbb.git 参考链接 ● 一个项目push到多个远程Git仓库 https://segmentfault....

June 17, 2022 · 2 min · czyt

Golang nocopy check

目的 实现nocopy的目的在于,golang在进行参数传递时,都是传递副本的方式。但是某些情况,我们是需要进行传递对象的引用的(特别是一些指针对象,可能会导致多个指针的副本的操作造成程序陷入恐慌),为了杜绝调用者的复制,只能指针传递全局唯一对象。那么就可以通过添加nocopy来实现对go vet参数支持的no copy 检查。 实现 golang里面最常用的sync.WaitGroup就是通过nocopy实现的。参考定义 // A WaitGroup must not be copied after first use. type WaitGroup struct { noCopy noCopy // 64-bit value: high 32 bits are counter, low 32 bits are waiter count. // 64-bit atomic operations require 64-bit alignment, but 32-bit // compilers do not ensure it. So we allocate 12 bytes and then use // the aligned 8 bytes in them as state, and the other 4 as storage // for the sema....

June 17, 2022 · 1 min · czyt

golang正则校验支付宝微信支付授权码

参考sdk定义 package main import ( "fmt" "regexp" ) // wechat pay 用户付款码条形码规则:18位纯数字,以10、11、12、13、14、15开头 // alipay 支付授权码,25~30开头的长度为16~24位的数字,实际字符串长度以开发者获取的付款码长度为准 func main() { // wechat regwechat:=regexp.MustCompile("^(1[0-5])\\d{16}$") matchwechat := regwechat.MatchString("154658833119096245") fmt.Println(matchwechat) // alipay regalipay:=regexp.MustCompile("^(2[5-9]|30)\\d{14,22}$") matchalipay := regalipay.MatchString("307573774583867517336") fmt.Println(matchalipay) } 参考 微信 支付宝

June 17, 2022 · 1 min · czyt

Linux环境下Perl提权

事故起因 我们公司的应用程序部署目录有个bin目录,手误,删除的时候输入的是/bin 事故现象 ● SSH 不能登陆进来了 ● ls、chmod等常用命令都不能使用了 ● wget 还能用 事故解决 通过查找谷歌,发现有个perl带有提权的功能,简单来说就是 perl -e "chmod 0777, '/bin/ls'" 通过这个方式可以对指定的文件进行权限的修改。于是从另外的机器上打包了一个/bin目录,放到网上,wget 下载到本地wget bin.tar.gz ​ 本机开外网ssh转发,scp 拷贝tar文件到目录,执行 perl -e "chmod 0777, './tar'" ,再使用tar进行文件解压./tar xvzf bin.tar.gz -C /,然后再给chmod执行文件赋予执行权限 perl -e "chmod 0777, '/bin/chmod'" 然后再通过chmod 执行 chmod -R +x /bin/给/bin目录下的可执行程序文件授予执行权限。至此,完成事故修复。 参考连接 ● https://perldoc.perl.org/functions/chmod.html

June 17, 2022 · 1 min · czyt

ebpf Golang参考

整理一个列表,持续更新。 理论 ebf官网 B站视频 eBPF 和 Go,超能力组合 实践 Tracing Go Functions with eBPF part1 part2 Getting Started with eBPF and Go Linux中基于eBPF的恶意利用与检测机制 如何用eBPF分析Golang应用 使用BPF, 将Go网络程序的吞吐提升8倍 使用ebpf跟踪rpcx微服务 BPF MAP机制 一种通用数据结构,可以存储不同类型数据的通用数据结构 Andrii Nakryiko 抽象数据容器(abstract data container) bpf系统调用的说明 《使用C语言从头开发一个Hello World级别的eBPF程序》 《Linux Observability with BPF》 《揭秘BPF map前生今世》 bpf系统调用说明 官方bpf map参考手册 bpftool参考手册 《Building BPF applications with libbpf-bootstrap》 https://github.com/DavadDi/bpf_study https://github.com/mikeroyal/eBPF-Guide#go-development golang 包 https://github.com/cilium/ebpf

June 11, 2022 · 1 min · czyt

Go性能优化参考

电子书 编写和优化Go代码 Go Optimizations 101 https://github.com/dgryski/go-perfbook https://github.com/DataDog/go-profiler-notes https://github.com/bobstrecansky/HighPerformanceWithGo/ https://github.com/caibirdme/hand-to-hand-optimize-go Go package https://github.com/aclements/go-perf https://github.com/256dpi/god 文章 官方博客 Profiling Go Programs https://dave.cheney.net/high-performance-go-workshop/dotgo-paris.html https://github.com/golang/go/wiki/Performance https://sumercip.com/posts/inside-the-go-cpu-profiler/ How to Write Benchmarks in Go : https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go Improving Observability of GoLang Services Debugging performance issues in Go programs : https://github.com/golang/go/wiki/Performance Go execution tracer : https://blog.gopheracademy.com/advent-2017/go-execution-tracer/ (see also the The tracer design doc link) A whirlwind tour of Go’s runtime environment variables (see godebug) : https://dave.cheney.net/2015/11/29/a-whirlwind-tour-of-gos-runtime-environment-variables benchstat : https://godoc.org/golang.org/x/perf/cmd/benchstat...

June 11, 2022 · 1 min · czyt

Cap'n Proto Windows环境设置

Cap’n proto 号称是比protobuff更快的proto语言。官网截图 Cap’n Proto is an insanely fast data interchange format and capability-based RPC system. Think JSON, except binary. Or think Protocol Buffers, except faster. In fact, in benchmarks, Cap’n Proto is INFINITY TIMES faster than Protocol Buffers. 协议特性 Cap’n Proto’s RPC protocol has the following notable features. Since the protocol is complicated, the feature set has been divided into numbered “levels”, so that implementations may declare which features they have covered by advertising a level number....

June 10, 2022 · 14 min · czyt

Golang反射使用指南

Go是一门强类型的语言,在大多数情况下,申明一个变量、函数、struct都是直截了当的。在大多数情况下,这些都是够用的,但有时你想在程序运行中来动态扩展程序的信息,也许你想把文件或网络请求中的数据映射到一个变量中;也许你想建立一个能处理不同类型的工具(虽然Go1.18有了泛型)。在这些情况下,你需要使用反射。反射使你有能力在运行时检查、修改和创建变量、函数和结构的能力。 反射的核心 图片转自 Go 语言设计与实现 反射的三大核心是*Types, Kinds, Values,下面将围绕这三个方面来进行讲解。 我们先定义一个struct对象。 type User struct { Name string Age int } 类型Types 通过反射获取类型 u := User{ Name: "czyt", Age: 18, } uptr := &u ot := reflect.TypeOf(u) otptr := reflect.TypeOf(uptr) log.Println(ot.Name()) // 打印 User log.Println(otptr.Name()) // 打印 空 通过调用Name()方法返回类型的名称,某些类型,如切片或指针,没有名称,此方法返回一个空字符串。 种类Kinds Kind通过调用Kind()得来。 u := User{ Name: "czyt", Age: 18, } uptr := &u ot := reflect.TypeOf(u) otptr := reflect.TypeOf(uptr) log.Println(ot.Kind()) // 输出 struct log.Println(otptr.Kind()) // 输出 ptr Kind() 返回的是kind类型的枚举。...

June 2, 2022 · 7 min · czyt

Golang MongoDB ODM mgm使用

(本文大部分内容根据官方文档翻译而来) 环境准备 golang 1.10+ mongodb mgm 模型定义 定义 定义模型 type Book struct { // DefaultModel adds _id, created_at and updated_at fields to the Model mgm.DefaultModel `bson:",inline"` Name string `json:"name" bson:"name"` Pages int `json:"pages" bson:"pages"` } func NewBook(name string, pages int) *Book { return &Book{ Name: name, Pages: pages, } } mgm 在创建表时会自动检测Model生成的Collection名称 book:=Book{} // Print your model collection name. collName := mgm.CollName(&book) fmt.Println(collName) // 打印: books 如果要自定义生成Collection的名称。需要实现CollectionNameGetter接口。 func (model *Book) CollectionName() string { return "my_books" } // mgm return "my_books" collection coll:=mgm....

May 31, 2022 · 6 min · czyt