In Go, you can extend an existing struct by embedding it into a new struct. This can be useful when you want to add additional fields or methods to the original struct.
Here is an example of how to extend a struct in Go:
main.go388 chars24 lines
In this example, we have a Person
struct with Name
and Age
fields. We then define a new struct Employee
which embeds the Person
struct and adds two new fields (Salary
and Department
).
You can create an instance of the Employee
struct just like any other struct, and you can access the fields of both the Person
and Employee
structs.
gistlibby LogSnag