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",
    },
}
Answer from OneOfOne on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › go language › nested-structure-in-golang
Nested Structure in Golang - GeeksforGeeks
July 12, 2025 - In Go, a structure can have fields that are themselves structures, which are called nested structures.
Discussions

Nested Struct
The data field of your struct is a slice of structs, you're not telling it which element in the slice to access: type Server struct { Data []struct { Attributes struct {... This line: fmt.Printf("Server Name: %s\n", server.Data.Attributes.Name) Would need to be this: fmt.Printf("Server Name: %s\n", server.Data[0].Attributes.Name) More on reddit.com
🌐 r/golang
2
1
May 28, 2022
How to created nested struct
I Want to create nested struct. I don’t know the depth of my child object . Example : I want to create menus at runtime no limit for child. Example Root M1 M11 M111 M12 M121 M2 M21 M211 M2111 M212 M2121 M3 M4 How achieve this More on forum.golangbridge.org
🌐 forum.golangbridge.org
0
0
March 26, 2024
When my struct has another anonymous struct as its field, how do I initialize it?
You can do this: human := Human{ Name: "foo", Age: 16, Dog: struct { Breed string Age uint8 }{ Breed: "porker", Age: 5, }, } Though it’s a bit awkward so you may want to give a name to the anonymous struct. More on reddit.com
🌐 r/golang
23
11
January 23, 2023
How to print nicely a nested struct

Hey! So are you looking to do anything more than just view the data that's in those structs? For like debugging purposes. A few ways that come to mind to pretty print structs are:

  1. Printf fmt. Printf("%#v\n", theStruct)

  2. Marshal with indent and then print the bytes

https://golang.org/pkg/encoding/json/#MarshalIndent

3. Use a third party library like go spew https://github.com/davecgh/go-spew

If you want to do more with it than just pretty print, then implementing the stringer interface seems like a good way to go. You'll need to do it for each type as you said.

Sorry if I misunderstood your question!

More on reddit.com
🌐 r/golang
16
2
May 27, 2020
🌐
Medium
medium.com › @xcoulon › nested-structs-in-golang-2c750403a007
Nested structs in Golang. Today I heard about nested structs in… | by Xavier Coulon | Medium
November 20, 2017 - you can declare a single type by using nested anonymous structures: (note how the json tags apply on the Server and Postgres fields) This is very convenient when associated with the json.Unmarshal() function, for example: (you can try it out yourself in this Golang playground)
🌐
Reddit
reddit.com › r/golang › nested struct
r/golang on Reddit: Nested Struct
May 28, 2022 -

How do you access data from within a nested struct?

I used https://mholt.github.io/json-to-go/ to turn an JSON API response from https://api.battlemetrics.com/servers?filter[game]=rust into a struct.

Data.Attributes.Name doesn't seem to work to get the data from the struct in this case: https://go.dev/play/p/904eFMv6oKU

The code is based on https://www.youtube.com/watch?v=aYk8XAKxhxU

🌐
Go Forum
forum.golangbridge.org › getting help
How to created nested struct - Getting Help - Go Forum
March 26, 2024 - I Want to create nested struct. I don’t know the depth of my child object . Example : I want to create menus at runtime no limit for child. Example Root M1 M11 M111 M12 M121 M2 M21 M211 M2111 M212 M2121 M3 …
🌐
DEV Community
dev.to › mowshon › go-insert-a-value-into-nested-structures-with-a-dot-32mh
Go: Insert a value into nested structures with a dot - DEV Community
September 15, 2023 - This package was created to solve the problem of adding data in nested structures, maps, slices and channels - of ⚡️ ANY complexity and different data types. We know the exact path to the required field, but if there is a map on this path, we must first initialise the map correctly, check if such a key exists and then insert the value. But, yes… if you have a simple structure, you don't need the dot package, but if you have a more complicated hierarchy, I'm happy to present my latest open source Golang project!
Find elsewhere
🌐
Google Groups
groups.google.com › g › golang-nuts › c › hvraQJtcY9U
How do I initialise a doubly/deeply nested struct?
I have deeply nested structs as a result of JSON decoding but would like to also create them manually for testing purposes. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... There is unfortunately no really useful way to write a composite literal for that kind of type. Easier to initialize it field by field. Both versions shown here: http://play.golang.org/p/m2pMJWrIgR Ian
🌐
IncludeHelp
includehelp.com › golang › go-nested-structures.aspx
Go Nested Structures
In Go, nested structures refer to the concept of defining one struct type within another.
🌐
Medium
kevin-yang.medium.com › golang-embedded-structs-b9d20aadea84
Golang Embedded Structs
May 29, 2022 - Golang allows you to compose structs inside of other structs. There are two ways of composing structs: anonymous embedded fields and explicit fields. An anonymous embedded field is a struct field that doesn’t have an explicit field name.
🌐
Psj
psj.codes › structs-in-go-part-2
Structs in Go
March 7, 2023 - Nested Structs Go allows us to use a struct as a field of another struct, this pattern is called nesting. A nested struct can be defined using the following syntax. type struct1 struct{ // fields } type struct2 struct{ // fields s struct...
🌐
TutorialsPoint
tutorialspoint.com › nested-structure-in-golang
Nested Structure in Golang
April 26, 2023 - To define a structure, you must use type and struct statements. The struct statement defines a new data type, with multiple members for your program. The type statement binds a name with the type which is struct in our case.
🌐
Reddit
reddit.com › r/golang › how to print nicely a nested struct
r/golang on Reddit: How to print nicely a nested struct
May 27, 2020 -

Hello everyone! I'm trying to learn go, and to do so I've created a little quiz game to study psychopathology for an exam. My problem is that I don't like the code I've wrote, so I'm trying to do better, hoping to learn good go practices along the way.

My question is: if I have a nested struct, with maps and stuff like this...:

type Disorders struct {
    Disorders []Disorder
}

type Disorder struct {
    Name     string
    Symptoms Symptom
    // More stuff
}

type Symptom struct {
    Cognitive map[string]string
    Emotional map[string]string
    // More stuff
}

...what is the cleanest way to write a .String() method for everyone of them, that lets me specify the kind of symptom, the key of the map, etc.

I load the values from a JSON, so it looks like I can't use the stringer tool

Do you see a better solution to writing every .String() method manually?

Btw I wanted to thank this community, you're all very inclusive and supportive, and it's one of the subs I read from most gladly!

🌐
GitHub
github.com › pocketbase › pocketbase › discussions › 4664
Nested Golang Struct to database? · pocketbase/pocketbase · Discussion #4664
Hey there, I have a nested struct within my CustomModel like that type A struct { Extension *B `db:"-" json:"b"` Extension string `db:"b" json:"b"` } type B ...
Author   pocketbase
🌐
Medium
leapcell.medium.com › deep-dive-into-go-struct-103961431c64
Go Struct: A Deep Dive
January 2, 2025 - Composition, on the other hand, creates new structs by including other structs, facilitating code reuse. Nested structs enable one struct to include another struct as a field. This makes data structures more flexible and organized.
🌐
YouTube
youtube.com › watch
Learn how to use Nested Structs with Golang - PART 36 - YouTube
Learn how to use Nested Structs with #GolangGo is a modern and extremely useful programming language. It was developed at google to work as an efficient serv...
Published   August 31, 2021
🌐
Reddit
reddit.com › r/golang › how do you iterate though nested structs and print them?
r/golang on Reddit: How do you iterate though nested structs and print them?
December 15, 2022 -

Hi gophers, I'm new to golang so apologies in advance if this is a dumb question, but how do you iterate through nested structs and print the fields and values of each?

So far I've got something like :

`fields := reflect.TypeOf(p)

values := reflect.ValueOf(p)

num := fields.NumField()

for i := 0; i < num; i++ {

field := fields.Field(i)

value := values.Field(i)

fmt.Print(field.Name, "is", value, "\n")

}`

Which is effective for a single struct but ineffective for a struct that contains another struct. Here's the example code I'm trying to experiment with to learn interfaces, structs and stuff: https://go.dev/play/p/kfBc31b-VoI

Sorry for my poor English, but I would appreciate any advice, pointers or criticism. thanks!

🌐
Medium
medium.com › @gopal96685 › easy-ways-to-handle-nested-maps-structs-slices-in-go-5b8698600aea
Easy Ways to Handle Nested Maps, Structs & Slices in Go | by Gopal Agrawal | Medium
December 10, 2024 - This article will cover five common scenarios and show you the best practices for initializing nested data structures, avoiding nil pointer dereferences, and keeping your code clean and idiomatic.
🌐
Mohitkhare
mohitkhare.com › blog › marshal-structs-golang
Marshal structs the right way: Golang – Mohit Khare
June 29, 2025 - Say it has tens of 3 level nested structs? Would you manage pointer for each struct and making your code errorprone? No, no one likes do deal with loads of pointers. The problem lies within json package itself. It handles zero value checks for everything but not for struct. Not sure, why though? Let me know if you know 😅 · On some research and discussion, my teammate at work found out this issue - https://github.com/golang/go/issues/11939 which is exactly what we talk about here.