zhangqian
2023-12-08 5220cfff6d68f24875c5ce832bbe4541b9fe6639
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package serf
 
import (
    "fmt"
    "github.com/spf13/viper"
)
 
type vasystem struct {
    ServerName string `mapstructure:"serverName"`
    ServerID   string `mapstructure:"analyServerId"`
}
 
var Vasystem = &vasystem{}
 
// Init is an exported method that takes the environment starts the viper
// (external lib) and returns the configuration struct.
func init() {
    var err error
    v := viper.New()
    v.SetConfigType("yaml")
    v.SetConfigName("pro")
    v.AddConfigPath("")
    v.AddConfigPath("../config/")
    v.AddConfigPath("./config/")
    v.AddConfigPath("/opt/vasystem/config/")
 
    err = v.ReadInConfig()
    if err != nil {
        fmt.Println("error on parsing configuration file", err)
    }
 
    read2Conf(v)
}
 
func read2Conf(v *viper.Viper) {
    v.UnmarshalKey("server", Vasystem)
    fmt.Println("ServerID:", Vasystem.ServerID)
}