鸿蒙传说
I used it like this :----app.config{appname = firstapp# httpport = 8080runmode = dev //when in test comment this line#runmode = test //when in test uncomment this lineautorender = falsecopyrequestbody = trueEnableDocs = true[dev]httpport = 8080# [prod]# httpport = 8088[test]httpport = 8888}-----main.gofunc init() {// 1 - Enable debugorm.Debug = false// 2 - Register object with Beego ORMorm.RegisterModel(new(models.User), new(models.Role), new(models.Function), new(models.Role_function))//orm.RegisterModel(new(models.User), new(models.Role))if beego.BConfig.RunMode == "dev" { // 3 - Fetch database properties stored as YAML, decode secrets connStr, err := util.FetchYAMLDEV() if err != nil { log.Fatalf("error: %v", err) } // 4 - Register posgres driver and db orm.RegisterDriver("postgres", orm.DRPostgres) orm.RegisterDataBase("default", "postgres", connStr) // 5 - Create and load DB with sample data, can comment after initial load models.CreateDb() models.LoadDb()}if beego.BConfig.RunMode == "test" { // 3 - Fetch database properties stored as YAML, decode secrets fmt.Println("in test runmofe") connStr, err := util.FetchYAMLTEST() if err != nil { log.Fatalf("error: %v", err) } // 4 - Register posgres driver and db orm.RegisterDriver("postgres", orm.DRPostgres) orm.RegisterDataBase("default", "postgres", connStr) // 5 - Create and load DB with sample data, can comment after initial load models.CreateDb() models.LoadDb()}}----util.FetchYAML.gopackage utilimport ( "fmt" "os" "log" "github.com/joho/godotenv")func goDotEnvVariable(key string) string {// load .env fileerr := godotenv.Load(".env")if err != nil { log.Fatalf("Error loading .env file")}return os.Getenv(key)}func FetchYAMLDEV() (string, error) {user := goDotEnvVariable("user_dev")dbname := goDotEnvVariable("dbname_dev")password := goDotEnvVariable("password_dev")host := goDotEnvVariable("host_dev")port := goDotEnvVariable("port_dev")return fmt.Sprintf("user=%s dbname=%s password=%s host=%s port=%s sslmode=disable", user, dbname, password, host, port), nil}func FetchYAMLTEST() (string, error) {user := goDotEnvVariable("user")dbname := goDotEnvVariable("dbname")password := goDotEnvVariable("password")host := goDotEnvVariable("host")port := goDotEnvVariable("port")return fmt.Sprintf("user=%s dbname=%s password=%s host=%s port=%s sslmode=disable", user, dbname, password, host, port), nil}----.env ACCESS_SECRET = vjhbjhjhuser = postgresdbname = golangpassword = passwordport = 5432host = localhostsslmode = disableuser_dev = devuserdbname_dev = defdbnamepassword_dev = devpassport_dev = 5432host_dev = devhostsslmode_dev = disableuser_test = testuserdbname_test = testdbnamepassword_test = testpassport_test = 5432host_test = testhostsslmode_test = disable