英文:
Returning gocql's session variable from function
问题
我有以下的gocql连接。
 //cassandra连接
  cluster := gocql.NewCluster("localhost")
  cluster.Keyspace = "demo"
  cluster.DefaultPort = 9042
  cluster.Consistency = gocql.Quorum
  session, _ = cluster.CreateSession()
  defer session.Close()
我想把这段代码放在一个Golang函数中,并返回session变量,但我不知道它的数据类型。我尝试返回*Session,但是会报错。有什么办法可以解决吗?
英文:
I have the following gocql connection.
 //cassandra connection
  cluster := gocql.NewCluster("localhost")
  cluster.Keyspace = "demo"
  cluster.DefaultPort = 9042
  cluster.Consistency = gocql.Quorum
  session, _ = cluster.CreateSession()
  defer session.Close()
I want to put this inside a golang function and return the session variable,but I don't know its datatype. I tried returning *Session but it gives me an error. Any idea how to do this.
答案1
得分: 2
根据文档(http://godoc.org/github.com/gocql/gocql#ClusterConfig.CreateSession),类型是*gocql.Session。但是请注意,如果你延迟调用session.Close(),它将在你返回时立即运行,这意味着你将永远不会返回一个有效的连接。你需要在调用函数中关闭会话。
英文:
Per the documentation (http://godoc.org/github.com/gocql/gocql#ClusterConfig.CreateSession) the type is *gocql.Session. I would note, however, that if you defer the session.Close() call, it will run as soon as you return, meaning you will never return a valid connection. You will have to close the session in the calling function.
答案2
得分: 0
cluster.CreateSession() 返回 *gocql.Session, error。
也许如果你能分享你的实际代码并告诉我们你遇到了什么错误,这个问题会更容易回答。
顺便说一下,有一个邮件列表可以用于更复杂的问题,还有Github上的问题跟踪器。
英文:
cluster.CreateSession() returns *gocql.Session, error
Maybe if you could share your actual code and tell us what error you are getting, this question would be easier to answer.
BTW there is a mailing list for more involved questions, as well as the issue tracker on Github.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论