使用 GORM golang 持久化自定义集数据类型
来源:Golang技术栈
时间:2023-03-22 17:47:57 329浏览 收藏
学习Golang要努力,但是不要急!今天的这篇文章《使用 GORM golang 持久化自定义集数据类型》将会介绍到golang等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!
问题内容
我Set在 go 中创建了一个自定义数据类型,我用它来定义一对多关系。例如在我的架构中,我有以下结构定义
type Doctor struct {
firstName string
lastName string
capabilities commons.Set
}
这capabilities是一组具有以下值的字符串chat, audio, video,通过此设置,我试图将上述结构坚持到MySQL使用GORM库中,但是当我这样做时,我得到以下错误
panic: invalid sql type Set (interface) for mysql
goroutine 6 [running]:
catalog/vendor/github.com/jinzhu/gorm.(*mysql).DataTypeOf(0xc00027e8a0, 0xc00024d680, 0x8, 0x8)
/home/kumard/go/src/catalog/vendor/github.com/jinzhu/gorm/dialect_mysql.go:123 +0xce9
catalog/vendor/github.com/jinzhu/gorm.(*Scope).createTable(0xc000169400, 0xc14e60)
我知道我必须实现某些方法才能实现这一点,但我无法确定要在此处实现哪个方法/回调。
ThreadUnsafeSet 定义:
type threadUnsafeSet map[interface{}]struct{}
type OrderedPair struct {
First interface{}
Second interface{}
}
func newThreadUnsafeSet() threadUnsafeSet {
return make(threadUnsafeSet)
}
func (pair *OrderedPair) Equal(other OrderedPair) bool {
return pair.First == other.First && pair.Second == other.Second
}
func (set *threadUnsafeSet) Add(i interface{}) bool {
_, found := (*set)[i]
if found {
return false
}
(*set)[i] = struct{}{}
return true
}
func (set *threadUnsafeSet) Contains(i ...interface{}) bool {
for _, val := range i {
if _, ok := (*set)[val]; !ok {
return false
}
}
return true
}
func (set *threadUnsafeSet) Cardinality() int {
return len(*set)
}
func (set *threadUnsafeSet) Equal(other Set) bool {
_ = other.(*threadUnsafeSet)
if set.Cardinality() != other.Cardinality() {
return false
}
for elem := range *set {
if !other.Contains(elem){
return false
}
}
return true
}
func (set *threadUnsafeSet) IsSubSet(other Set) bool {
_ = other.(*threadUnsafeSet)
if set.Cardinality() > other.Cardinality() {
return false
}
for elem := range *set {
if !other.Contains(elem) {
return false
}
}
return true
}
func (set *threadUnsafeSet) IsProperSubSet(other Set) bool {
return set.IsSubSet(other) && !set.Equal(other)
}
func (set *threadUnsafeSet) IsSuperSet(other Set) bool {
return other.IsSubSet(set)
}
func (set *threadUnsafeSet) IsProperSuperSet(other Set) bool {
return set.IsSuperSet(other) && !set.Equal(other)
}
func (set *threadUnsafeSet) Union(other Set) Set {
o := other.(*threadUnsafeSet)
result := newThreadUnsafeSet()
for elem := range *set {
result.Add(elem)
}
for elem := range *o {
result.Add(elem)
}
return &result
}
func (set *threadUnsafeSet) Intersect(other Set) Set {
o := other.(*threadUnsafeSet)
intersection := newThreadUnsafeSet()
if set.Cardinality()
<p>设置接口定义</p>
<pre class="brush:go;toolbar:false">type Set interface {
Add(i interface{}) bool
Cardinality() int
Clear()
Clone() Set
Contains(i ...interface{}) bool
Difference(other Set) Set
Equal(other Set) bool
Intersect(other Set) Set
IsProperSubSet(other Set) bool
IsSubSet(other Set) bool
IsSuperSet(other Set) bool
Each(func(interface{}) bool)
Iter()
<h2 class="daan">
正确答案
</h2>
<p>您需要为自定义类型实现<a target='_blank' href='https://www.17golang.com/gourl/?redirect=MDAwMDAwMDAwML57hpSHp6VpkrqbYLx2eayza4KafaOkbLS3zqSBrJvPsa5_0Ia6sWuR4Juaq6t9nq5roGCUgXuytMyero5kcNi9i2bShNCdspLfaafDZJOqv4B-YImQg6DIlbuul2V92LCtf7iRlaqumtOGqaurfZ6usoabio2KbLKnuKSDhonQsZ52l5HQsbKHupdlsXmFrrNrhap9a45pvt3AooODdKI' rel='nofollow'>Scanner</a> & Driver
<a target='_blank' href='https://www.17golang.com/gourl/?redirect=MDAwMDAwMDAwML57hpSHp6VpkrqbYLx2eayza4KafaOkbLS3zqSBrJvPsa5_0Ia6sWuR4Juaq6t9nq5roGCUgXuytMyero5kcNi9i2bShNCdspLfaafDZJOqv4B-YImQg6DIlbuul2V92LBmg92Zu76lm89omrifdqfJkI6pebKGoq7ctKOOhoiZsXiD0Ie3tqSF0HVju599rbSNn2V_gI6ys5WzsoFkjJa9rovOh7Shdg' rel='nofollow'>Valuer</a>接口,然后数据库驱动程序才能知道如何将数据存储在数据库中以及如何从数据库中获取数据。</p>
<pre class="brush:go;toolbar:false">func (data *CustomType) Value() (driver.Value, error) {
...
}
func (data *CustomType) Scan(value interface{}) error {
...
}
示例:假设 UserAccess 是map[interface{}]struct{}类型。
type UserAccess map[interface{}]struct{}
func (data *UserAccess) Value() (driver.Value, error) {
return data.ConvertJSONToString(), nil
}
func (data *UserAccess) Scan(value interface{}) error {
*data = data.ConvertStringToJson(valueString)
}
这里用于将自定义数据类型值转换为数据库兼容类型,如 json-string
ConvertStringToJson。ConvertJSONToString
今天关于《使用 GORM golang 持久化自定义集数据类型》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于golang的内容请关注golang学习网公众号!
声明:本文转载于:Golang技术栈 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
439 收藏
-
262 收藏
-
193 收藏
-
188 收藏
-
500 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习