英文:
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
}
}
t
是 Config.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 "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< --
}
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论