<pre><code>import (
"database/sql"
_ "github.com/lib/pq"
)
func main() {
sql.Open("postgres", "user=pqgotest dbname=pqgotest sslmode=verify-full")
}
</code></pre>
<p>How does <code>sql</code> package instantiate <code>pq</code> driver via <code>sql.Open</code>?</p>
<p>Is there a package-level interface?</p>
<hr/>**评论:**<br/><br/>gureggu: <pre><p>It associates itself with "postgres" in an init function. That's why you have to import it. </p>
<p><a href="https://github.com/lib/pq/blob/a8d8d01c4f91602f876bf5aa210274e8203a6b45/conn.go#L43-L45">https://github.com/lib/pq/blob/a8d8d01c4f91602f876bf5aa210274e8203a6b45/conn.go#L43-L45</a></p></pre>SBSTP: <pre><p>It's similar to Java, where database drivers are registered at runtime and you use the language's API to work with the driver.</p></pre>gchain: <pre><p>import _ "github.com/lib/pq" will basically register <code>pq</code> driver [1] by calling <code>sql.Register()</code>.</p>
<p>[1] <a href="https://github.com/lib/pq/blob/master/conn.go#L43-L45" rel="nofollow">https://github.com/lib/pq/blob/master/conn.go#L43-L45</a>
[2] <a href="http://golang.org/pkg/database/sql/#Register" rel="nofollow">http://golang.org/pkg/database/sql/#Register</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传