php小编西瓜在这篇文章中将为大家介绍Golang sqlx如何将多对多连接的右侧转换为数组。Golang是一种强大的编程语言,而sqlx是一个流行的Golang数据库工具。在使用sqlx进行多对多连接时,我们经常需要将右侧的结果转换为数组,以便更方便地进行处理和操作。本文将详细介绍如何使用sqlx和一些实例来演示这一过程,希望能对大家有所帮助。
问题内容
我有两张桌子 具有 unit 外键的 unit 和 unitimage。
我执行这个sql查询
select un.*, array_agg(row(ui.is_main, ui.image, ui.id)) as unit_images from unit un inner join unitimage ui on ui.unit_id = un.id group by un.id;
作为回应,我得到了所有单位字段并像这样加入了字段unit_images
{"(f,photos/units/unit_37/90_big.jpg,108)","(f,photos/units/unit_37/91_big.jpg,109)","(f,photos/units/unit_37/92_big.jpg,110)","(f,photos/units/unit_37/93_big.jpg,111)"}
go 语言结构体
type unit struct { id *int `json:"id" db:"id"` name *string `json:"name" db:"name"` ... a lot of fields unitimages []unitimages `json:"unit_images" db:"unit_images"` } type unitimages struct { id *int `json:"id" db:"id"` image *string `json:"image" db:"image"` ismain *bool `json:"is_main" db:"is_main"` }
sqlx代码
query := fmt.Sprintf("SELECT un.*, array_agg(ROW(ui.id, ui.image, ui.is_main)) as unit_images FROM %s un INNER JOIN %s ui ON ui.unit_id = un.id GROUP BY un.id", unitsTable, unitImagesTable) err := r.db.Select(&units, query)
我收到错误 "sql: 列索引 45 上的扫描错误,名称 "unit_images": 不支持扫描,将 driver.value 类型 []uint8 存储到类型 *[]*unitimages"
我是 golang 的新手,我想获得有关如何解决此问题的任何提示。也许我选择了错误的方式。
我想知道解决此问题的正确方法。
解决方法
看起来返回的 sql 结果是:
un.id | un.name | un.description | unit_images ------+---------+----------------+--------------------------------------------------------------------- 1 | unit a | description a | [(true, 'image1.jpg', 1), (false, 'image2.jpg', 2), (false, 'image3.jpg', 3)] 2 | unit b | description b | [(true, 'image4.jpg', 4), (true, 'image5.jpg', 5), (false, 'image6.jpg', 6)] 3 | unit c | description c | [(true, 'image7.jpg', 7), (false, 'image8.jpg', 8), (false, 'image9.jpg', 9)]
所以
`unitimages []unitimages `json:"unit_images" db:"unit_images"`
这是正确的想法,您有一个 unitimages 数组。但是:
id *int `json:"id" db:"id"` image *string `json:"image" db:"image"` ismain *bool `json:"is_main" db:"is_main"`
请注意,id
、image
、is_main
没有 sql 列,因此 go 无法映射它们。
轻松修复将 unitimages 更改为 []any,然后自己将内容投射到该数组中,如下所示:
for _, item := range thing.UnitImages { isMain := item[0].(bool) image := item[1].(string) id := item[2].(int64) //TODO do something with these vars } Or you could use `pg.StringArray` type.
以上是Golang sqlx 将多对多连接的右侧转换为数组的详细内容。更多信息请关注PHP中文网其他相关文章!

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go语言的错误处理通过errors.Is和errors.As函数变得更加灵活和可读。1.errors.Is用于检查错误是否与指定错误相同,适用于错误链的处理。2.errors.As不仅能检查错误类型,还能将错误转换为具体类型,方便提取错误信息。使用这些函数可以简化错误处理逻辑,但需注意错误链的正确传递和避免过度依赖以防代码复杂化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)

go'sfutureisbrightwithtrendslikeMprikeMprikeTooling,仿制药,云 - 纳蒂维德象,performanceEnhancements,andwebassemblyIntegration,butchallengeSinclainSinClainSinClainSiNgeNingsImpliCityInsImplicityAndimimprovingingRornhandRornrorlling。

goroutinesarefunctionsormethodsthatruncurranceingo,启用效率和灯威量。1)shememanagedbodo'sruntimemultimusingmultiplexing,允许千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

在Go中使用recover()函数可以从panic中恢复。具体方法是:1)在defer函数中使用recover()捕获panic,避免程序崩溃;2)记录详细的错误信息以便调试;3)根据具体情况决定是否恢复程序执行;4)谨慎使用,以免影响性能。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver CS6
视觉化网页开发工具

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版