登录
首页 >  Golang >  Go问答

如何将结构体作为参数传递给函数

来源:stackoverflow

时间:2024-04-23 22:03:33 432浏览 收藏

从现在开始,我们要努力学习啦!今天我给大家带来《如何将结构体作为参数传递给函数》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!

问题内容

我怎样才能做这样的事情?

我正在尝试将 struct 作为参数传递给 go 中的函数。

func handleEntityProperties(w http.ResponseWriter, r *http.Request) {
    const sliceSize = 100
    var entityProperties struct {
        Instance string `json:"instance"`
        Entities []struct {
            Id         string            `json:"id"`
            Properties map[string]string `json:"properties"`
            Type       string            `json:"type"`
        } `json:"entities"`
    }

    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        panic(err)
    }

    if !json.Valid([]byte(body)) {
        fmt.Fprintf(w, "invalid json")
        return
    }

    err = json.Unmarshal(body, &entityProperties)

    sendData(entityProperties.Entities[0:100])

    return
}

func sendData(entities struct) {
    log.Println("Doing things with entities ", entities)
}

正如您在代码中看到的,我正在尝试将 entityproperties.entities struct 的前 100 个元素发送到 senddata。我知道这在语法上是错误的。


解决方案


只需在函数之外声明您的类型:

type entity struct {
    id         string            `json:"id"`
    properties map[string]string `json:"properties"`
    type       string            `json:"type"`
}

并在 handleentityproperties()senddata() 的签名中重用它:

func handleentityproperties(w http.responsewriter, r *http.request) {
    const slicesize = 100
    var entityproperties struct {
        instance string   `json:"instance"`
        entities []entity `json:"entities"`
    }

    body, err := ioutil.readall(r.body)
    if err != nil {
        panic(err)
    }

    if !json.valid([]byte(body)) {
        fmt.fprintf(w, "invalid json")
        return
    }

    err = json.unmarshal(body, &entityproperties)

    senddata(entityproperties.entities[0:slicesize])

    return
}

func senddata(entities []entity) {
    log.println("doing things with entities ", entities)
}

另请注意,不能保证客户端将发送至少 100 个实体,因此您应该检查切片表达式是否可能导致运行时恐慌:

max := 100
if len(entityproperties.entities) < max {
    max = len(entityproperties.entities)
}
senddata(entityproperties.entities[:max])

此外,检查无效 json 是不必要的:如果 json 无效,json.Unmarshal() 将报告(非 nil)错误,您就会知道。

更进一步,您甚至不必将完整的正文读入内存(读入字节片),您可以使用 json.Decoder 直接从中读取(无需中间内存缓冲区),如下所示:

dec := json.newdecoder(r.body)
if err := dec.decode(&entityproperties); err != nil {
    // handle error
}

最后的 return 语句也是不必要的。

因此改进版本可能如下所示:

func handleEntityProperties(w http.ResponseWriter, r *http.Request) {
    var entityProperties struct {
        Instance string   `json:"instance"`
        Entities []entity `json:"entities"`
    }

    dec := json.NewDecoder(r.Body)
    if err := dec.Decode(&entityProperties); err != nil {
        // handle error
        http.Error(w, "invalid json", http.StatusBadRequest)
        return
    }

    max := 100
    if len(entityProperties.Entities) < max {
        max = len(entityProperties.Entities)
    }
    sendData(entityProperties.Entities[:max])
}

本篇关于《如何将结构体作为参数传递给函数》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>