If you don't know what extra fields are there ahead of time this won't work. In some places I use it as is -- as an array of St1. There are different ways of doing it and each having some limitations. Option 1: Have multiple structs, give them a common interface, and have an array of that interface. By using our Services or clicking I agree, you agree to our use of cookies. The different ways are: By using embedded struct – The parent struct is embedded in child struct. The different ways are: In inheritance using a struct, a base struct is embedded in child struct and base properties and methods can directly be called on child struct. Let's look at options which approximate that. Do visit our Inheritance in Go Complete Guide post for full reference. Structs aren't JavaScript objects or Python dicts. Option 2: Optional fields in a struct. a struct itself? Go doesn't do that directly. Ask questions and post articles about the Go programming language and related tools, events etc. I have a struct St1. Go is a statically typed language so you'll have to "cast" your results each place you need the field. You cannot pass the child struct to a function that expects base. This is generally frowned upon because of a lack of type info - you need reflection to figure out what's in each entry - but if you're coming from a dynamic language you're probably used to working without type safety anyway. Option 3: This is the closest to a Javascript object: instead of a struct use a map[string]interface{}. It doesn't really map to how you solve problems in Go in a typical way. Take a step back and maybe describe what problem you think this is the solution to, and maybe people can suggest a different approach. A struct (short for "structure") is a collection of data fields with declared data types. If you don't know what extra fields are there ahead of time this won't work. Cookies help us deliver our Services. The easiest is to add a map[string] interface{} to your struct. The limitation is … OP: This sounds like an XY problem. In other place I need to be able to dynamically attach extra data to each struct, akin to adding a few additional fields *dynamically* to each St1. is it possible to extend a type ? You can't avoid casting at the location where you'll use the field's value. This package is an example of something you should not do in Go. Option 2: Optional fields in a struct. Go supports inheritance by embedding struct or using interface. any downsides to that? What would be the key in a map? To fix this we can do inheritance using Interface, Variables in Go (Golang) – Complete Guide, OOP: Inheritance in GOLANG complete guide, Using Context Package in GO (Golang) – Complete Guide, Understanding time and date in Go (Golang) – Complete Guide, By using interfaces – Subtyping is possible but the limitation is that one has no way to refer to common properties. New comments cannot be posted and votes cannot be cast. Instead of asking about Y (forcing dynamic stuff into a static language) if you describe X (what you really need to do that you think requires dynamic stuff) you'll likely get a straight forward idiomatic Go way of accomplishing X that will work much better, be much cleaner, be easier to read, understand, debug, etc. Press J to jump to the feed. Structs aren't JavaScript objects or Python dicts. I remembered I saw something last year and here it is: https://github.com/Ompluscator/dynamic-struct. If those fields are the same type you could create a structure having those additional "fields" and put a pointer in struct St1 to such a struct. Option 1: Have multiple structs, give them a common interface, and have an array of that interface. But there is a workaround. Don't know yet if I like this or not, but hey, look at it and see if this is something usable for you. Go doesn't do that directly. Let's look at options which approximate that. Refer this link for more details –, By using interface + struct – This fixes the limitations of above two approach but one limitation is that overriding methods is not possible. For example in the above code if you uncomment //check(child) it will give compilation error: “cannot use child (type *child) as type base in argument to check”. Refer to this link for more details –. Each data field in a struct is declared with a known type, which could be a built-in type or another user-defined type. If you're thinking from a dynamic language then there's no solution that's going to be particularly comfortable for you. Just because you can do a thing doesn't mean you should do a thing. There are different ways of doing it and each having some limitations. You can add a func GetField("fieldName") interface{} getter to your struct type. This post desicribes inheritance using struct only. ... the library contains the structs and the data being passed by the user matches these structs, however I may need to add a few additional fields that the library doesnt need by my rest wrapper will use. Press question mark to learn the rest of the keyboard shortcuts. Go supports inheritance by embedding struct or using interface. Golang has the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types. See below code: Subtyping is not supported. That's what you want to do based on your experience with other languages.
Best Supermarket Turkey,
Flying Dove Images,
Patanjali Products Wholesale Price List,
Herring Eggs Wiki,
Costco Butterball Ground Turkey 4 Pack,
Wild Yellow Plum Chutney,
Color Depositing Shampoo,
Can You Bake A Pie In An Air Fryer,