1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package global
- import (
- "bigdata_archives/configs"
- "fmt"
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
- "gorm.io/gorm/logger"
- "log"
- "runtime"
- )
- var (
- DBLink *gorm.DB
- )
- func SetupDBLink() error {
- var err error
- dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?%s",
- configs.Config.GetString("database.userName"),
- configs.Config.GetString("database.password"),
- configs.Config.GetString("database.host"),
- configs.Config.GetString("database.db"),
- configs.Config.GetString("database.otherParams"),
- )
- //判断当前系统是windows还是linux
- //runtime获取的是当前Go语言框架的参数,不是实际运行的操作系统的参数
- sysType := runtime.GOOS
- if sysType == "windows" {
- DBLink, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
- DisableForeignKeyConstraintWhenMigrating: true,
- Logger: logger.Default.LogMode(logger.Info),
- })
- log.Print("当前系统为windows")
- } else if sysType == "linux" {
- DBLink, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
- }
- if err != nil {
- return err
- }
- return nil
- }
|