func init() {
useConfig := flag.String("config", "", "using configuration file")
generateConfig := flag.Bool("gc", false, "generate config file")
flag.Parse()
fmt.Printf("config: %s\n", *useConfig)
if *useConfig != "" {
data, err := ioutil.ReadFile(*useConfig)
check.Require(err)
check.Require(config.Build(data))
} else {
data, err := configcenterAPI.GetConfig("wbs")
check.Require(err)
check.Require(config.Build(data))
}
if *generateConfig { //生成gc
data, _ := json.MarshalIndent(*config.Get(), "", "\t") //把資料庫config拿過來,JSON格式,不縮排,格式化
check.Require(ioutil.WriteFile("./config.conf", data, 0770)) //把前面的資料庫資料寫入config.conf,filemode 0770=擁有者及群組有rwx權限,io底層寫法
}
}
這是專案中一開始會init的東西,但我不想給他灌入config檔,他正常應該會進入到下面的程式去拿取本機address去建置mongodb
type MongoDBInfo struct {
Address string `json:"address"`
Database string `json:"db"`
User string `json:"user"`
Password string `json:"password"`
Description string `json:"desc"`
MaxPoolSize uint64 `json:"max_pool_size"`
RequestTimeout int64 `json:"request_timeout"`
ReplicaSet string `json:"replica_set"`
}
type Configuration struct {
SelfDB MongoDBInfo `json:"self_db_info"`
AppserverDB MongoDBInfo `json:"appserver_db_info"`
AgentDBs []MysqlDBInfo `json:"agent_db_infos"`
}
instance = &Configuration{ //如果資料空的就用以下默認值
SelfDB: MongoDBInfo{
Address: "127.0.0.1:27017", //192.168.96.163
Database: "reportdb",
MaxPoolSize: 512,
RequestTimeout: 20,
},
但我運行時打的指令如下,卻報錯
PS C:\Users\carry.chi\Desktop\walletbalanceserver-master\src> go run .\main.go
config:
panic: {{10002 <nil>} []}
goroutine 1 [running]:
walletbalanceserver/src/check.Require(...)
C:/Users/carry.chi/Desktop/walletbalanceserver-master/src/check/check.go:31
main.init.0()
C:/Users/carry.chi/Desktop/walletbalanceserver-master/src/main.go:36 +0x21f
exit status 2
我自己查了查,發覺可能是資料沒傳進來?我自己在CMD是有打開mongoDB的,但我不清楚是我命令列指令打錯還是其他原因搞得,有點沒頭緒。請各位指點明燈XD