doujiao4710 2015-11-22 06:51
浏览 33
已采纳

将结构的命名字段传递给其他函数

This is my first golang program rather than just reading docs so please bear with me.

I've a structure like:- (comes from a parsed yaml)

type GLBConfig struct {
    GLBList []struct {
        Failover string `json:"failover" yaml:"failover"`
        GLB      string `json:"glb" yaml:"glb"`
        Pool     []struct {
            Fqdn              string `json:"fqdn" yaml:"fqdn"`
            PercentConsidered int    `json:"percent_considered" yaml:"percent_considered"`
        } `json:"pool" yaml:"pool"`
    } `json:"glb_list" yaml:"glb_list"`
}

Now, in my main function, there's a for loop which does processing of each GLB:-

for _, glb := range config_file.GLBList {
    processGLB(glb)
}

What will be the function definition of processGLB to receive this type?

I tried doing this but it doesn't work and I would like to know why.

func processGLB(glb struct{}) {
    fmt.Println(glb)
}

./glb_workers.go:42: cannot use glb (type struct { Failover string "json:\"failover\" yaml:\"failover\""; Glb string "json:\"glb\" yaml:\"glb\""; Pool []struct { Fqdn string "json:\"fqdn\" yaml:\"fqdn\""; PercentConsidered int "json:\"percent_considered\" yaml:\"percent_considered\"" } "json:\"pool\" yaml:\"pool\"" }) as type struct {} in argument to processGLB

Then, some googling later, this works.

func processGLB(glb interface{}) {
    fmt.Println(glb)
}

Is it a good idea to do this? Why do I have to use interfaces to pass a simple structure named field?

In the end, what's the most elegant/right way to do this in golang?

EDIT:

Simple solution was to define the structure separately.

type GLBList struct {
        Failover string `json:"failover" yaml:"failover"`
        GLB      string `json:"glb" yaml:"glb"`
        Pool     []struct {
                Fqdn              string `json:"fqdn" yaml:"fqdn"`
                PercentConsidered int    `json:"percent_considered" yaml:"percent_considered"`
        } `json:"pool" yaml:"pool"`
}

type GLBConfig struct {
        GLBs []GLBList `json:"glb_list" yaml:"glb_list"`
}

And then a function definition like:-

func processGLB(glb GLBList) {
}
  • 写回答

2条回答 默认 最新

  • dpgkg42484 2015-11-22 07:44
    关注

    You should really consider define the struct explicitly and reuse it. The all point of using a langauge with support for static typing is to define your types whenever you can, it helps the compiler to find bugs and produce a faster code.

    Also, if you would like to have a more compact code you can use the concept of anonymous fields, although I don't think that is the best scenario for this.

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

报告相同问题?

悬赏问题

  • ¥15 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动