我刚开始学习Go。当天的课程是将我的数据库处理程序包装在结构中以避免使用全局范围变量。以为我理解它到目前为止,并希望延迟Close()方法,就像我以前那样,以堆栈溢出结束。延迟结构sqlx.Close()以堆栈溢出结束
我找不到解释为什么会发生这种情况,也没有什么解决方法。
这里是关键代码:
package exporter
type DB struct {
*sqlx.DB
queriesExecuted int
}
func Open(dataSourceName string) *DB {
connection := sqlx.MustConnect("mysql", dataSourceName)
db := &DB{connection, 0}
return db
}
func (db *DB) Close() {
db.Close() // this is where the stack growth happens
}
func (db *DB) GetArticles() []oxarticle {
...
}
package main
func main() {
exporter := feedexporter.Open("root:[email protected]/feedexport")
defer exporter.Close()
articles := exporter.GetArticles()
}
一切正常,无延迟exporter.Close(),包括它结束在:
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
感觉如此不好关闭连接;)处理这个问题的方法是什么?