帮你理解golang与AI Agent
·
本质上不是一个大模型,而是一个以 LLM 为决策核心,以 Go 服务为执行核心的智能系统。
Go 更多承担的是 Agent Runtime,也就是:
- Workflow 编排
- Tool 调度
- MCP Server
- Memory 管理
- RAG 检索
- 并发执行
- 状态管理
Go 为什么适合 Agent
这里就是 Go 面试最喜欢问的。
① Goroutine
Agent 最大特点就是:
大量 IO。
例如:
查数据库
查ES
查知识库
查天气
查CRM
调用ERP
这些都是 IO。
Go 可以:
go searchDoc()
go searchSQL()
go searchCRM()
最后:
WaitGroup
一起汇总。
所以一个 Agent 可以同时调用很多 Tool。
② Context
Agent 生命周期非常长。
例如:
用户
↓
思考
↓
Tool1
↓
Tool2
↓
LLM
↓
Tool3
↓
总结
可能几十秒。
Go 的 Context 非常适合:
超时
取消
Trace
日志
RequestID
例如:
ctx, cancel := context.WithTimeout(...)
整个 Agent 都能共享。
③ Channel
多个 Tool 返回:
SQL
ES
RAG
API
可以:
channel
↓
聚合
↓
LLM
而不是一直阻塞等待。
④ Interface
Go Tool 很容易抽象。
例如:
type Tool interface{
Name() string
Run(ctx,input string)(string,error)
}
以后:
SQL Tool
Weather Tool
Email Tool
CRM Tool
全部统一管理。
⑤ 高并发
Agent 经常:
1000+
用户同时聊天。
Go:
GMP 调度
M:N
内存占用低。
非常适合。
MCP 只是 Tool 的一种实现方式。
更多推荐



所有评论(0)