Component
A unique persistent instance of a component
There are several ways to load components.
Initialized
Components are loading when the app is initialized.
From Configuration
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
Get hello.Get().Message
from "app.yml".
From Parameters
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
Loading Components
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
Runtime
Components are loading at runtime. using langgo.LoadComponents()
method.
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