如何在Go中传递嵌套的结构体

huangapple go评论76阅读模式
英文:

How to pass a nested struct in Go

问题

一个小的应用程序,它读取一个配置文件并将其存储在一个struct中。如何将配置struct的一部分传递给fetchTemperature函数?

配置

package configuration

type Config struct {
    Temperatures []struct {
        Temperature
    }
}

type Temperature struct {
    AppId string
}

func Load() Config {
    var c Config
    // -- 8< -- snip -- 8< --
    return c
}

主函数

package main

import "configuration"

var c configuration.Config = configuration.Load()

func main() {
    for _, t := range c.Temperatures {
        fetchTemperature(t)
    }
}

func fetchTemperature(t configuration.Temperature) {
    // -- 8< -- snip -- 8< --
}

给出的错误信息是:

cannot use t (type struct { configuration.Temperature }) as type configuration.Temperature in argument to fetchTemperature

t不是configuration.Temperature类型,那么如何传递这个struct呢?

英文:

A little 2 file application that reads a config file and stores it in a struct. How do pass part of the config struct to the fetchTemperature function?

Configuration

package configuration

type Config struct {
    Temperatures []struct {
        Temperature
    }
}

type Temperature struct {
    AppId string
}

func Load() Config {
    var c Config
    // -- 8< -- snip -- 8< --
    return c
}

Main

package main

import "configuration"

var c configuration.Config = configuration.Load()

func main() {
    for _, t := range c.Temperatures {
        fetchTemperature(t)
    }
}

func fetchTemperature(t configuration.Temperature) {
    // -- 8< -- snip -- 8< --
}

Gives me:

cannot use t (type struct { configuration.Temperature }) as type configuration.Temperature in argument to fetchTemperature

Isn't t of configuration.Temperature and if not, how do I pass the struct around?

答案1

得分: 3

type Config struct {
Temperatures []struct {
Temperature
}
}

tConfig.Temperatures[i]。对于来自匿名结构体 struct { Temperature }Temperature,可以使用 t.Temperature 来选择结构体中的字段。

例如,

package main

import "configuration"

var c configuration.Config = configuration.Load()

func main() {
for _, t := range c.Temperatures {
fetchTemperature(t.Temperature)
}
}

func fetchTemperature(t configuration.Temperature) {
// -- 8< -- snip -- 8< --
}

我怀疑你的困惑是因为你写了

type Config struct {
Temperatures []struct {
Temperature
}
}

Temperatures 是一个类型为匿名结构体 struct { configuration.Temperature } 的切片。

你可能想要的是

type Config struct {
Temperatures []Temperature
}

Temperatures 是一个类型为 configuration.Temperature 的切片。

英文:
type Config struct {
    Temperatures []struct {
        Temperature
    }
}

t is Config.Temperatures[i]. For Temperature from anonymous struct { Temperature }, write t.Temperature to select the field from the struct.

For example,

package main

import &quot;configuration&quot;

var c configuration.Config = configuration.Load()

func main() {
    for _, t := range c.Temperatures {
        fetchTemperature(t.Temperature)
    }
}

func fetchTemperature(t configuration.Temperature) {
    //  -- 8&lt; -- snip -- 8&lt; --
}

I suspect that your confusion arose because you wrote

type Config struct {
    Temperatures []struct {
        Temperature
    }
}

Temperatures is a slice of type anonymous struct { configuration.Temperature }.

What you probably wanted was

type Config struct {
    Temperatures []Temperature
}

Temperatures is a slice of type configuration.Temperature.

huangapple
  • 本文由 发表于 2016年2月7日 06:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/35247287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定