的实现表明,在实际的开发过程中,我们不可避免地会用到MySQL、Redis等服务。为了实现系统的配置,我们会将一些配置信息单独放在一些文件中,在使用的地方直接读取配置文件。常见的文件配置方式有很多,比如json、tomal、yml或者text格式。下面是几种方法的演示。演示代码JSON配置首先我们创建一个JSON文件,里面配置我们需要的参数格式,例如:{"host":"127.0.0.1","user":"root","password":"123456","port":"3306","db":"demo"}读取配置文件,需要用到Golang自带的json包。具体读取过程:读取json文件内容->使用json包进行反序列化->使用变量存储反序列化后的数据。//使用struct定义json格式和存储。typeDbJsonstruct{Hoststring`json:"host"`Userstring`json:"user"`Passwordstring`json:"password"`Portstring`json:"port"`Dbstring`json:"db"`}//分析funcGetJsonConfig(){//1。读取json文件文件内容,err:=ioutil.ReadFile("./config/json.json")iferr!=nil{fmt.Println("err1",err)return}db:=new(DbJson)//2。反序列化读取的json文件内容;会得到一个[]byte类型的sliceerr=json.Unmarshal(file,db)iferr!=nil{fmt.Println("err2",err)return}//2.1反序列化读取的json文件内容,复制map[string][]byte(效果同2)allConfig:=make(map[string]json.RawMessage,0)err=json.Unmarshal(file,&allConfig)iferr!=nil{fmt.Println("err3",err)return}//3.循环映射内容fork,v:=rangeallConfig{fmt.Println(k,string(v))//值为[]byte类型,转成string}}2和2.1其实是不同的实现。yml配置yml格式也是我们常用的文件配置格式。在Golang中,我们读取这个配置,主要是使用gopkg.in/yaml.v2包。同样,我们需要读取配置文件->解析文件内容。我们创建一个yml.yml文件,写入如下示例配置:host:127.0.0.1user:rootpassword:123456port:3306db:demo需要注意的是yml配置项:和value之间有一个空格。//定义一个struct来定义格式`}funcGetYmlConfig(){//1。读取配置文件内容,返回一个[]byte内容文件,err:=ioutil.ReadFile("./config/yml.yml")iferr!=nil{return}db:=new(DbYml)//2.使用yaml包反序列化err=yaml.Unmarshal(file,db)iferr!=nil{return}fmt.Println(db.Host,db.User,db.Password,db.Port,db.Db)}最终输入结果:127.0.0.1root1234563306demotextformat读取文件格式的内容,也就是逐行读取,然后分析每一行的内容。因为我们文本中的格式一般都是sequentialkey=value的格式,所以我们只需要读取换行的内容,然后按照=来划分即可。首先,我们创建一个内容为.txt的文件。大致内容如下:host=127.0.0.1user=rootpassword=123456port=3306db=demo具体阅读配置代码:funcGetKeyValue(){allConfig:=make(map[string]string)//1.读取文件并获取文件句柄open,err:=os.Open("./config/key.txt")iferr!=nil{fmt.Println("err1",err)return}//2.读取文件内容content:=bufio.NewReader(open)for{//3.读文件内容行,_,err:=content.ReadLine()iferr!=nil{iferr==io.EOF{//读到最后,跳出循环读break}return}//4.处理每一行读入的文件内容s:=strings2.TrimSpace(string(line))//去掉左右空格index:=strings2.Index(s,"=")//因为配置的是=,所以找到theindexpositionof=ifindex<0{continue}key:=strings2.TrimSpace(s[:index])//截取=keyiflen(key)==0{continue}value:=strings2左边的值。TrimSpace(s[index+1:])//截取=右侧的值为valueiflen(value)==0{continue}allConfig[key]=value//添加到map中,key是key的map,value为map的值}fork,v:=rangeallConfig{fmt.Println(k,string(v))}deferopen.Close()//关闭文件}输出内容大致如下:host127.0.0.1userrootpassword123456port3306dbdemotomal使用的是toml格式的配置文件,主要是使用toml包进行解析。同样,首先我们加载文件并将文件的路径传递到toml包中。首先我们创建一个toml文件,定义如下:[database]host="127.0.0.1"user="root"password="123456"port=[3306,3307]db="demo"下面是具体解析代码:import("github.com/BurntSushi/toml""path/filepath")typeDbTomlstruct{DbDatabase`toml:"database"`}typeDatabasestruct{HoststringUserstringPasswordstringPort[]int32Dbstring}funcGetToml(){//1.定义结构变量以接收Parsed数据varconfigDbToml//2。获取文件fileName的绝对路径,err:=filepath.Abs??("./config/toml.toml")iferr!=nil{fmt.Println("err1",err)return}//3.传入文件路径按照tomlpackage_,err1:=toml.DecodeFile(fileName,&config)iferr1!=nil{fmt.Println("err2",err1)return}fmt.Println(config.Db.Host,config.Db.User,config.Db.Password,config.Db.Port[0],config.Db.Db)}输出如下:127.0.0.1root1234563306demo
