How do I share a reference to a gorethink.session between multiple modules?

agolangf · · 612 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>For context, I&#39;m a Python and JavaScript programmer making the jump to Go (and I love what I&#39;ve seen so far). Let me know if this is the wrong forum to ask for help, thanks!</p> <p>I&#39;m trying to create a webapp using Go, and I want to open a singleton-like connection to my RethinkDB database using gorethink. So I created a module called <code>common</code> inside my app. I figured I could do something like:</p> <pre><code>package common import ( // RethinkDB connector r &#34;github.com/dancannon/gorethink&#34; &#34;github.com/golang/glog&#34; ) var session *r.session = nil func init() { session, err := r.Connect(r.ConnectOpts{ Address: &#34;localhost:28015&#34;, // Database: &#34;coffeehouse&#34;, DiscoverHosts: true, }) if err != nil { glog.Fatalf(err.Error()) } } func getSession() *r.session { if session == nil { init() } return session } </code></pre> <p>But Go complains: <code>cannot refer to unexported name gorethink.session</code>. I sort of understand the error, but I have no idea how to fix. Any thoughts? Should I refactor?</p> <hr/>**评论:**<br/><br/>callumjones: <pre><p>You need to refer to </p> <pre><code>r.Session </code></pre> <p>In Go anything that begins with a lower case isn&#39;t exposed publicly (that&#39;s why you see the unexported error)</p></pre>simplyh: <pre><p>This worked perfectly thanks! I knew about the lowercase rule but I&#39;m confused about the existence of both r.session and r.Session. </p></pre>elithrar_: <pre><p>Also note that you&#39;re shadowing your global—fix this by assigning to your global rather than init-and-assign:</p> <pre><code> var err error // Note the &#39;=&#39; and not the &#39;:=&#39; session, err = r.Connect(r.ConnectOpts{ Address: &#34;localhost:28015&#34;, // Database: &#34;coffeehouse&#34;, DiscoverHosts: true, }) </code></pre></pre>simplyh: <pre><p>Caught this while compiling :) Go&#39;s compiler rocks.</p></pre>doliner: <pre><p>Gorethink has a built in connection pool which you could use with a <code>MaxOpen</code> set to <code>1</code> to get the same effect. The readme on the the GH repo has a better description of this than I can provide here so I&#39;d recommend reading that. Hope this helps.</p> <p>Edit: The advice above is probably still the best way to solve your problem. But the code you have should work if you change <code>session</code> to <code>Session</code>. Remember in go the capitalization of the first letter denotes private vs. public. Anytime you see an <code>cannot refer to unexported name</code> check the capitalization.</p></pre>simplyh: <pre><p>I&#39;ve read through the readme, but unless I&#39;m wrong, the MaxOpen open refers to a particular session, which would still require me to pass around this session pointer, no?</p> <p>Anyway, your and <a href="/u/callumjones" rel="nofollow">/u/callumjones</a>&#39;s tip worked, thanks!</p></pre>doliner: <pre><p>If you set <code>MaxOpen</code> to <code>1</code> then I believe <code>Connect</code> will return the same session on successive calls. So there&#39;s no need for you to pass around the pointer. Just call <code>Connect</code> and use the <code>Session</code> it gives you.</p></pre>simplyh: <pre><p>Hmm that&#39;s useful. Since GoRethink pools connections, don&#39;t I need to start the pool somewhere? If I just call <code>Connect</code> inside my view functions then there&#39;s no guarantee that they&#39;re pooling, right? They might just be opening new connections since there&#39;s no earlier connection hanging around.</p></pre>doliner: <pre><p>I&#39;m pretty sure it starts the pool implicitly when you first call connect.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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