英文:
Accepting Cassandra's Set datatype in golang
问题
我在Cassandra中有一个列,类型为set。我想使用gocql在Golang代码中接收该列。在接收时,接收整个set的变量应该是什么数据类型?
例如:
err = session.Query("select product_list from category where category_id=?", key).Scan(&productIdList)
Product_list的类型是set。在Golang中,productIdList应该是什么数据类型?
英文:
I have a column in cassandra which is of type set.
Usig gocql I want to accept that column in golang code.So while accepting what should be the datatype of the variable in which the entire set is accepted.
e.g:
err = session.Query("select product_list from category where category_id=?",key).Scan(&productIdList)
Product_list is of type set.So what should be the data type of productIdList in golang?
答案1
得分: 2
看起来gocql默认使用切片或数组来表示Cassandra的set类型。
如果这不是你想要的,它允许你使用gocql.Marshaller和gocql.Unmarshaller接口来定义自己的类型。
例如:
type Set map[string]bool
func (s Set) UnmarshalCQL(info TypeInfo, data []byte) error {
/* 解析你的set数据到Go map的代码,从CQL结果中 */
}
func (s Set) MarshalCQL(info TypeInfo) ([]byte, error) {
/* 将你的set数据从Go map序列化为CQL可用的代码 */
}
这意味着你需要自己编写set输出解析的代码。
英文:
It looks like gocql uses a slice or array by default for Cassandra's set type.
If that's not what you want it does let you define one of your own using the gocql.Marshaller and gocql.Unmarshaller interfaces.
So for example:
type Set map[string]bool
func (s Set) UnmarshalCQL(info TypeInfo, data []byte) error {
/* code to unmarshal your set into a Go map. from cql results */
}
func (s Set) MarshalCQL(info TypeInfo) ([]byte, error) {
/* code to marshal your set out of a go map for cql to use */
}
This does however mean that you'll have to write the set output parsing yourself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论