Embedded Structures in Golang Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In the Go programming language, Embedded Structures are a way to reuse a struct's fields without having to inherit from it. It allows you to include the fields and methods of an existing struct into a new struct. The new struct can add additional fields and methods to those it inherits. Syntax: type outer struct { inner struct { field1 string field2 int } } Example 1: Go package main import ( "fmt" ) type Address struct { City string State string } type Person struct { Name string Address //embedded struct } func main() { p := Person{ Name: "John Doe", Address: Address{ City: "New York", State: "NY", }, } fmt.Println("Name:", p.Name) fmt.Println("City:", p.City) fmt.Println("State:", p.State) } Output: Name: John Doe City: New York State: NY Explanation: This program defines two structs - Address and Person. The Person struct includes the Address struct. In the main function, a Person named "John Doe" is created with an address of "New York, NY". The program then prints out the person's name, city, and state. Because the Address struct is included in the Person struct, we can access the person's city and state directly from the Person struct using dot notation. The program simply creates a person object with an address and prints out some of their details. Example 2: Go package main import "fmt" type Author struct { Name string Branch string Year int } type HR struct { Author // Embedded Type } func main() { result := HR{ Author: Author{ Name: "Dhruv", Branch: "IT", Year: 2024, }, } fmt.Println("Details of Author") fmt.Println(result) } Output: Details of Author {{Dhruv IT 2024}} Explanation: This program creates two structs - Author and HR. The HR struct includes the Author struct. An instance of the HR struct is created with a value of the Author field set to "Dhruv", "IT", and 2024. The program then prints the message "Details of Author" followed by the result variable value. The output shows the Author fields inside the HR struct with curly braces. Comment More infoAdvertise with us Next Article Embedded Structures in Golang D dhruvrawat54 Follow Improve Article Tags : Go Language Similar Reads Embedding Interfaces in Golang In Go language, the interface is a collection of method signatures and it is also a type means you can create a variable of an interface type. As we know that the Go language does not support inheritance, but the Go interface fully supports embedding. In embedding, an interface can embed other inter 6 min read Function as a Field in Golang Structure In Go, you can define functions as fields within a structure (struct). This feature allows you to associate behaviors (methods) directly with data types, enabling a more organized and encapsulated way of managing data and its related operations.Examplepackage mainimport "fmt"// Define a struct with 2 min read Templates in GoLang Template in Golang is a robust feature to create dynamic content or show customized output to the user. Golang has two packages with templates: text/template html/template There are mainly 3 parts of a template which are as follows: 1. Actions They are data evaluations, control structures like loops 4 min read How to print struct variables data in Golang? A struct (Structure) is a user-defined type in Golang that contains a collection of named fields/properties which creates own data types by combining one or more types. Also, this concept is generally compared with the classes in object-oriented programming. A struct has different fields of the same 2 min read fmt Package in GoLang Prerequisite: Packages in GoLang and Import in GoLang Technically defining, a package is essentially a container of source code for some specific purpose. Packages are very essential as in all programs ranging from the most basic programs to high-level complex codes, these are used. A package ensur 13 min read Like