1. 首页
  2. 主题
  3. Go问与答

conn.(*dbConnection).ID 括号中的这种用户没懂,请指点

simongaoo · · 1703 次点击
```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
更多评论
polaris
社区,需要你我一同完善!
conn 是接口,conn.(*dbConnection) 是类型断言。官方文档:http://docs.studygolang.com/ref/spec#Type_assertions 注:发布帖子时,底下那么大的预览效果,格式乱了也没看到么?
#1

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中