我可以将gorilla模式与sql.NullString一起使用吗?

编程入门 行业动态 更新时间:2024-10-27 08:25:15
本文介绍了我可以将gorilla模式与sql.NullString一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用大猩猩模式根据用户的表单提交来填充结构.我的结构包含 sql.NullString ,当前出现以下错误:

I am using gorilla schema to populate a struct based on a user's form submission. My struct contains sql.NullString and I am currently getting the following error:

模式:找不到用于sql.NullString的转换器

如何在要用大猩猩模式填充的结构中使用 sql.NullString ?

How can I use sql.NullString in a struct that I want to populate with gorilla schema?

推荐答案

我创建了要点( gist.github/carbocation/51b55297702c7d30d3ef ),展示了一种解决此问题的方法.您需要为以下四种类型分别创建一个 schema.Converter :sql.NullString,sql.NullBool,sql.NullInt64和sql.NullFloat64.

I created a gist ( gist.github/carbocation/51b55297702c7d30d3ef ) that shows one way to approach this. You need to create a schema.Converter for each of the four types: sql.NullString, sql.NullBool, sql.NullInt64, and sql.NullFloat64.

sql.NullString的示例:

An example for sql.NullString:

import "database/sql" import "reflect" func ConvertSQLNullString(value string) reflect.Value { v := sql.NullString{} if err := v.Scan(value); err != nil { return reflect.Value{} } return reflect.ValueOf(v) }

然后使用您的 * schema.Decoder (通常是一个全局包,在本例中为 d )注册:

Then register this with your *schema.Decoder (usually a package global, in this case named d):

import "database/sql" nullString := sql.NullString{} d.RegisterConverter(nullString, ConvertSQLNullString)

更多推荐

我可以将gorilla模式与sql.NullString一起使用吗?

本文发布于:2023-10-16 11:36:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   gorilla   NullString   sql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!