🎉 很高兴宣布 Go-Spring 全新版本正式发布! 这是一个融合了 Java Spring 生态成熟理念 🌱 与 Go 语言高性能特性 ⚡ 的现代框架, 旨在为 Go 开发者 👨 💻👩 💻 提供更高效、更优雅的应用开发体验 ✨。
🌟 框架简介
Go-Spring 是一个面向现代 Go 应用开发的高性能框架,灵感源自 Java 社区的 Spring / Spring Boot。 它的设计理念深度融合 Go 语言的特性,既保留了 Spring 世界中成熟的开发范式,如依赖注入(DI)、自动配置和生命周期管理, 又避免了传统框架可能带来的繁复和性能开销。 Go-Spring 让开发者能够在保持 Go 原生风格与执行效率的前提下,享受更高层次的抽象与自动化能力。
无论你是在开发单体应用,还是构建基于微服务的分布式系统,Go-Spring 都提供了统一且灵活的开发体验。 它以“开箱即用”的方式简化了项目搭建流程,减少模板代码的编写需求,并且不强加侵入式的框架结构,让开发者可以更专注于业务逻辑的实现。 Go-Spring 致力于提升开发效率、可维护性和系统的一致性,是 Go 语言生态中一个具有里程碑意义的框架。
🚀 特性一览
Go-Spring 提供了丰富而实用的特性,帮助开发者高效构建现代 Go 应用:
-
⚡ 极致启动性能
<ul style="list-style-type:disc"> <li> 基于 Go 的<span> </span><code>init()</code><span> </span>机制进行 Bean 注册,无运行时扫描,启动迅速; </li> <li> 注入仅依赖初始化阶段的反射,运行时零反射,保障性能最大化。 </li> </ul> </li> <li> <p style="color:black; margin-left:0; margin-right:0">🧩<span> </span><strong style="color:black">开箱即用、无侵入式设计</strong> <ul style="list-style-type:disc"> <li> 支持结构体标签注入与链式配置,无需掌握复杂概念即可使用; </li> <li> 不强依赖接口或继承结构,业务逻辑保持原生 Go 风格。 </li> </ul> </li> <li> <p style="color:black; margin-left:0; margin-right:0">🔄<span> </span><strong style="color:black">配置热更新,实时生效</strong> <ul style="list-style-type:disc"> <li> 多格式、多来源配置加载,支持环境隔离与动态刷新; </li> <li> 配置变更可即时应用,便于灰度发布与在线调参。 </li> </ul> </li> <li> <p style="color:black; margin-left:0; margin-right:0">⚙️<span> </span><strong style="color:black">灵活依赖注入机制</strong> <ul style="list-style-type:disc"> <li> 支持构造函数注入、结构体字段注入、构造函数参数注入多种方式; </li> <li> 注入行为可按配置项或运行环境灵活调整。 </li> </ul> </li> <li> <p style="color:black; margin-left:0; margin-right:0">🔌<span> </span><strong style="color:black">多模型服务启动支持</strong> <ul style="list-style-type:disc"> <li> 内建 HTTP Server 启动器,快速部署 Web 服务; </li> <li> 支持<span> </span><code>Runner</code>、<code>Job</code>、<code>Server</code><span> </span>三类运行模型,适配不同服务形态; </li> <li> 生命周期钩子完备,支持优雅启停。 </li> </ul> </li> <li> <p style="color:black; margin-left:0; margin-right:0">🧪<span> </span><strong style="color:black">内建测试能力</strong> <ul style="list-style-type:disc"> <li> 与<span> </span><code>go test</code><span> </span>无缝集成,支持 Bean Mock 和依赖注入,轻松编写单元测试。 </li> </ul> </li>
📦 安装方式
Go-Spring 使用 Go Modules 管理依赖,安装非常简单:
go get github.com/go-spring/spring-core
🚀 快速开始
Go-Spring 主打“开箱即用”,下面通过两个示例,快速感受其强大能力。
更多示例请见:gs/examples
示例一:最小 API 服务
func main() {
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world!"))
})
gs.Run()
}
访问方式:
curl http://127.0.0.1:9090/echo
# 输出: hello world!
✅ 无需繁杂配置,Go 标准库 http
可以直接使用; ✅ gs.Run()
接管生命周期,支持优雅退出、信号监听等能力。
示例二:基础特性展示
func init() {
gs.Object(&Service{})
gs.Provide(func (s *Service) *http.ServeMux {
http.HandleFunc("/echo", s.Echo)
http.HandleFunc("/refresh", s.Refresh)
return http.DefaultServeMux
})
sysconf.Set("start-time", time.Now().Format(timeLayout))
sysconf.Set("refresh-time", time.Now().Format(timeLayout))
}
const timeLayout = "2006-01-02 15:04:05.999 -0700 MST"
type Service struct {
StartTime time.Time`value:"${start-time}"`
RefreshTime gs.Dync[time.Time] `value:"${refresh-time}"`
}
func (s *Service) Echo(w http.ResponseWriter, r *http.Request) {
str := fmt.Sprintf("start-time: %s refresh-time: %s",
s.StartTime.Format(timeLayout),
s.RefreshTime.Value().Format(timeLayout))
w.Write([]byte(str))
}
func (s *Service) Refresh(w http.ResponseWriter, r *http.Request) {
sysconf.Set("refresh-time", time.Now().Format(timeLayout))
gs.RefreshProperties()
w.Write([]byte("OK!"))
}
访问方式:
curl http://127.0.0.1:9090/echo # 查看当前时间
curl http://127.0.0.1:9090/refresh# 触发热刷新
✅ value
标签自动绑定配置; ✅ gs.Dync[T]
实现字段热更新; ✅ gs.Object
gs.Provide()
注册 Bean。
📚 与其他框架的对比
Go-Spring 具备以下几个显著优势:
功能点 | Go-Spring | Wire | fx | dig |
---|---|---|---|---|
运行时 IoC 容器 | ✓ | ✗ | ✓ | ✓ |
编译期校验 | 部分支持 | ✓ | ✗ | ✗ |
条件 Bean 支持 | ✓ | ✗ | ✗ | ✗ |
动态配置能力 | ✓ | ✗ | ✗ | ✗ |
生命周期管理 | ✓ | ✗ | ✓ | ✗ |
属性绑定 | ✓ | ✗ | ✗ | ✗ |
零结构体侵入(无需修改原结构体) | ✓ | ✓ | ✗ | ✓ |
📱 微信公众号
更多内容请访问 Go-Spring 官方仓库: https://github.com/go-spring/spring-core