获取配置例子
package main import ( "flag" "fmt" "runtime" "github.com/spf13/viper" ) type Config struct { Base BaseConf `mapstructure:"base"` } type BaseConf struct { pidfile string `mapstructure:"pidfile"` } var ( Conf *Config confPath string ) func init() { flag.StringVar(&confPath, "d", "./", " set logic config file path") } func InitConfig() (err error) { Conf = NewConfig() viper.SetConfigName("logic") // 配置文件的名字 viper.SetConfigType("toml") // 配置文件的类型 viper.AddConfigPath(confPath) // 配置文件的路径 if err := viper.ReadInConfig(); err != nil { return err } if err := viper.Unmarshal(&Conf); err != nil { panic(fmt.Errorf("unable to decode into struct: %s \n", err)) } return nil } func NewConfig() *Config { return &Config{ Base: BaseConf{ Pidfile: "/tmp/comet.pid", }, } } # toml 文件内容为 [base] pidfile = "/tmp/logic.pid"
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- RecyclerView使用指南(一)—— 基本使用
- 如何使用Meteorjs使用URL参数
- 使用 defer 还是不使用 defer?
- 使用 Typescript 加强 Vuex 使用体验
- [译] 何时使用 Rust?何时使用 Go?
- UDP协议的正确使用场合(谨慎使用)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深入Linux内核架构
Wolfgang Mauerer / 郭旭 / 人民邮电出版社 / 201005 / 149.00元
众所周知,Linux操作系统的源代码复杂、文档少,对程序员的要求高,要想看懂这些代码并不是一件容易事。本书结合内核版本2.6.24源代码中最关键的部分,深入讨论Linux内核的概念、结构和实现。具体包括进程管理和调度、虚拟内存、进程间通信、设备驱动程序、虚拟文件系统、网络、时间管理、数据同步等方面的内容。本书引导你阅读内核源代码,熟悉Linux所有的内在工作机理,充分展现Linux系统的魅力。 ......一起来看看 《深入Linux内核架构》 这本书的介绍吧!