5.6 KiB
Executable File
5.6 KiB
Executable File
tags: [software development, go]
name: go-learning
description: "Go 语言学习路径与实战 — 环境配置(go1.23.5)、goroutine+channel并发、HTTP服务、Worker消息队列。已集成Muchen-Watchdog实战项目(自我感知守护进程)。参考: go-code-patching.md。"
related_skills:
- zhiyi: "织忆系统用Go编写 — zhiyid daemon + Rust sidecar"
- muchen: "Muchen-Watchdog — Go实战项目"
triggers:
- 想学 Go
- 开始学 Go
- 需要 Go 环境配置
version: 1.0.0
last_reviewed: 2026-07-09
usage_count: 5+
quality_score: 6.5
test_cases:
- "配置Go环境" → "下载go.tar.gz → 配置path → go version验证"
- "写HTTP服务" → "go mod init → 写handler → go run验证"
error_handling:
- "patch工具对tab缩进敏感失败" → "用Python字节级patch"
- "binary部署后未更新" → "stat对比binary mtime vs commit时间"
changelog:
- version 1.0.0: "首次规范化,增加元数据"
Go 语言学习
环境配置
# 下载(用国内源)
cd /tmp && curl -L -o go.tar.gz https://mirrors.ustc.edu.cn/golang/go1.23.5.linux-amd64.tar.gz
tar -xzf go.tar.gz -C /home/muc/
# 添加到 .profile
export GOROOT=/home/muc/go
export GOPATH=/home/muc/gopath
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
# 验证
go version # go1.23.5 linux/amd64
学习里程碑
| 里程碑 | 完成标准 |
|---|---|
| M1 | go run 能跑通 |
| M2 | goroutine + channel 会用 |
| M3 | HTTP 服务能写 |
| M4 | Worker 消息队列系统能写 |
| M5 | Muchen 版本迁移工具完成 |
第一个程序
mkdir -p /home/muc/gopath/src/hello
cd /home/muc/gopath/src/hello
go mod init hello
cat > main.go << 'EOF'
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
EOF
go run .
关键语法点
package main+func main()= 入口go mod init= 初始化模块goroutine=go func()channel=make(chan int)go build= 编译二进制
⚠️ 陷阱与常见问题
真正的学习不是打印标签
学习引擎的目标是做事,不是打印"goroutine+channel"。perpetual_worker(已废弃)犯了错误:每5秒写文件但不执行、不理解、不积累。正确的学习 = 代码写出来 + 执行 + 结果验证 + 存入记忆。
M2: goroutine + channel 会用
goroutine 练习代码(已验证可运行):
conc.go— 并发文件遍历(3 worker + channel)http.go— 简单 HTTP 服务
M3: HTTP 服务能写
HTTP 服务代码 http_server.go:
package main
import ("net/http";"fmt";"sync")
var count int
var mu sync.Mutex
func h(w http.ResponseWriter,r *http.Request){mu.Lock();count++;fmt.Fprintf(w,"Req#%d",count);mu.Unlock()}
func main(){http.HandleFunc("/",h);http.HandleFunc("/health",func(w http.ResponseWriter,r *http.Request){fmt.Fprint(w,"OK")});fmt.Println(":8090");http.ListenAndServe(":8090",nil)}
验证:curl localhost:8090/health
M4: Worker 消息队列系统
待实现。
M5: Muchen 版本迁移工具完成
待实现。
学习资源
- tour.golang.org(官方教程)
- gobyexample.com(边学边练)
- The Go Programming Language(Alan A.A.,经典书)
进度追踪
- 进度文件:
/home/muc/gopath/src/hello/progress.md - Go 环境:GOROOT=/home/muc/go, GOPATH=/home/muc/gopath
- 第一个程序:/home/muc/gopath/src/hello/main.go ✅
- 并发练习:/home/muc/gopath/src/learn/conc.go ✅
- HTTP 服务:/home/muc/gopath/src/learn/http_server.go ✅
Go 用于 Muchen
Muchen-Watchdog(自我感知守护进程)是 Go 的第一个实战项目:
- 每秒心跳,不靠 cron
- 系统监控感受器官
- 飞书主动告警
- 行为日志
这是 Go 学习的正确方向:做真实有用的东西,不是空转的练习代码。
为什么学 Go(来自 Muchen 架构)
构建能自己进化的系统需要:
- 能编译成独立二进制(不依赖 Python 运行时)
- 原生并发(goroutine/channel 设计给分布式系统用)
- 简单部署(未来"游走于网络"需要轻量载体)
- 比 C 简单,比 Rust 上手快
Go 是 Muchen 版本迁移工具的首选语言。
参考
- Go 代码修改技术要点 →
references/go-code-patching.md(缩进规范、字节级 patch、binary 部署验证、consolidation pipeline 两种模式) - Muchen-Watchdog → muchen skill(muchen 自我感知守护进程,Go 实战项目)
- perpetual_worker 已废弃 →
/home/muc/gopath/src/learn/perpetual_worker.go是错误案例(空转无意义)
⚠️ 陷阱与常见问题(续)
Go 代码 patch 失败用字节级 Python
patch 工具对 tab 缩进敏感,失手就报 diff 不匹配。遇到这种情况:
python3 -c "with open('file.go','rb') as f: ..."读原始字节- 用
\x09数量定位目标行 - 替换后
go build ./...验证 - 不要反复用 patch 工具重试 — 会越修越乱
详见 references/go-code-patching.md 第 1-2 节。
Consolidation 跑的是 cluster_only 而非 full
症状:journal 里每分钟都在跑 [consolidation] Rust sidecar 完成 但从不触发 decay/quality。
根因:consolPipe.Run() 默认 cluster_only,跳过了 prune/decay/quality。修复方法见 references/go-code-patching.md 第 4 节。
Binary 部署后没更新
症状:改了代码、build 成功,但服务行为没变。
根因:restart 前没有确认 binary mtime > latest commit。验证:
stat --format='%y' /home/muc/.local/bin/zhiyid
git log -1 --format="%ci" HEAD
# 必须 binary 时间 > commit 时间才算生效
find -printf "%T+" 对普通文件可能不准,用 stat。