```go
func abc() interface{} {
blogRows, _ := models.Db.Query("select * from blog where id=1")
defer blogRows.Close()
catRows, _ := models.Db.Query("select * from category where id=1")
defer catRows.Close()
return 1
}
```
这样并没有实现我想要的释放掉两个资源,似乎只释放了一个。于是我又做了以下实验:
```go
func abc() interface{} {
blogRows, _ := models.Db.Query("select * from blog where id=1")
blogRows.Close()
catRows, _ := models.Db.Query("select * from category where id=1")
defer catRows.Close()
return 1
}
```
这样就都能释放
```go
func abc() interface{} {
blogRows, _ := models.Db.Query("select * from blog where id=1")
defer blogRows.Close()
catRows, _ := models.Db.Query("select * from category where id=1")
catRows.Close()
return 1
}
```
这样同样只释放了一个
```go
func abc() interface{} {
blogRows, _ := models.Db.Query("select * from blog where id=1")
blogRows.Close()
catRows, _ := models.Db.Query("select * from category where id=1")
catRows.Close()
return 1
}
```
这样当然肯定是没有问题的,都能释放
请教各位大神,这个defer在这里面究竟是个什么原理。
有疑问加站长微信联系(非本文作者)