本文原始碼 https://github.com/tothis/go-manager
- Viper Hugo作者開發的配置插件 支持
JSONTOMLYAML… - GORM 資料庫ORM映射插件
- Gin Web插件
Viper讀取TOML
- config.toml
[MySQL]
userName = "root"
password = "123456"
host = "localhost"
port = 3306
dbName = "test"
arg = "charset=utf8mb4&parseTime=True&loc=Local"
- config.go
package main
import (
"fmt"
"github.com/spf13/viper"
"go-manager/model"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
// 讀取組態檔config
type Config struct {
MySQL MySQLConfig
}
type MySQLConfig struct {
Host string
Port uint16
DbName string
Arg string
UserName string
Password string
}
// 初始化配置
func initConfig() Config {
// 把組態檔讀取到結構體上
var config Config
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.ReadInConfig()
// 將組態檔系結到config上
viper.Unmarshal(&config)
fmt.Println("config : ", config)
return config
}
func main() {
fmt.Println(initConfig())
}
GORM配置和使用
...更多請看原始碼 https://github.com/tothis/go-manager
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/226922.html
標籤:區塊鏈
