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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错