Initialize a nested structure in the Golang

I cannot figure out how to initialize a nested structure. 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", }, } } 
+82
go
Jul 17 '14 at 16:48
source share
8 answers

Well, any specific reason for not making Proxy its own structure?

In any case, you have 2 options:

The correct way is to simply move the proxy into your own structure, for example:

 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) } 

Less correct and ugly way, but still works:

 c := &Configuration{ Val: "test", Proxy: struct { Address string Port string }{ Address: "addr", Port: "80", }, } 
+119
Jul 17 '14 at 16:56
source share

If you do not want to use a separate structure definition for a nested structure, and you do not like the 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

+57
Sep 07 '16 at 9:12
source share

Define the Proxy structure separately, outside of Configuration , for example:

 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

+13
Jul 17 '14 at 16:55
source share

You also have this option:

 type Configuration struct { Val string Proxy } type Proxy struct { Address string Port string } func main() { c := &Configuration{"test", Proxy{"addr", "port"}} fmt.Println(c) } 
+9
Jan 29 '15 at 14:52
source share

One of them occurs when you want to create an instance of an open type defined in an external package, and this type inserts other private types.

Example:

 package animals type otherProps{ Name string Width int } type Duck{ Weight int otherProps } 

How do you create a Duck instance 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" } 
+5
Jan 23 '16 at 2:04 on
source share

You can define a structure and create its object in another structure, as I did 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 this helped you :)

+1
May 16 '18 at 7:07
source share

You need to redefine the nameless structure 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

+1
Jun 06 '18 at 7:09
source share

You can also highlight new and initialize all fields manually.

 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" } 

Look at the playground: https://play.golang.org/p/sFH_-HawO_M

0
Jan 18 '19 at 10:14
source share



All Articles