<p>For context, I'm a Python and JavaScript programmer making the jump to Go (and I love what I've seen so far). Let me know if this is the wrong forum to ask for help, thanks!</p>
<p>I'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 "github.com/dancannon/gorethink"
"github.com/golang/glog"
)
var session *r.session = nil
func init() {
session, err := r.Connect(r.ConnectOpts{
Address: "localhost:28015",
// Database: "coffeehouse",
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't exposed publicly (that's why you see the unexported error)</p></pre>simplyh: <pre><p>This worked perfectly thanks! I knew about the lowercase rule but I'm confused about the existence of both r.session and r.Session. </p></pre>elithrar_: <pre><p>Also note that you're shadowing your global—fix this by assigning to your global rather than init-and-assign:</p>
<pre><code> var err error
// Note the '=' and not the ':='
session, err = r.Connect(r.ConnectOpts{
Address: "localhost:28015",
// Database: "coffeehouse",
DiscoverHosts: true,
})
</code></pre></pre>simplyh: <pre><p>Caught this while compiling :) Go'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'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've read through the readme, but unless I'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>'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'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's useful. Since GoRethink pools connections, don't I need to start the pool somewhere? If I just call <code>Connect</code> inside my view functions then there's no guarantee that they're pooling, right? They might just be opening new connections since there's no earlier connection hanging around.</p></pre>doliner: <pre><p>I'm pretty sure it starts the pool implicitly when you first call connect.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传