golang – 如何将字节切换转换为bool?

golang – 如何将字节切换转换为bool?,第1张

概述我有一个数据库sql.NullBool.为了将json解组到它中,我正在编写这个小函数.我可以通过简单地转换它来将字节数组转换为字符串(字符串(数据))……对于bool来说不是这样.知道如何转换为bool吗? type NullBool struct { sql.NullBool}func (b *NullBool) UnmarshalJSON(data []byte) error 我有一个数据库sql.NullBool.为了将Json解组到它中,我正在编写这个小函数.我可以通过简单地转换它来将字节数组转换为字符串(字符串(数据))……对于bool来说不是这样.知道如何转换为bool吗?

type NullBool struct {    sql.NullBool}func (b *NullBool) UnmarshalJsON(data []byte) error {  b.Bool = bool(data) //BREAKS!!   b.ValID = true  return nil}
解决方法 您几乎可以直接使用Json模块.

func (nb *NullBool) UnmarshalJsON(data []byte) error {    err := Json.Unmarshal(data,&nb.Bool)    nb.ValID = (err == nil)    return err}
总结

以上是内存溢出为你收集整理的golang – 如何将字节切换转换为bool?全部内容,希望文章能够帮你解决golang – 如何将字节切换转换为bool?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1264992.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存