github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/config/file/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/micro/go-micro/v2/config"
     7  	"github.com/micro/go-micro/v2/config/source/file"
     8  )
     9  
    10  func main() {
    11  	// load the config from a file source
    12  	if err := config.Load(file.NewSource(
    13  		file.WithPath("./config.json"),
    14  	)); err != nil {
    15  		fmt.Println(err)
    16  		return
    17  	}
    18  
    19  	// define our own host type
    20  	type Host struct {
    21  		Address string `json:"address"`
    22  		Port    int    `json:"port"`
    23  	}
    24  
    25  	var host Host
    26  
    27  	// read a database host
    28  	if err := config.Get("hosts", "database").Scan(&host); err != nil {
    29  		fmt.Println(err)
    30  		return
    31  	}
    32  
    33  	fmt.Println(host.Address, host.Port)
    34  }