Go
容器下的 Go 应用程序优化
· ☕ 2 分钟
1. 内存对齐 结构体内字段,从大到小排列 减少内存占用 安装 fieldalignment 工具 1 go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest 分析并修复内存对齐 1 2 3 4 5 6 7 8 9 fieldalignment -fix ./... /Users/shaowenchen/Code/app/config/config.go:136:14: struct with 32 pointer bytes could be 24 /Users/shaowenchen/Code/app/config/config.go:150:11: struct of size 96 could be 88 /Users/shaowenchen/Code/app/config/config.go:166:14: struct of size 152 could be 144 /Users/shaowenchen/Code/app/config/config.go:194:12: struct with 80 pointer bytes could be 72 /Users/shaowenchen/Code/app/config/config.go:209:12: struct with 56 pointer bytes could be 40 /Users/shaowenchen/Code/app/dao/gormx/gorm.go:12:13: struct with 16 pointer bytes could be 8 /Users/shaowenchen/Code/app/dao/gormx/entity/cluster.go:5:14: struct with 128 pointer bytes could be 104 查看 fieldalignment 进行

一些程序构建的优化技巧
· ☕ 2 分钟
持续更新中… 1. Golang 使用 trimpath 移除编译路径,避免暴露编译路径信息 1 go build -gcflags="all=-trimpath=${PWD}" -asmflags="all=-trimpath=${PWD}" -o ./bin/opscli ./cmd/cli/main.go 添加之前异常输出路径 /Users/shaowenchen/Code/Github/ops/main.go,添加之后异常输出路径 main.go 。 -ldflags “-w -s” 移除调试信息,减小

使用 Go 编写 WebAssembly 程序
· ☕ 4 分钟
1. WebAssembly 简介 跨平台性,可以在任何支持 WebAssembly 的平台上运行,包括 Web 浏览器、服务器、移动设备等 高性能,采用了一种紧凑的二进制格式,可以在浏览器中快速加载和解析,从而提高应用程序的性能 安全性,采用了一种沙箱模型,可以隔离运行在其中的代码,从而保护系统免受恶

使用 tinygo 开发 Istio WasmPlugin
· ☕ 2 分钟
wasme 只支持到 istio 1.9,而我使用的是 Istio 1.14,因此本篇直接使用 tinygo 进行验证和学习。 1. 安装 tinygo 要求 Go v1.18+ 安装 tinygo 1 2 brew tap tinygo-org/tools brew install tinygo 查看版本 1 2 3 tinygo version tinygo version 0.27.0 darwin/amd64 (using go version go1.19.3 and LLVM version 15.0.0) 2. 创建 wasm-istio 项目 初始化项目 1 2 3 mkdir wasm-istio cd wasm-istio go mod init wasm-istio 编辑 main.go 见 https://github.com/shaowenchen/demo/blob/master/wasm-istio/main.go 主要是下面这段 1 2 3 4 5 6 7

Go mod 使用问题
· ☕ 1 分钟
1. 升级 Go 版本之后 go.sum 版本不匹配 执行命令 go build 报错。 错误提示: missing go.sum entry for module providing package golang.org/x/time/rate; to add 解决办法: 在 go build 之前更新 go.sum,执行命令 go mod tidy 2. tls 错误 执行命令 go mod download 报错。 错误提示: fatal: unable to access 'https://github.com/agiledragon/gomonkey/': GnuTLS recv error (-110): The TLS connection was non-properly terminated. 解决办法: 禁用证书校验执行命令,export