英文:
Storing slice of bytes in Cassandra using gocql driver
问题
如何使用gocql驱动程序将字节切片存储为Cassandra的blob类型?
英文:
How to store slice of bytes as cassandra blob type using gocql driver?
答案1
得分: 0
你的问题非常不具体,你没有展示你已经做了什么。
由于我不知道你实际的问题所在,我将只是发布我之前在使用gocql包测试cassandra时编写的WriteRecord
函数:
func (cs *cassandra) WriteRecord(table string, fields []string, values ...interface{}) error {
var placeholder []string
for range fields {
placeholder = append(placeholder, "?")
}
querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, ", "), strings.Join(placeholder, ", "))
return cassandraSession.Query(querystring, values...).Exec()
}
如你所见,values
的类型是 interface{}
。因此,它们可以是任何类型,包括 []byte
类型。请注意,必须满足以下条件:len(fields) = len(values)
。
希望这对你成为 golang
专家的旅程有所帮助
英文:
Your question is very unspecific and you do not show us what you have done so far.
Since I don't know where your actual problem lies I will just post my WriteRecord
function I wrote a while back when testing cassandra with the gocql package:
func (cs *cassandra) WriteRecord(table string, fields []string, values ...interface{}) error {
var placeholder []string
for range fields {
placeholder = append(placeholder, "?")
}
querystring := fmt.Sprintf(insertstring, table, strings.Join(fields, ", "), strings.Join(placeholder, ", "))
return cassandraSession.Query(querystring, values...).Exec()
}
As you can see the values are of type interface{}
. So they can be anything including of type []byte
. Note that the following condition must be met: len(fields) = len(values)
I hope this helps you on your journey of becoming a golang
expert
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论