Home  >  Article  >  Backend Development  >  How to remove certain items from structure received by imported package in golang?

How to remove certain items from structure received by imported package in golang?

WBOY
WBOYforward
2024-02-06 08:40:06976browse

如何从 golang 中导入的包接收的结构中删除某些项目?

Question content

I received a project from the package of an imported third-party module:

myitem := importpackage.get()

It is a structure like this:

type importedstruct struct {
    ip                  net.ip                  `json:"ip"`
    index               uint32                  `json:"index"`
    localindex          uint32                  `json:"localindex"`
    remoteindex         []*udp.addr             `json:"remoteindex"`
    certificates        *certificates           `json:"certificates"`
    vpnaddress          []iputil.vpnip          `json:"vpnaddress"`
}

I want to delete one or more of them and return it as json from my golang gin api:

c.json(200, &myitem)

The problem is trying to find the most efficient resource utilization way to do this.

I thought about making a loop and writing the fields I need into a new structure:

newitem := make([]importedstruct, len(myitem))

i := 0
for _, v := range myitem {
    newitem[i] = ...
    ...
}

c.json(200, &hostlist)

I also considered marshalling and then unmarshalling to assign it to another struct before returning it via the api:

// Marshal the host map to json
marshaledJson, err := json.Marshal(newItem)
if err != nil {
    log.Error(err)
    c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
    return
}

// Unmarshal the json into structs
var unmarshalledJson []ImportedStruct
err = json.Unmarshal(marshaledJson, &unmarshalledJson)
if err != nil {
    log.Error(err)
    c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
    return
}

// Return the modified host map
c.JSON(200, &unmarshalledJson)

I'm hoping to find a more efficient way to do this. myitem could contain over 3 million lines of json and loop through everything, or marshalling and unmarshaling seems to involve more processes than just implementing something relatively simple.

EDIT: The structure is a slice ([]).


Correct answer


Define a new structure that is a copy of your existing structure with different labels:

type importedstructmarshal struct {
    ip                  net.ip                  `json:"ip"`
    index               uint32                  `json:"index"`
    localindex          uint32                  `json:"-"`
    remoteindex         []*udp.addr             `json:"remoteindex"`
    certificates        *certificates           `json:"certificates"`
    vpnaddress          []iputil.vpnip          `json:"vpnaddress"`
}

Then, use this new structure to marshal:

var input ImportedStruct
forMarshal:=ImportedStructMarshal(input)
...

This will work as long as the new structure is field-by-field compatible with the imported structure.

The above is the detailed content of How to remove certain items from structure received by imported package in golang?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete