Ich kann nicht herausfinden, wie man eine verschachtelte Struktur initialisiert. Ein Beispiel finden Sie hier: 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",
},
}
}
Nun, gibt es einen bestimmten Grund, Proxy nicht zu seiner eigenen Struktur zu machen?
Trotzdem haben Sie 2 Möglichkeiten:
Auf die richtige Art und Weise verschieben Sie einfach Proxy in seine eigene Struktur, zum Beispiel:
type Configuration struct {
Val string
Proxy
}
type Proxy struct {
Address string
Port string
}
func main() {
c := &Configuration{
Val: "test",
Proxy: Proxy{
Address: "addr",
Port: "port",
},
}
fmt.Println(c)
}
Der weniger ordentliche und hässliche Weg funktioniert aber trotzdem:
c := &Configuration{
Val: "test",
Proxy: struct {
Address string
Port string
}{
Address: "addr",
Port: "80",
},
}
Wenn Sie keine separate Strukturdefinition für verschachtelte Struktur verwenden möchten und die von @OneOfOne vorgeschlagene zweite Methode nicht mögen, können Sie diese dritte Methode verwenden:
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`
}
Sie können es hier überprüfen: https://play.golang.org/p/WoSYCxzCF2
Definieren Sie Ihre Proxy
-Struktur separat außerhalb von Configuration
wie folgt:
type Proxy struct {
Address string
Port string
}
type Configuration struct {
Val string
P Proxy
}
c := &Configuration{
Val: "test",
P: Proxy{
Address: "addr",
Port: "80",
},
}
Sie haben diese Option auch:
type Configuration struct {
Val string
Proxy
}
type Proxy struct {
Address string
Port string
}
func main() {
c := &Configuration{"test", Proxy{"addr", "port"}}
fmt.Println(c)
}
Ein Problem tritt auf, wenn Sie einen in einem externen Paket definierten öffentlichen Typ instanziieren möchten und dieser Typ andere private Typen einbettet.
Beispiel:
package animals
type otherProps{
Name string
Width int
}
type Duck{
Weight int
otherProps
}
Wie instanziieren Sie eine Duck
in Ihrem eigenen Programm? Hier ist das Beste, was mir einfällt:
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"
}
Sie können eine Struktur definieren und ihr Objekt in einer anderen Struktur erstellen, wie ich es gemacht habe:
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)
}
Hoffe es hat dir geholfen :)
Sie müssen die unbenannte Struktur während &Configuration{}
neu definieren.
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)
}
Sie können auch new
zuweisen und alle Felder von Hand initialisieren
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"
}
Siehe auf dem Spielplatz: https://play.golang.org/p/sFH_-HawO_M