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

WBOY
Release: 2024-02-06 08:40:06
forward
957 people have browsed it

如何从 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"`
}
Copy after login

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)
Copy after login

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)
Copy after login

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"`
}
Copy after login

Then, use this new structure to marshal:

var input ImportedStruct
forMarshal:=ImportedStructMarshal(input)
...
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!