```go
// performQueries tests the resource pool of connections.
func performQueries(query int, p *pool.Pool) {
// Acquire a connection from the pool.
conn, err := p.Acquire()
if err != nil {
log.Println(err)
return
}
// Release the connection back to the pool.
defer p.Release(conn)
// Wait to simulate a query response.
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
log.Printf("Query: QID[%d] CID[%d]\n", query, conn.(*dbConnection).ID)
}
```
以上为go in action 中的一段,请教conn.(*dbConnection).ID怎么理解?
我能看出是什么意思,但是没看到官方解释。
```go
if dbConn, ok := conn.(*dbConnection); ok {
log.Printf("Query: QID[%d] CID[%d]\n", query, dbConn.ID)
}
```
:smiley:
#5
更多评论
conn 是接口,conn.(*dbConnection) 是类型断言。官方文档:http://docs.studygolang.com/ref/spec#Type_assertions
注:发布帖子时,底下那么大的预览效果,格式乱了也没看到么?
#1