How does sql.Open(...) work?

polaris · · 644 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<pre><code>import ( &#34;database/sql&#34; _ &#34;github.com/lib/pq&#34; ) func main() { sql.Open(&#34;postgres&#34;, &#34;user=pqgotest dbname=pqgotest sslmode=verify-full&#34;) } </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 &#34;postgres&#34; in an init function. That&#39;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&#39;s similar to Java, where database drivers are registered at runtime and you use the language&#39;s API to work with the driver.</p></pre>gchain: <pre><p>import _ &#34;github.com/lib/pq&#34; 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

644 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传