How to initialize a nested struct?

Go

Go Problem Overview


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

}

Go Solutions


Solution 1 - Go

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

Solution 2 - Go

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

Solution 3 - Go

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

Solution 4 - Go

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

Solution 5 - Go

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

Solution 6 - Go

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

Solution 7 - Go

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

Solution 8 - Go

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

Solution 9 - Go

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

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsontagsView Question on Stackoverflow
Solution 1 - GoOneOfOneView Answer on Stackoverflow
Solution 2 - GosepehrView Answer on Stackoverflow
Solution 3 - GoVitor De MarioView Answer on Stackoverflow
Solution 4 - GoJoseView Answer on Stackoverflow
Solution 5 - GodvdplmView Answer on Stackoverflow
Solution 6 - GoFerdyView Answer on Stackoverflow
Solution 7 - GolizhenpengView Answer on Stackoverflow
Solution 8 - GoVipin GuptaView Answer on Stackoverflow
Solution 9 - GojamleeView Answer on Stackoverflow