100 Mistakes in Golang: Chapter 4

30: Ignoring the fact that elements are copied in range loops type account struct { balance float32 } accounts := []account{ {balance: 100.}, {balance: 200.}, {balance: 300.}, } for _, a := range accounts { a.balance += 1000 } // Output: [{100} {200} {300}] In this example, the range loop does not affect the slice’s content. In Go, everything we assign is a copy: If we assign the result of a function returning a struct, it performs a copy of that struct. If we assign the result of a function returning a pointer, it performs a copy of the memory address (an address is 64 bits long on a 64-bit architecture). Solutions: ...

February 20, 2025 · Last updated on August 1, 2025 · 3 min · KKKZOZ

Handling Golang Errors

Using Error Wrapping An error often “bubbles up” a call chain of multiple functions. In other words, a function receives an error and passes it back to its caller through a return value. The caller might do the same, and so on, until a function up the call chain handles or logs the error. An error can be “wrapped” around another error using fmt.Errorf() and the special formatting verb %w. ...

February 4, 2025 · Last updated on August 3, 2025 · 4 min · KKKZOZ

Golang Struct and Interfaces

这里简单总结一下三种组合方式: interface 中嵌套 interface struct 中嵌套 struct struct 中嵌套 interface interface 中嵌套 interface 这种组合方式体现了接口隔离原则(ISP)和接口组合原则, 通过接口嵌套,我们可以构建更大的接口 // 基础读接口 type Reader interface { Read(p []byte) (n int, err error) } // 基础写接口 type Writer interface { Write(p []byte) (n int, err error) } // 组合接口 type ReadWriter interface { Reader // 嵌套 Reader 接口 Writer // 嵌套 Writer 接口 } struct 中嵌套 struct 这种组合方式体现了组合优于继承12的原则,是 Go 中实现代码复用的重要方式 // 地址信息 type Address struct { Street string City string Country string } // 用户信息 type User struct { Name string Age int Address // 嵌套 Address struct } // 使用示例 func main() { user := User{ Name: "张三", Age: 25, Address: Address{ Street: "中关村大街", City: "北京", Country: "中国", }, } // 可以直接访问嵌套字段 fmt.Println(user.Street) // 输出: 中关村大街 } struct 中嵌套 interface 这种组合方式体现了依赖倒置原则(DIP),常用于策略模式的实现 ...

January 28, 2025 · Last updated on August 1, 2025 · 2 min · KKKZOZ

Rustlings Note

05 Vectors fn array_and_vec() -> ([i32; 4], Vec<i32>) { let a = [10, 20, 30, 40]; // Array // TODO: Create a vector called `v` which contains the exact same elements as in the array `a`. // Use the vector macro. // let v = ??? (a, v) } 这里有几种写法,首先想到的肯定是用一个类似于 for 循环的结构类循环赋值 这里总结一下 Rust 中常见的迭代器 for item in list 会调用 into_iter(),消耗 list 的所有权 for item in &list 会调用 iter(),遍历 list 的引用 for item in &mut list 会调用 iter_mut(),遍历 list 的可变引用 如果想要获取 index, 可以使用 enumerate() 方法: ...

December 26, 2024 · Last updated on August 1, 2025 · 10 min · KKKZOZ

Aerospace

Concepts Each workspace contains its own single root node Each non-leaf node is called a “Container” Each container can contain arbitrary number of children nodes Windows are the only possible leaf nodes. Windows contain zero children nodes Every container has two properties: Layout (Possible values: tiles, accordion) Orientation (Possible values: horizontal, vertical) Some examples: In total, AeroSpace provides 4 possible layouts: h_tiles horizontal tiles (in i3, it’s called “horizontal split”) v_tiles vertical tiles (in i3, it’s called “vertical split”) h_accordion horizontal accordion (analog of i3’s “tabbed layout”) v_accordion vertical accordion (analog of i3’s “stacked layout”) Accordion is a layout where windows are placed on top of each other. ...

December 24, 2024 · Last updated on August 1, 2025 · 2 min · KKKZOZ

From Clicks to Commands: A Windows User's Survival Guide

WIP 🚧 这篇 Blog 主要记录了一些在 Unix 终端环境下对应的 Windows 下的常见的鼠标操作 主要面向 Linux 和 MacOS 的新手们,所以有些信息会被简化或者忽略 可为什么要用终端/命令行呢? 就像“为什么使用 Vim”一样,原因有很多,对于我来说,它足够简单,足够快,支持许多的自定义,是 Programmable 的 在程序员的世界里,你真的能够做到(抖个机灵哈哈哈): 如果你觉得终端难用,就去定制你的 Shell 配置;如果你觉得命令不够方便,就写个函数或别名把它封装得更好;如果你觉得工具功能太弱,就用脚本扩展它的能力;如果你觉得操作繁琐,就从自动化开始,一点点改善工作流程,而不是一昧地抱怨,排斥,逃向 GUI。 Left Click 首先需要学习的肯定是鼠标左键的双击 — 打开某个文件夹,以及打开某个文件夹后的结果:查看这个文件夹的内容 对应到终端中: cd 跳转到某个文件夹 ls 展示文件夹的内容 > ls Applications Playground DataGripProjects Projects Desktop Public Documents Scripts Downloads Temp Dropbox Virtual Machines.localized Library Zotero Movies dotfiles-main Music go Pictures > cd Projects 在使用 cd 命令时,不用把文件夹的名字输入完整 比如想要 cd Projects,只需要输入 cd Pro,然后按下键盘上的 Tab 键,就能触发自动补全 ...

November 26, 2024 · Last updated on August 1, 2025 · 3 min · KKKZOZ

Mac Development Environment Setup

How to install Homebrew? 参考资料 homebrew 软件仓库和 homebrew bottles 软件仓库有什么不一样 homebrew 仓库类型主要有两种,它们用途不同: homebrew 软件仓库(brew git remote) 包含所有软件包的安装脚本(Ruby 格式的 Formula) 主要仓库是 homebrew-core 仓库相对较小,包含的是软件包的描述文件 homebrew bottles 软件仓库 存放预编译的二进制软件包 类似于 Linux 下的 .deb 或 .rpm 包 仓库较大,因为包含实际的软件二进制文件 使用 bottles 可以避免从源码编译,加快安装速度 举例说明: 当你运行 brew install wget 时: Homebrew 先从软件仓库获取 wget 的 Formula 然后检查 bottles 仓库是否有对应的预编译包 如果有 bottle 就直接下载安装,否则就按照 Formula 的指示编译源码 所以设置镜像时通常需要同时配置这两种仓库: 配置软件仓库以获取安装脚本,同时配置 bottles 仓库来获取预编译包 安装步骤 前置条件 在终端中执行 xcode-select --install 设置环境变量 首先设置环境变量,方便使用国内镜像进行安装 export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git" export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git" export HOMEBREW_INSTALL_FROM_API=1 安装 Homebrew /bin/bash -c "$(curl -fsSL https://github.com/Homebrew/install/raw/master/install.sh)" 设置镜像 # 将下面这段内容添加到 ~/.zshrc 中 export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git" export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api" export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles" 完整流程是: ...

November 21, 2024 · Last updated on August 1, 2025 · 3 min · KKKZOZ

A Piece Of: Golang Profile

Tips on Profile 先说几个小坑点: 默认情况下,Golang Profile 取样的频率为 100Hz,意味着每 10ms 才会取样一次,所以程序运行时间少于 10ms 肯定不会被取样 但是我们可以通过 runtime.SetCPUProfileRate() 重新设置值,重新进行调试,比如将采样率设置为 1000,每 1ms 就会取样一次,但是这样对性能的开销很大,Golang 官方建议这个值不要设置在 200 以上 其实最好的还是让程序多运行一段时间,比如 1s 左右,个人测试即使将采样率调高,如果一个程序只运行 10ms 左右,采样的结果也大概率为空 在正确位置设置上述语句后,程序提示 “runtime: cannot set cpu profile rate until previous profile has finished.",这句错误提示简直不明不白的,我第一次看到这个错误提示还以为就是字面意思:系统中可能存在正在运行的 profile 程序 查资料后发现,runtime.SetCPUProfileRate() 是一种比较 Hack 的做法,所以该错误提示可以忽略 在程序运行时,直接通过 kill 命令结束程序的话,最后生成的 profile 为空(?),最好使用下面的这个模式: sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) server := NewServer(port, connMap) go server.Run() <-sigs Log.Info("Shutting down server") 通过 signal.Notify 来拦截 kill 的信号,保证程序能够正常结束,profile 文件不为空 How to Profile if pprofFlag { // runtime.SetCPUProfileRate(1000) cpuFile, err := os.Create("executor_profile.prof") if err != nil { fmt.Println("无法创建 CPU profile 文件:", err) return } defer cpuFile.Close() // 开始 CPU profile if err := pprof.StartCPUProfile(cpuFile); err != nil { fmt.Println("无法启动 CPU profile:", err) return } defer pprof.StopCPUProfile() } 注意这段代码只能放在 main 函数中,否则 defer 语句的语义会有影响 How to Read Profile Results 命令行 (pprof) top 10 Showing nodes accounting for 40ms, 100% of 40ms total Showing top 10 nodes out of 26 flat flat% sum% cum cum% 10ms 25.00% 25.00% 10ms 25.00% os.(*File).checkValid (inline) 10ms 25.00% 50.00% 10ms 25.00% runtime.(*mheap).allocSpan 10ms 25.00% 75.00% 10ms 25.00% runtime.(*profBuf).read 10ms 25.00% 100% 10ms 25.00% runtime.startm 0 0% 100% 10ms 25.00% log.(*Logger).Output 0 0% 100% 10ms 25.00% log.Println 0 0% 100% 10ms 25.00% main.main.func1 0 0% 100% 10ms 25.00% os.(*File).Write 0 0% 100% 10ms 25.00% runtime.(*mcache).nextFree 0 0% 100% 10ms 25.00% runtime.(*mcache).refill flat:该函数本身直接消耗的时间。比如,flat = 10ms 表示这个函数自身消耗了 10 毫秒的 CPU 时间,而不是它调用的其他函数所消耗的时间。 ...

October 23, 2024 · Last updated on August 1, 2025 · 4 min · KKKZOZ