组件
组件是唯一的持久化的实例
有多种方法加载组件
初始化
创建应用的时候加载组件。
来自配置
hello:
message: "hello"
package main
import (
"github.com/langwan/langgo"
"github.com/langwan/langgo/components/hello"
)
func main() {
langgo.Run(&hello.Instance{})
fmt.Println(hello.Get().Message)
}
hello
hello.Get().Message
来自 "app.yml".
来自参数
package main
import (
"github.com/langwan/langgo"
"github.com/langwan/langgo/components/hello"
)
func main() {
langgo.Run(&hello.Instance{Message: "hello"})
fmt.Println(hello.Get().Message)
}
hello
加载多个组件
hello:
message: "hello langgo"
sqlite:
path: database.db
package main
import (
"fmt"
"github.com/langwan/langgo"
"github.com/langwan/langgo/components/hello"
"github.com/langwan/langgo/components/sqlite"
)
func main() {
langgo.Run(&hello.Instance{}, &sqlite.Instance{Path: "./database.db"})
fmt.Println(hello.Get().Message)
var i int64
sqlite.Get().Raw("SELECT 1").Scan(&i)
fmt.Println(i)
}
hello
1
运行时
使用 langgo.LoadComponents()
方法加载组件。
package main
import (
"fmt"
"github.com/langwan/langgo"
"github.com/langwan/langgo/components/hello"
"github.com/langwan/langgo/components/sqlite"
)
func main() {
langgo.Run(&sqlite.Instance{Path: "./database.db"})
langgo.LoadComponents(&hello.Instance{Message: "hello langgo"})
fmt.Println(hello.Get().Message)
}
hello
Last updated