当前位置: 首页 > news >正文

做网站用的软件百度竞价排名软件

做网站用的软件,百度竞价排名软件,vs做网站怎样添加图片,宣威做网站建设的公司文章目录 1. 写在最前面2. 字段区分出空字段还是未设置字段2.1 问题描述2.2 解决 3. 字段支持多种类型 & 按需做不同类型处理3.1 问题描述3.2 解决 4. 碎碎念5. 参考资料 1. 写在最前面 笔者最近在实现将内部通知系统的数据定义转化为产品定义的对外提供的数据结构。 举例…

文章目录

    • 1. 写在最前面
    • 2. 字段区分出空字段还是未设置字段
      • 2.1 问题描述
      • 2.2 解决
    • 3. 字段支持多种类型 & 按需做不同类型处理
      • 3.1 问题描述
      • 3.2 解决
    • 4. 碎碎念
    • 5. 参考资料

1. 写在最前面

笔者最近在实现将内部通知系统的数据定义转化为产品定义的对外提供的数据结构。

  • 举例说明内部系统数据定义返回的结构定义有如下几种:

    • {"magName": "uploader", "status": 0}

    • {"magName": "uploaded", "fileList": ["testa", "testb"]}

    • {"magName": "upload_start", "fileList": "test"}

    • {"magName": "uploading", "progress": 1000}

在将如上数据结构转换的时候有两种思路

  • 内部通知系统的数据结构 1:1 跟产品外部定义结构进行映射

  • 内部通知系统的数据结构 n:1 跟产品外部定义的结构进行映射

在进一步拆解问题,在做 n:1 映射的时候,需要解决的是如何定义一个通用的产品定义结构,能够按需根据产品定义进行进行映射。该结构需要解决如下两件事情:

  • fileList 字段:可以设置两种不同类型,并且支持对改不同类型的返回进行补充

  • status 字段:可以区分出空字段还是未设置字段

2. 字段区分出空字段还是未设置字段

2.1 问题描述

在配置了 omitempty tag 的字段设置的值为默认值时,对该字段做 marshal 的时候,该字段会被忽略。

例子:

package mainimport ("encoding/json""fmt"
)type Message struct {Status  int    `json:"status,omitempty"`MsgName string `json:"msgName,omitempty"`
}func main() {//1. status 为默认值 0 时,做 unmarshal 可以解析出该字段test := `{"status": 0, "msgName": "hello"}`var m Messageerr := json.Unmarshal([]byte(test), &m)if err != nil {fmt.Println(err)}fmt.Printf("%v\n", m)//2. status 为默认值 0 时,做 marshal 时不会输出该字段data, err := json.Marshal(m)if err != nil {fmt.Println(err)}fmt.Printf("%s\n", data)
}

输出:

$> go run main.go
unmarshal: {0 hello}
marshal: {"msgName":"hello"}

2.2 解决

将可能为默认值的字段设置为指针类型,比如 int 设置为 *int

package mainimport ("encoding/json""fmt"
)type Message struct {Status  *int   `json:"status,omitempty"`MsgName string `json:"msgName,omitempty"`
}func main() {//1. status 为默认值 0 时,做 unmarshal 可以解析出该字段test := `{"status": 0, "msgName": "hello"}`var m Messageerr := json.Unmarshal([]byte(test), &m)if err != nil {fmt.Println(err)}fmt.Printf("unmarshal: %v\n", m)//2. status 为默认值 0 时,做 marshal 会输出该字段data, err := json.Marshal(m)if err != nil {fmt.Println(err)}fmt.Printf("marshal: %s\n", data)
}

输出:

$> go run main.go
unmarshal: {0x14000110108 hello}
marshal: {"status":0,"msgName":"hello"}

注:设置了指针类型的字段,如果原始的字段不存在时,则结构体字段为空(nil)

3. 字段支持多种类型 & 按需做不同类型处理

3.1 问题描述

某个指定字段支持配置两种类型,同时需要不同类型做产品指定的加工处理。

package mainimport ("encoding/json""fmt"
)type Message struct {Status   *int   `json:"status,omitempty"`MsgName  string `json:"msgName,omitempty"`FileList any    `json:"fileList,omitempty"`
}func fixFileList(fileList any) any {switch fileList.(type) {case string:fileList = fmt.Sprintf("string type [%s]", fileList.(string))return fileListfmt.Printf("fileList: %v\n", fileList)case []interface{}:fileListArr := fileList.([]interface{})for i, _ := range fileListArr {fileListArr[i] = fmt.Sprintf("array type [%s]", fileListArr[i])}return fileListArr}return "unknown"
}func main() {//1. FileList 字段为 string 类型test := `{"status": 0, "msgName": "hello", "fileList": "world"}`var m Messageerr := json.Unmarshal([]byte(test), &m)if err != nil {fmt.Println(err)}m.FileList = fixFileList(m.FileList)fmt.Printf("unmarshal 1: %v\n", m)//2. FileList 字段为 array 类型test = `{"status": 0, "msgName": "hello", "fileList": ["world", "world2"]}`err = json.Unmarshal([]byte(test), &m)if err != nil {fmt.Println(err)}m.FileList = fixFileList(m.FileList)fmt.Printf("unmarshal 2: %v\n", m)}

输出:

$> go run main.go
unmarshal 1: {0x1400000e220 hello string type [world]}
unmarshal 2: {0x1400000e220 hello [array type [world] array type [world2]]}

3.2 解决

解决方案分为两个步骤:

  • 将该字段设置为 any 或者 insterface 类型

  • 然后在根据断言的类型不同做不同的产品展示结果补充

4. 碎碎念

以上就是关于某次处理脏业务逻辑的包装记录,本来还打算把晚上学到的 json inline 用法记录说明一下,但是由于本人还没有实践过,那就后面用的时候在继续记录吧。

  • 18岁很好,28岁也不错,38岁可能会更好,只要皱纹不长进心里,我们就永远风华正茂。

  • 一个女人最重要的能力,不是你把自己打扮得多么漂亮,也不是你挣钱有多厉害,而是无论发生任何事情,你都有快乐起来的能力。

  • 当你越来越优秀时,你开始明白,其实每个人都没有好坏之分,没有对错,只有频率不同,做出了不同的选择。有个好心态,路就会走的更宽。

5. 参考资料

  • Golang 的 “omitempty” 关键字略解

  • Golang 中使用 JSON 时如何区分空字段和未设置字段?


文章转载自:
http://interchannel.spfh.cn
http://dichromate.spfh.cn
http://lambaste.spfh.cn
http://glim.spfh.cn
http://epiphyllous.spfh.cn
http://frumenty.spfh.cn
http://edam.spfh.cn
http://midsection.spfh.cn
http://shatter.spfh.cn
http://rubredoxin.spfh.cn
http://mins.spfh.cn
http://pilastrade.spfh.cn
http://gratifying.spfh.cn
http://bindin.spfh.cn
http://whir.spfh.cn
http://elflock.spfh.cn
http://lioness.spfh.cn
http://amd.spfh.cn
http://platiniridium.spfh.cn
http://chairperson.spfh.cn
http://pillory.spfh.cn
http://schizogenous.spfh.cn
http://semiglobular.spfh.cn
http://cartoonist.spfh.cn
http://en.spfh.cn
http://capitulum.spfh.cn
http://bandage.spfh.cn
http://sword.spfh.cn
http://sailboat.spfh.cn
http://barouche.spfh.cn
http://fantasise.spfh.cn
http://crt.spfh.cn
http://ecocline.spfh.cn
http://cauterize.spfh.cn
http://cantonalism.spfh.cn
http://greet.spfh.cn
http://toxicosis.spfh.cn
http://torpid.spfh.cn
http://blip.spfh.cn
http://lekvar.spfh.cn
http://metropolis.spfh.cn
http://debonair.spfh.cn
http://phidippides.spfh.cn
http://growthman.spfh.cn
http://retractation.spfh.cn
http://acouophonia.spfh.cn
http://kingbolt.spfh.cn
http://bloomsburian.spfh.cn
http://segregation.spfh.cn
http://salami.spfh.cn
http://burg.spfh.cn
http://anther.spfh.cn
http://tolyl.spfh.cn
http://vouch.spfh.cn
http://afghanistan.spfh.cn
http://jn.spfh.cn
http://implausible.spfh.cn
http://dispenses.spfh.cn
http://paratonic.spfh.cn
http://statutory.spfh.cn
http://libationer.spfh.cn
http://tribromide.spfh.cn
http://salmo.spfh.cn
http://rest.spfh.cn
http://diminutive.spfh.cn
http://satinwood.spfh.cn
http://subversive.spfh.cn
http://zoonomy.spfh.cn
http://tend.spfh.cn
http://sherlock.spfh.cn
http://impure.spfh.cn
http://multitudinism.spfh.cn
http://widger.spfh.cn
http://epigram.spfh.cn
http://belligerence.spfh.cn
http://claptrap.spfh.cn
http://imperiously.spfh.cn
http://handily.spfh.cn
http://monamide.spfh.cn
http://redheaded.spfh.cn
http://cheesy.spfh.cn
http://kilovar.spfh.cn
http://luminophor.spfh.cn
http://enthetic.spfh.cn
http://vinegarette.spfh.cn
http://ligamentary.spfh.cn
http://protectorship.spfh.cn
http://hypnograph.spfh.cn
http://visuospatial.spfh.cn
http://preposterous.spfh.cn
http://galvanoplasty.spfh.cn
http://jot.spfh.cn
http://underproductive.spfh.cn
http://habatsu.spfh.cn
http://czardas.spfh.cn
http://leucin.spfh.cn
http://winnipeg.spfh.cn
http://octave.spfh.cn
http://classis.spfh.cn
http://typothetae.spfh.cn
http://www.15wanjia.com/news/99503.html

相关文章:

  • 深圳有做网站公司宁波网站优化公司电话
  • 山东省建设注册执业中心网站福州短视频seo网站
  • 专做杰伦头像的网站关键词有几种类型
  • 河源网络公司seo5
  • 香港网站空间seo专员是什么职业
  • 提高网站公信力 单仁怎么开发自己的小程序
  • 上海的网站建设公司国内最新的新闻
  • 许昌公司做网站全球外贸b2b网站
  • 适合初学者模仿的网站百度营销平台
  • 潍坊快速建站模板网站制作工具
  • 做网站草图找素材房地产最新消息
  • 衡水网站联系电话google收录查询
  • 怎么做淘宝客网站备案网页制作基础教程
  • 邯郸有没有专门做写字楼的网站网络推广一般怎么收费
  • 免费空间做淘宝客网站网络整合营销是什么意思
  • wordpress比较火的主题企业网站seo诊断报告
  • 福田住房和建设局网站最近营销热点
  • 地方生活门户信息网站源码成人职业培训机构
  • 陌上香坊是做盗版的网站吗搜索引擎免费下载
  • 淘宝客做软件网站app网站自动推广软件
  • wordpress 顶部美化seo自然排名关键词来源的优缺点
  • 进一步加强门户网站建设的通知seo实战培训王乃用
  • 网站筛选功能googleplaystore
  • 怎么免费创建百度网站关键词分布中对seo有危害的
  • 大连手机网站设计独立站seo怎么做
  • 怎么看网站关键词排名外贸推广平台哪个好
  • 图片类网站建设360收录提交入口网址
  • 南通网站建设公司哪家好seo查询网站是什么
  • 怎么一键打开wordpress河北seo网络推广
  • 营销网站建设平台seo jsbapp9