如何初始化嵌套结构体?

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

How to initialize a nested struct?

问题

我无法弄清楚如何初始化嵌套结构。在这里找到一个示例:
http://play.golang.org/p/NL6VXdHrjh

  1. package main
  2. type Configuration struct {
  3. Val string
  4. Proxy struct {
  5. Address string
  6. Port string
  7. }
  8. }
  9. func main() {
  10. c := &Configuration{
  11. Val: "test",
  12. Proxy: struct {
  13. Address string
  14. Port string
  15. }{
  16. Address: "addr",
  17. Port: "80",
  18. },
  19. }
  20. }
英文:

I cannot figure out how to initialize a nested struct. Find an example here:
http://play.golang.org/p/NL6VXdHrjh

  1. package main
  2. type Configuration struct {
  3. Val string
  4. Proxy struct {
  5. Address string
  6. Port string
  7. }
  8. }
  9. func main() {
  10. c := &Configuration{
  11. Val: "test",
  12. Proxy: {
  13. Address: "addr",
  14. Port: "80",
  15. }
  16. }
  17. }

答案1

得分: 256

好的,以下是翻译好的内容:

好的,有没有特定的原因不将Proxy作为自己的结构体?

无论如何,你有两个选项:

第一种正确的方式是将Proxy移动到自己的结构体中,例如:

  1. type Configuration struct {
  2. Val string
  3. Proxy Proxy
  4. }
  5. type Proxy struct {
  6. Address string
  7. Port string
  8. }
  9. func main() {
  10. c := &Configuration{
  11. Val: "test",
  12. Proxy: Proxy{
  13. Address: "addr",
  14. Port: "port",
  15. },
  16. }
  17. fmt.Println(c)
  18. fmt.Println(c.Proxy.Address)
  19. }

第二种不太正确和不太美观的方式,但仍然有效:

  1. c := &Configuration{
  2. Val: "test",
  3. Proxy: struct {
  4. Address string
  5. Port string
  6. }{
  7. Address: "addr",
  8. Port: "80",
  9. },
  10. }
英文:

Well, any specific reason to not make Proxy its own struct?

Anyway you have 2 options:

The proper way, simply move proxy to its own struct, for example:

  1. type Configuration struct {
  2. Val string
  3. Proxy Proxy
  4. }
  5. type Proxy struct {
  6. Address string
  7. Port string
  8. }
  9. func main() {
  10. c := &Configuration{
  11. Val: "test",
  12. Proxy: Proxy{
  13. Address: "addr",
  14. Port: "port",
  15. },
  16. }
  17. fmt.Println(c)
  18. fmt.Println(c.Proxy.Address)
  19. }

The less proper and ugly way but still works:

  1. c := &Configuration{
  2. Val: "test",
  3. Proxy: struct {
  4. Address string
  5. Port string
  6. }{
  7. Address: "addr",
  8. Port: "80",
  9. },
  10. }

答案2

得分: 172

如果您不想为嵌套结构使用单独的结构定义,并且不喜欢@OneOfOne建议的第二种方法,您可以使用第三种方法:

  1. package main
  2. import "fmt"
  3. type Configuration struct {
  4. Val string
  5. Proxy struct {
  6. Address string
  7. Port string
  8. }
  9. }
  10. func main() {
  11. c := &Configuration{
  12. Val: "test",
  13. }
  14. c.Proxy.Address = `127.0.0.1`
  15. c.Proxy.Port = `8080`
  16. }

您可以在此处查看:https://play.golang.org/p/WoSYCxzCF2

英文:

If you don't want to go with separate struct definition for nested struct and you don't like second method suggested by @OneOfOne you can use this third method:

  1. package main
  2. import "fmt"
  3. type Configuration struct {
  4. Val string
  5. Proxy struct {
  6. Address string
  7. Port string
  8. }
  9. }
  10. func main() {
  11. c := &Configuration{
  12. Val: "test",
  13. }
  14. c.Proxy.Address = `127.0.0.1`
  15. c.Proxy.Port = `8080`
  16. }

You can check it here: https://play.golang.org/p/WoSYCxzCF2

答案3

得分: 16

请注意,我将为您翻译以下内容:

将您的Proxy结构体单独定义在Configuration之外,如下所示:

  1. type Proxy struct {
  2. Address string
  3. Port string
  4. }
  5. type Configuration struct {
  6. Val string
  7. P Proxy
  8. }
  9. c := &Configuration{
  10. Val: "test",
  11. P: Proxy{
  12. Address: "addr",
  13. Port: "80",
  14. },
  15. }

请参考 http://play.golang.org/p/7PELCVsQIc

英文:

Define your Proxy struct separately, outside of Configuration, like this:

  1. type Proxy struct {
  2. Address string
  3. Port string
  4. }
  5. type Configuration struct {
  6. Val string
  7. P Proxy
  8. }
  9. c := &Configuration{
  10. Val: "test",
  11. P: Proxy{
  12. Address: "addr",
  13. Port: "80",
  14. },
  15. }

See http://play.golang.org/p/7PELCVsQIc

答案4

得分: 12

你还有这个选项:

  1. type Configuration struct {
  2. Val string
  3. Proxy
  4. }
  5. type Proxy struct {
  6. Address string
  7. Port string
  8. }
  9. func main() {
  10. c := &Configuration{"test", Proxy{"addr", "port"}}
  11. fmt.Println(c)
  12. }
英文:

You have this option also:

  1. type Configuration struct {
  2. Val string
  3. Proxy
  4. }
  5. type Proxy struct {
  6. Address string
  7. Port string
  8. }
  9. func main() {
  10. c := &Configuration{"test", Proxy{"addr", "port"}}
  11. fmt.Println(c)
  12. }

答案5

得分: 11

你也可以使用new来分配内存,并手动初始化所有字段。

  1. package main
  2. type Configuration struct {
  3. Val string
  4. Proxy struct {
  5. Address string
  6. Port string
  7. }
  8. }
  9. func main() {
  10. c := new(Configuration)
  11. c.Val = "test"
  12. c.Proxy.Address = "addr"
  13. c.Proxy.Port = "80"
  14. }

在playground中查看:https://play.golang.org/p/sFH_-HawO_M

英文:

You also could allocate using new and initialize all fields by hand

  1. package main
  2. type Configuration struct {
  3. Val string
  4. Proxy struct {
  5. Address string
  6. Port string
  7. }
  8. }
  9. func main() {
  10. c := new(Configuration)
  11. c.Val = "test"
  12. c.Proxy.Address = "addr"
  13. c.Proxy.Port = "80"
  14. }

See in playground: https://play.golang.org/p/sFH_-HawO_M

答案6

得分: 9

一个陷阱出现在你想要实例化一个外部包中定义的公共类型,并且该类型嵌入了其他私有类型的情况下。

示例:

  1. package animals
  2. type otherProps{
  3. Name string
  4. Width int
  5. }
  6. type Duck{
  7. Weight int
  8. otherProps
  9. }

如何在你自己的程序中实例化一个 Duck?这是我能想到的最好方法:

  1. package main
  2. import "github.com/someone/animals"
  3. func main(){
  4. var duck animals.Duck
  5. // 不能使用 something.Duck{Weight: 2, Name: "Henry"} 实例化一个 duck,因为 `Name` 是私有类型 `otherProps` 的一部分
  6. duck.Weight = 2
  7. duck.Width = 30
  8. duck.Name = "Henry"
  9. }
英文:

One gotcha arises when you want to instantiate a public type defined in an external package and that type embeds other types that are private.

Example:

  1. package animals
  2. type otherProps{
  3. Name string
  4. Width int
  5. }
  6. type Duck{
  7. Weight int
  8. otherProps
  9. }

How do you instantiate a Duck in your own program? Here's the best I could come up with:

  1. package main
  2. import "github.com/someone/animals"
  3. func main(){
  4. var duck animals.Duck
  5. // Can't instantiate a duck with something.Duck{Weight: 2, Name: "Henry"} because `Name` is part of the private type `otherProps`
  6. duck.Weight = 2
  7. duck.Width = 30
  8. duck.Name = "Henry"
  9. }

答案7

得分: 7

你需要重新定义&Configuration{}中的未命名结构体。

  1. package main
  2. import "fmt"
  3. type Configuration struct {
  4. Val string
  5. Proxy struct {
  6. Address string
  7. Port string
  8. }
  9. }
  10. func main() {
  11. c := &Configuration{
  12. Val: "test",
  13. Proxy: struct {
  14. Address string
  15. Port string
  16. }{
  17. Address: "127.0.0.1",
  18. Port: "8080",
  19. },
  20. }
  21. fmt.Println(c)
  22. }

链接:https://play.golang.org/p/Fv5QYylFGAY

英文:

You need to redefine the unnamed struct during &Configuration{}

  1. package main
  2. import "fmt"
  3. type Configuration struct {
  4. Val string
  5. Proxy struct {
  6. Address string
  7. Port string
  8. }
  9. }
  10. func main() {
  11. c := &Configuration{
  12. Val: "test",
  13. Proxy: struct {
  14. Address string
  15. Port string
  16. }{
  17. Address: "127.0.0.1",
  18. Port: "8080",
  19. },
  20. }
  21. fmt.Println(c)
  22. }

https://play.golang.org/p/Fv5QYylFGAY

答案8

得分: 2

你可以在另一个结构体中定义一个结构体,并创建其对象,就像我下面所做的那样:

  1. package main
  2. import "fmt"
  3. type Address struct {
  4. streetNumber int
  5. streetName string
  6. zipCode int
  7. }
  8. type Person struct {
  9. name string
  10. age int
  11. address Address
  12. }
  13. func main() {
  14. var p Person
  15. p.name = "Vipin"
  16. p.age = 30
  17. p.address = Address{
  18. streetName: "Krishna Pura",
  19. streetNumber: 14,
  20. zipCode: 475110,
  21. }
  22. fmt.Println("Name: ", p.name)
  23. fmt.Println("Age: ", p.age)
  24. fmt.Println("StreetName: ", p.address.streetName)
  25. fmt.Println("StreeNumber: ", p.address.streetNumber)
  26. }

希望对你有帮助 如何初始化嵌套结构体?

英文:

You can define a struct and create its object in another struct like i have done below:

  1. package main
  2. import "fmt"
  3. type Address struct {
  4. streetNumber int
  5. streetName string
  6. zipCode int
  7. }
  8. type Person struct {
  9. name string
  10. age int
  11. address Address
  12. }
  13. func main() {
  14. var p Person
  15. p.name = "Vipin"
  16. p.age = 30
  17. p.address = Address{
  18. streetName: "Krishna Pura",
  19. streetNumber: 14,
  20. zipCode: 475110,
  21. }
  22. fmt.Println("Name: ", p.name)
  23. fmt.Println("Age: ", p.age)
  24. fmt.Println("StreetName: ", p.address.streetName)
  25. fmt.Println("StreeNumber: ", p.address.streetNumber)
  26. }

Hope it helped you 如何初始化嵌套结构体?

答案9

得分: 1

包 main

类型 Proxy 结构体 {
地址 string
端口 string
}

类型 Configuration 结构体 {
Proxy
值 string
}

主函数 main() {
c := &Configuration{
值: "test",
Proxy: Proxy {
地址: "addr",
端口: "80",
},
}
}

英文:
  1. package main
  2. type Proxy struct {
  3. Address string
  4. Port string
  5. }
  6. type Configuration struct {
  7. Proxy
  8. Val string
  9. }
  10. func main() {
  11. c := &Configuration{
  12. Val: "test",
  13. Proxy: Proxy {
  14. Address: "addr",
  15. Port: "80",
  16. },
  17. }
  18. }

答案10

得分: 0

当你的配置是全局的时候,你可以这样做:

  1. package main
  2. var Configuration struct {
  3. Val string
  4. Proxy struct {
  5. Address string
  6. Port string
  7. }
  8. }
  9. func main() {
  10. Configuration.Val = "test"
  11. Configuration.Proxy.Address = "addr"
  12. Configuration.Proxy.Port = "80"
  13. }

这段代码定义了一个名为Configuration的结构体变量,其中包含了ValProxy两个字段。Proxy字段又包含了AddressPort两个字段。通过给这些字段赋值,你可以设置全局配置的值。在main函数中,示例代码给ValProxy.AddressProxy.Port分别赋了相应的值。

英文:

When your configuration is something global, you can do it this way:

  1. package main
  2. var Configuration struct {
  3. Val string
  4. Proxy struct {
  5. Address string
  6. Port string
  7. }
  8. }
  9. func main() {
  10. Configuration.Val = "test"
  11. Configuration.Proxy.Address = "addr"
  12. Configuration.Proxy.Port = "80"
  13. }

huangapple
  • 本文由 发表于 2014年7月18日 00:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/24809235.html
匿名

发表评论

匿名网友

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

确定