mirror of
https://github.com/go-micro/go-micro.git
synced 2025-08-10 21:52:01 +02:00
Add examples
This commit is contained in:
12
examples/config/file/config.json
Normal file
12
examples/config/file/config.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"hosts": {
|
||||
"database": {
|
||||
"address": "10.0.0.1",
|
||||
"port": 3306
|
||||
},
|
||||
"cache": {
|
||||
"address": "10.0.0.2",
|
||||
"port": 6379
|
||||
}
|
||||
}
|
||||
}
|
34
examples/config/file/main.go
Normal file
34
examples/config/file/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/micro/go-micro/v2/config"
|
||||
"github.com/micro/go-micro/v2/config/source/file"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// load the config from a file source
|
||||
if err := config.Load(file.NewSource(
|
||||
file.WithPath("./config.json"),
|
||||
)); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// define our own host type
|
||||
type Host struct {
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
}
|
||||
|
||||
var host Host
|
||||
|
||||
// read a database host
|
||||
if err := config.Get("hosts", "database").Scan(&host); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(host.Address, host.Port)
|
||||
}
|
Reference in New Issue
Block a user