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

app.yml
hello:
    message: "hello"
main.go
package main

import (
	"github.com/langwan/langgo"
	"github.com/langwan/langgo/components/hello"
)

func main() {
	langgo.Run(&hello.Instance{})
	fmt.Println(hello.Get().Message)
}
outout
hello

Get hello.Get().Message from "app.yml".

hello.Get() Get a unique persistent instance of a component.

From Parameters

main.go
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)
}
output
hello

Loading Components

app.yml
hello:
  message: "hello langgo"
sqlite:
  path: database.db
main.go
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)
}
output
hello
1

Runtime

Components are loading at runtime. using langgo.LoadComponents() method.

main.go
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)
}
output
hello

Last updated