Giter Site home page Giter Site logo

go-container's People

Contributors

keepeye avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

go-container's Issues

format the code

As an open-source project, It is necessary to format the code with gofmt 😄

绑定gorm服务后,无法使用gorm实例

// 注册一个数据库服务 不成功
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.