如何初始化嵌套结构体?

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

How to initialize a nested struct?

问题

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

package main

type Configuration struct {
    Val   string
    Proxy struct {
        Address string
        Port    string
    }
}

func main() {

    c := &Configuration{
        Val: "test",
        Proxy: struct {
            Address string
            Port    string
        }{
            Address: "addr",
            Port:    "80",
        },
    }

}
英文:

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

package main

type Configuration struct {
	Val   string
	Proxy struct {
		Address string
		Port    string
	}
}

func main() {

	c := &Configuration{
		Val: "test",
		Proxy: {
			Address: "addr",
			Port:    "80",
		}
	}

}

答案1

得分: 256

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

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

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

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

type Configuration struct {
    Val   string
    Proxy Proxy
}

type Proxy struct {
    Address string
    Port    string
}

func main() {

    c := &Configuration{
        Val: "test",
        Proxy: Proxy{
            Address: "addr",
            Port:    "port",
        },
    }
    fmt.Println(c)
    fmt.Println(c.Proxy.Address)
}

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

c := &Configuration{
    Val: "test",
    Proxy: struct {
        Address string
        Port    string
    }{
        Address: "addr",
        Port:    "80",
    },
}
英文:

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:

type Configuration struct {
	Val string
	Proxy Proxy
}

type Proxy struct {
	Address string
	Port    string
}

func main() {

	c := &Configuration{
		Val: "test",
		Proxy: Proxy{
			Address: "addr",
			Port:    "port",
		},
	}
	fmt.Println(c)
    fmt.Println(c.Proxy.Address)
}

The less proper and ugly way but still works:

c := &Configuration{
	Val: "test",
	Proxy: struct {
		Address string
		Port    string
	}{
		Address: "addr",
		Port:    "80",
	},
}

答案2

得分: 172

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

package main
import "fmt"
type Configuration struct {
    Val   string
    Proxy struct {
        Address string
        Port    string
    }
}

func main() {
    c := &Configuration{
        Val: "test",
    }

    c.Proxy.Address = `127.0.0.1`
    c.Proxy.Port = `8080`
}

您可以在此处查看: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:

package main
import "fmt"
type Configuration struct {
    Val   string
	Proxy struct {
    	Address string
	    Port    string
	}
}

func main() {
    c := &Configuration{
		Val: "test",
    }

	c.Proxy.Address = `127.0.0.1`
	c.Proxy.Port = `8080`
}

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

答案3

得分: 16

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

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

type Proxy struct {
    Address string
    Port    string
}

type Configuration struct {
    Val string
    P   Proxy
}

c := &Configuration{
    Val: "test",
    P: Proxy{
        Address: "addr",
        Port:    "80",
    },
}

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

英文:

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

type Proxy struct {
	Address string
	Port    string
}

type Configuration struct {
	Val string
	P   Proxy
}

c := &Configuration{
	Val: "test",
	P: Proxy{
		Address: "addr",
		Port:    "80",
	},
}

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

答案4

得分: 12

你还有这个选项:

type Configuration struct {
    Val string
    Proxy
}

type Proxy struct {
    Address string
    Port    string
}

func main() {
    c := &Configuration{"test", Proxy{"addr", "port"}}
    fmt.Println(c)
}
英文:

You have this option also:

type Configuration struct {
    	Val string
    	Proxy
}

type Proxy struct {
    	Address string
    	Port    string
}

func main() {
    	c := &Configuration{"test", Proxy{"addr", "port"}}
    	fmt.Println(c)
}

答案5

得分: 11

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

package main

type Configuration struct {
    Val   string
    Proxy struct {
        Address string
        Port    string
    }
}

func main() {
	c := new(Configuration)
	c.Val = "test"
	c.Proxy.Address = "addr"
	c.Proxy.Port = "80"
}

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

英文:

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

package main

type Configuration struct {
    Val   string
    Proxy struct {
        Address string
        Port    string
    }
}

func main() {
	c := new(Configuration)
	c.Val = "test"
	c.Proxy.Address = "addr"
	c.Proxy.Port = "80"
}

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

答案6

得分: 9

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

示例:

package animals

type otherProps{
  Name string
  Width int
}

type Duck{
  Weight int
  otherProps
}

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

package main

import "github.com/someone/animals"

func main(){
  var duck animals.Duck
  // 不能使用 something.Duck{Weight: 2, Name: "Henry"} 实例化一个 duck,因为 `Name` 是私有类型 `otherProps` 的一部分
  duck.Weight = 2
  duck.Width = 30
  duck.Name = "Henry"
}
英文:

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:

package animals

type otherProps{
  Name string
  Width int
}

type Duck{
  Weight int
  otherProps
}

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

package main

import "github.com/someone/animals"

func main(){
  var duck animals.Duck
  // Can't instantiate a duck with something.Duck{Weight: 2, Name: "Henry"} because `Name` is part of the private type `otherProps`
  duck.Weight = 2
  duck.Width = 30
  duck.Name = "Henry"
}

答案7

得分: 7

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

package main

import "fmt"

type Configuration struct {
    Val   string
    Proxy struct {
        Address string
        Port    string
    }
}

func main() {

    c := &Configuration{
        Val: "test",
        Proxy: struct {
            Address string
            Port    string
        }{
            Address: "127.0.0.1",
            Port:    "8080",
        },
    }
    fmt.Println(c)
}

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

英文:

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

package main

import "fmt"
    
type Configuration struct {
    Val   string
    Proxy struct {
        Address string
        Port    string
    }
}
    
func main() {
    
    c := &Configuration{
        Val: "test",
        Proxy: struct {
            Address string
            Port    string
        }{
            Address: "127.0.0.1",
            Port:    "8080",
        },
    }
    fmt.Println(c)
}

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

答案8

得分: 2

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

package main

import "fmt"

type Address struct {
    streetNumber int
    streetName   string
    zipCode      int
}

type Person struct {
    name    string
    age     int
    address Address
}

func main() {
    var p Person
    p.name = "Vipin"
    p.age = 30
    p.address = Address{
        streetName:   "Krishna Pura",
        streetNumber: 14,
        zipCode:      475110,
    }
    fmt.Println("Name: ", p.name)
    fmt.Println("Age: ", p.age)
    fmt.Println("StreetName: ", p.address.streetName)
    fmt.Println("StreeNumber: ", p.address.streetNumber)
}

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

英文:

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

package main

import "fmt"

type Address struct {
	streetNumber int
	streetName   string
	zipCode      int
}

type Person struct {
	name    string
	age     int
	address Address
}

func main() {
	var p Person
	p.name = "Vipin"
	p.age = 30
	p.address = Address{
		streetName:   "Krishna Pura",
		streetNumber: 14,
		zipCode:      475110,
	}
	fmt.Println("Name: ", p.name)
	fmt.Println("Age: ", p.age)
	fmt.Println("StreetName: ", p.address.streetName)
	fmt.Println("StreeNumber: ", p.address.streetNumber)
}

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

答案9

得分: 1

包 main

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

类型 Configuration 结构体 {
Proxy
值 string
}

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

英文:
package main

type	Proxy struct {
		Address string
		Port    string
	}

type Configuration struct {
	Proxy
	Val   string

}

func main() {

	c := &Configuration{
		Val: "test",
		Proxy: Proxy {
			Address: "addr",
			Port:    "80",
		},
	}

}

答案10

得分: 0

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

package main

var Configuration struct {
    Val   string
    Proxy struct {
        Address string
        Port    string
    }
}

func main() {
    Configuration.Val = "test"
    Configuration.Proxy.Address = "addr"
    Configuration.Proxy.Port = "80"
}

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

英文:

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

package main

var Configuration struct {
    Val   string
    Proxy struct {
        Address string
        Port    string
    }
}

func main() {
    Configuration.Val = "test"
    Configuration.Proxy.Address = "addr"
    Configuration.Proxy.Port = "80"
}

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:

确定