日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

讀取Go項目中的配置文件的方法_Golang

作者:范閑 ? 更新時間: 2022-08-10 編程語言

Go語言提供了很簡便的讀取json和yaml文件的api,我們可以很輕松將一個json或者yaml文件轉換成Go的結構體,但是如果直接在項目中讀取配置文件,這種方式并不夠好。缺點如下:

實際開發中配置項在不同環境下,配置的值是不同的

上面的問題可以通過不同的配置文件去決定在那個環境讀取那個配置文件,但是還有一個問題就是實際開發中,配置項有些是相同的,有些是不同的,如果配置文件有一個主配置文件,里面存放的是不同環境相同的配置項,還有一個跟隨環境的配置文件,里面存放的是不同環境的配置項,然后讀取兩個配置文件后,做一個merge,這樣得到的結果就是總共的配置項信息。

有些配置項是必填的,有些配置項的值是一些特殊的值,比如,郵箱,手機號,IP信息等

來看看gonfig是怎么解決這個問題的

  • 安裝gonfig
go get github.com/xiao-ren-wu/gonfig
  • 項目中新建配置目錄,并編寫對應配置文件,在配置目錄同級目錄添加讀取配置文件的go文件

conf.yaml文件中存放的是通用的配置,conf-{{active}}.yaml中存放不同環境不同的配置信息。

conf
   |-conf.yaml
   |-conf-dev.yaml
   |-conf-prod.yaml
   |config.go
  • 利用go:embed將配置文件載入到內存,調用gonfig.Unmarshal讀取配置文件
package config

import (
   "model"
   "github.com/xiao-ren-wu/gonfig"
   "embed"
)

//go:embed *.yaml
var confDir embed.FS

// 我們配置文件的配置struct
type AppConf struct {
	AppName string `yaml:"app-name" json:"app-name"`
	DB      DBConf `yaml:"db" json:"db"`
}

type DBConf struct {
	Username string `yaml:"username" json:"username"`
	Password string `yaml:"password" json:"password"`
}

var conf Conf

func Init() {
   if err := gonfig.Unmarshal(confDir, &conf); err != nil {
      panic(err)
   }
}

func GetConf() *Conf {
   return &conf
}

這樣就完成了項目中配置文件的讀取,是不是很簡單? 此時讀到的配置形式是conf-{{profile}}.yamlconf.yaml的總和,如果conf-{{profile}}.yaml中定義的屬性和conf.yaml相同,那么會以conf-{{profile}}.yaml為準

約定

gonfigAPI的簡潔性的原因是因為背后做了很多約束,只有符合約束的配置才能被成功讀取,具體的約束條件如下:

  • gonfig.Unmarshal會默認讀取文件名稱有前綴conf的文件

  • 通過環境變量profile作為環境名稱,如果沒有配置,默認dev。

  • 程序會尋找conf.yaml作為主配置文件,conf-{{profile}}.yaml作為環境特有配置文件,然后對文件內容進行合并

  • 如果conf-{{profile}}.yaml中的屬性和conf.yaml中屬性都有定義,那么會以conf-{{profile}}.yaml為準。

根據項目定制化配置文件

gonfig.Unmarshal的函數簽名func Unmarshal(confDir ReadFileDir, v interface{}, ops ...Option) error 提供很多配置項,供用戶定制化需求,具體的配置信息如下:

  • 更改配置文件名稱前綴FilePrefix(prefix string)

  • 更改讀取配置文件類型UnmarshalWith(uType UnmarshalType)

  • 更改讀取的環境變量名稱ProfileUseEnv(envName, defaultProfile string)

  • 自定義設置profileProfileFunc(f func() string)

原理篇

gonfig的實現也很簡單,核心的源碼如下:

func Unmarshal(confDir ReadFileDir, v interface{}, ops ...Option) error {
	if v != nil && reflect.ValueOf(v).Kind() != reflect.Ptr {
		return gonfig_error.ErrNonPointerArgument
	}

	var cs = &confStruct{
		confPrefix:      "conf",
		envName:         "profile",
		defaultEnvValue: "dev",
		unmarshalType:   Yaml,
	}
	cs.activeProfileFunc = func() string {
		return getActiveProfile(cs.envName, cs.defaultEnvValue)
	}

	for _, op := range ops {
		op(cs)
	}

	cs.profileActive = cs.activeProfileFunc()

	if err := loadConf(confDir, cs); err != nil {
		return err
	}

	// copy val
	v1 := reflect.New(reflect.TypeOf(v).Elem()).Interface()

	if err := fileUnmarshal(cs.activeConfRaw, v1, cs.unmarshalType); err != nil {
		return err
	}

	if len(cs.masterConfRaw) == 0 {
		return gonfig_error.MasterProfileConfNotSetError
	}

	if err := fileUnmarshal(cs.masterConfRaw, v, cs.unmarshalType); err != nil {
		return err
	}

	return mergo.Merge(v, v1, mergo.WithOverride)
}

大概的原理就是復制了一份用戶傳給函數的結構體v1,結構體v1和v分別用于接收conf-{{profile}}.yaml中的屬性和conf.yaml的配置信息,然后通過調用三方開源庫mergo對兩個結構體的屬性做一個merge。

這就是關于gonfig的全部內容啦~~~

github地址是:https://github.com/xiao-ren-wu/gonfig

原文鏈接:https://juejin.cn/post/7109846075950759950

欄目分類
最近更新