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

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) } 参考 微信 支付宝

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 https://github.com/danger-dream/ebpf-firewall https://github.com/mozillazg/hello-libbpfgo

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 Tool flameshow goref Go package https://github.com/aclements/go-perf https://github.com/256dpi/god https://github.com/divan/expvarmon https://github.com/go-perf/awesome-go-perf https://github.com/iyashjayesh/monigo 文章 官方博客 Profiling Go Programs Profile-guided optimization 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....

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

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类型的枚举。...

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

MongoDB操作指北

TL;DR 环境准备 mongoDB 预备知识 MongoDB常见的数据类型 数据类型 示例 说明 Null {"x" : null} Boolean {"x" : true} Number {"x" : 3.14} {"x" : 3} {"x" : NumberInt("3")} {"x" : NumberLong("3")} 默认64位浮点数,整数需要使用NumberInt和NumberLong String {"x" : "foobar"} 编码格式为UTF-8 Date {"x" : new Date()} 64位时间戳(从January 1, 1970),不存时区。通过new Date()进行调用。 Regular expression {"x" : /foobar/i} javascript 正则 Array {"x" : ["a", "b", "c"]} Embedded document {"x" : {"foo" : "bar"}} Object ID {"x" : ObjectId()} 文档12字节的ID Binary data 一个任意字节的字符串。是保存非UTF-8字符串到数据库的唯一方法。 Code {"x" : function() { /* ....

Golang监测Linux网络事件

代码 package main import ( "fmt" "syscall" ) func main() { l, _ := ListenNetlink() for { msgs, err := l.ReadMsgs() if err != nil { fmt.Println("Could not read netlink: %s", err) } for _, m := range msgs { if IsNewAddr(&m) { fmt.Println("New Addr") } if IsDelAddr(&m) { fmt.Println("Del Addr") } } } } type NetlinkListener struct { fd int sa *syscall.SockaddrNetlink } func ListenNetlink() (*NetlinkListener, error) { groups := (1 << (syscall....

Golang False sharing

缘起 来自于一段prometheus代码 type stripeLock struct { sync.RWMutex // Padding to avoid multiple locks being on the same cache line. _ [40]byte } 简单地讲就是因为CPU读取数据的缓存机制问题,可能导致性能上的不同差异。参考资料见后文。 常见类型的内存占用大小(Go101): Kinds of Types Value Size Required by Go Specification bool 1 byte not specified int8, uint8 (byte) 1 byte 1 byte int16, uint16 2 bytes 2 bytes int32 (rune), uint32, float32 4 bytes 4 bytes int64, uint64, float64, complex64 8 bytes 8 bytes complex128 16 bytes 16 bytes int, uint 1 word architecture dependent, 4 bytes on 32-bit architectures and 8 bytes on 64-bit architectures uintptr 1 word large enough to store the uninterpreted bits of a pointer value string 2 words not specified pointer (safe or unsafe) 1 word not specified slice 3 words not specified map 1 word not specified channel 1 word not specified function 1 word not specified interface 2 words not specified struct (the sum of sizes of all fields) + (the number of padding bytes) the size of a struct type is zero if it contains no fields that have a size greater than zero array (element value size) * (array length) the size of an array type is zero if its element type has zero size 参考 https://medium....