keepeye / go-container Goto Github PK
View Code? Open in Web Editor NEWA simple container library implemented by go to facilitate the management of service objects.
License: MIT License
A simple container library implemented by go to facilitate the management of service objects.
License: MIT License
As an open-source project, It is necessary to format the code with gofmt
😄
// 注册一个数据库服务 不成功
container.Singleton("default_mysql", func(c *container.Container) interface{} {
db, err := gorm.Open(mysql.Open("abc:xyz@tcp(0.0.0.0)/t_test?parseTime=true&loc=Asia%2fShanghai"), &gorm.Config{
SkipDefaultTransaction: false,
NamingStrategy: schema.NamingStrategy{
SingularTable: true, // 禁用表名加s
},
Logger: logger.Default.LogMode(logger.Info), // 打印sql语句
DisableAutomaticPing: false,
DisableForeignKeyConstraintWhenMigrating: true, // 禁用创建外键约束
})
if err != nil {
panic(err)
}
// 注册释放函数
// add release function
//c.BeforeRelease(func() {
// db.Close()
//})
return &db
})
defaultMysql := container.Get("default_mysql")
fmt.Println(reflect.TypeOf(defaultMysql))
报错 # command-line-arguments
./container.go:106:15: defaultMysql.First undefined (type interface{} has no field or method First)
需要如何解决?
不用容器,实际测试gorm读写成功
//mydb, _ := gorm.Open(mysql.Open("abc:xyz@tcp(0.0.0.0)/t_test?parseTime=true&loc=Asia%2fShanghai"), &gorm.Config{
// SkipDefaultTransaction: false,
// NamingStrategy: schema.NamingStrategy{
// SingularTable: true, // 禁用表名加s
// },
// Logger: logger.Default.LogMode(logger.Info), // 打印sql语句
// DisableAutomaticPing: false,
// DisableForeignKeyConstraintWhenMigrating: true, // 禁用创建外键约束
//})
//var list1 PlanT
//mydb.First(&list1)
//fmt.Println(list1)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.