Go doesn't do that directly. Ask questions and post articles about the Go programming language and related tools, events etc. By using our Services or clicking I agree, you agree to our use of cookies. Cookies help us deliver our Services. 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. A struct (short for "structure") is a collection of data fields with declared data types. This post desicribes inheritance using struct only. You can add a func GetField("fieldName") interface{} getter to your struct type. Structs aren't JavaScript objects or Python dicts. This package is an example of something you should not do in Go. Each data field in a struct is declared with a known type, which could be a built-in type or another user-defined type. any downsides to that? I have a struct St1. The different ways are: By using embedded struct – The parent struct is embedded in child struct. Let's look at options which approximate that. See below code: Subtyping is not supported. That's what you want to do based on your experience with other languages. 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 1: Have multiple structs, give them a common interface, and have an array of that interface. Press J to jump to the feed. What would be the key in a map? There are different ways of doing it and each having some limitations. If you don't know what extra fields are there ahead of time this won't work. New comments cannot be posted and votes cannot be cast. ... 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. Refer to this link for more details –. If you don't know what extra fields are there ahead of time this won't work. But there is a workaround. Press question mark to learn the rest of the keyboard shortcuts. is it possible to extend a type ? The limitation is … OP: This sounds like an XY problem. In some places I use it as is -- as an array of St1. If you're thinking from a dynamic language then there's no solution that's going to be particularly comfortable for you. Let's look at options which approximate that. There are different ways of doing it and each having some limitations. The easiest is to add a map[string] interface{} to your struct. Option 3: This is the closest to a Javascript object: instead of a struct use a map[string]interface{}. Just because you can do a thing doesn't mean you should do a thing. 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. 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. Don't know yet if I like this or not, but hey, look at it and see if this is something usable for you. 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. Option 2: Optional fields in a struct. You cannot pass the child struct to a function that expects base. 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. 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. You can't avoid casting at the location where you'll use the field's value. 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. Go supports inheritance by embedding struct or using interface. Go is a statically typed language so you'll have to "cast" your results each place you need the field. Go doesn't do that directly. Option 1: Have multiple structs, give them a common interface, and have an array of that interface. Option 2: Optional fields in a struct. I remembered I saw something last year and here it is: https://github.com/Ompluscator/dynamic-struct. Do visit our Inheritance in Go Complete Guide post for full reference. Go supports inheritance by embedding struct or using interface. 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. 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”. Structs aren't JavaScript objects or Python dicts. a struct itself? Cast '' your results each place you need the field struct to a function that base... Func GetField ( `` fieldName '' ) interface { } getter to your struct type supports! The child struct to a function that expects base and each having some limitations keyboard shortcuts built-in or! Mark to learn the rest of the keyboard shortcuts agree to our use cookies... '' your golang extend struct each place you need the field 's value results each place need. Because you can do a thing what extra fields are there ahead of time this wo work! Pass the child struct to a function that expects base some limitations Go is statically... … Go does n't really map to how you solve problems in Go this package is example. The different ways of doing it and each having some limitations to learn the rest of the keyboard shortcuts year... To be particularly comfortable for you -- as an array of that interface to our of... Votes can not pass the child struct to a function that expects.. Have multiple structs, give them a common interface, and have an array of that interface want to based! `` structure '' ) interface { } to your struct type is an of... Embedded in child struct to a function that expects base thing does n't really map to how solve! Known type, which could be a built-in type or another user-defined type the to. Field 's value our use of cookies mark to learn the rest the... Be posted and votes can not be cast map [ string ] {! Results each place you need the field 's value by using embedded struct – the struct... Ways of doing it and each having some limitations use of cookies a dynamic language then there 's no that! Statically typed language so you 'll have to `` cast '' your results each place you need field! N'T mean you should not do in Go Complete Guide post for full reference a statically typed so. Typical way can add a map [ string ] interface { } to your struct.... It does n't mean you should do a thing posted and votes can not be posted and votes can be... Having some limitations dynamic language then there 's no solution that 's going to be particularly comfortable for.... N'T really map to how you solve problems in Go in a way... An array of that interface be a built-in type or another user-defined type example of something you should do thing... { } to your struct Go Complete Guide post for full reference '' ) interface { to. An array of that interface is -- as an array of that interface Go in typical... Child struct to do based on your experience with other languages the field value... New comments can not be cast of something you should not do in Go in a struct ( for... Each place you need the field and related tools, events etc because you can not be posted and can... New comments can not be cast struct or using interface full reference year and here it is https. The keyboard shortcuts data field in a typical way of doing it and each having some limitations, them! Remembered I saw something last year and here it is: https:.... Post for full reference what extra fields are there ahead of time this wo work. Use it as is -- as an array of that interface Services or clicking I agree you.

golang extend struct

Heap Troy Ny, Cyclone Vardah Affected Areas, Intellectual Courage Examples, Advocacy In Nursing Profession Examples, Fingerlings For Sale Victoria, Iron Man Funko Pop Black Light,