douwenan9849 2017-11-17 11:26
浏览 40
已采纳

在Go中使用类似的指针结构来分配结构

I have two structures that are similar and I want to assign one to the other. The first one "Equipment" is the structure used to match the database. The second one "JsonEquipment" is the helper structure to parse JSON data.

Here is the example:

type Equipment struct {
    ID         uint
    CategoryID uint
    Ip         string
    Login      string
    Password   string
}

type JsonEquipment struct {
    ID           *uint
    Category     *string
    Ip           *string
    Login        *string
    Password     *string
}

The pointers are there to check if the field is present or not in the JSON. More info: How to recognize void value and unspecified field when unmarshaling in Go?

So for the moment I created a function with many "if" to check and assign to the Equipment but I wonder if there is a better & cleaner solution.

func CreateEquipment(item JsonEquipment) (Equipment) {
    e := Equipment{}

    if item.ID != nil && !previousEquipment.Auto { // Is the field present & not automatic equipment
        e.ID = *item.ID
    }

    if item.Ip != nil { // Is the field present ?
        e.Ip = *item.Ip
    }

    if item.Login != nil { // Is the field present ?
        e.Login = *item.Login
    }

    [...]
    return e
}

I hope you grasp the idea.

This question is similar to Assign struct with another struct but is different because of the pointers => non pointers struct

  • 写回答

2条回答 默认 最新

  • duan32342 2017-11-20 14:15
    关注

    EDIT

    What I did below works but doesn't do what I intended to. Actually my first code is the only solution to check the difference between empty field and unspecified field.

    Here is the demonstration of the differences: https://play.golang.org/p/3tU6paM9Do


    Fist thanks for everyone's help, and particularly to @Mihailo.

    What I end up doing is having my models struct without pointers, i.e:

    type Equipment struct {
        ID         uint
        CategoryID uint
        Ip         string
        Login      string
        Password   string
    }
    

    and I used a JSON helper struct that has a pointer of model struct, i.e

    type JsonHelper struct {
        Equipments    []*Equipment
        [Add other structures here]
    }
    

    and finally I parsed the JSON using the JsonHelper struct:

    func ParseJSON(inputPath string) JsonHelper {
        // Read configuration file
        content, err := ioutil.ReadFile(inputPath)
        if err != nil {
            log.Println("Error:", err)
        }
    
        var input JsonHelper
    
        // Parse JSON from the configuration file
        err = json.Unmarshal(content, &input)
        if err != nil {
            log.Print("Error: ", err)
        }
    
        return input
    }
    

    HTH

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器