package
0.0.0-20240707130328-7cf8dae7750a
Repository: https://github.com/nite-coder/blackbear.git
Documentation: pkg.go.dev

# README

配置管理

介紹

用來管理系統配置,目前支援 環境變數, 文件檔案 等模組, 執行順序依照加入模組的先後

  • 環境變數: 讀取環境變數
  • 文件檔案: 讀取檔案內容,支援 YAML, JSON 等格式

使用方式

範例配置文件 (app.yml)

app_id: blackbear
web:
  port: 10080
  ping: true
  1. 設定 文件檔案 模組
    fileProvder := file.New()
    fileProvder.SetConfigName("config.yml") // 手動修改為讀取 config.yml, 預設讀取 app.yml 檔名
    fileProvder.SetConfigType("yaml") // 如果檔案沒有附檔名需要設定,支援 yaml
    err := fileProvder.Load() // 如果目錄下都找不到配置檔,ErrFileNotFound 會被回傳
    if err != nil {
        return err
    }

    config.AddProvider(fileProvider)
  1. 讀取配置內容
    fileProvder := file.New()
    err := fileProvder.Load()
    if err != nil {
        return err
    }
    config.AddProvider(fileProvider)
    appID, err := config.String("app.id") // case casesentive
    fmt.Print(appID) // print: blackbear

    port, err := config.Int32("web.port", 10080) // 設定預設值,如果 "web.port" 這個 key 找不到, 就會回傳 "10080"
    fmt.Print(port) // print: 10080

更新檢查

目前是採用緩存機制,如果已經有內容被讀入就會被緩存已提升後續的效能

RoadMap

  1. File Watch
  2. 配置檔的繼承模式
  3. integrate remote config system

# Packages

No description provided by the author

# Functions

No description provided by the author
Bool returns a boolean type value which has the key.
Bool returns a boolean type value which has the key.
Float32 returns a float32 type value which has the key.
Float64 returns a float64 type value which has the key.
Float64Slice returns the value associated with the key as a slice of float.
Int returns a int type value which has the key.
Int32 returns a int32 type value which has the key.
Int64 returns a int64 type value which has the key.
IntSlice returns the value associated with the key as a slice of int.
No description provided by the author
No description provided by the author
Scan binds a value which has the key.
String returns a string type value which has the key.
StringMap returns a map[string]interface{} type value which has the key.
StringMapString returns a map[string]string type value which has the key.
StringSlice returns the value associated with the key as a slice of strings.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author