hi there serdar yagerlop here for infoworld in this episode of smartgo we're going to
examine how inheritance and composition work with structures and types in go go's type system is a
little simpler than what you'd find in languages like java or c plus so some of the behaviors
of types that you might be used to from those languages don't exist in go go uses composition
rather than inheritance in its type system new types are made by combining existing ones
but by having the behaviors for each new type defined separately instead of just reused as is
across one type to the next let's start with a simple example i'm going to create two structs
person and actor person contains one field name actor has person as one of its elements and so
we'll have the field name but it also has another field imdb which will contain the imdb reference
id for that actor next i'm going to create a method data which is attached to person
all it does is return a formatted string of the name field from person in the main function
for this code i'm going to create a person harrison ford and i'm going to invoke data
on that struct next i'm going to create an actor instance using the person we just created as part
of it along with the imdb number for harrison ford and we'll invoke data on that object too
now when we run this code we're going to get the same output for both times that we invoke data
the first time we're invoking data directly on an instance of person
the second time we're invoking it on an instance of actor but actor has person as
part of it so the data method is only getting past the person part of it not all of actor go
is smart enough to recognize that if we invoke data on something that has person as a component
we want to use the person component for that not the whole struct
so this is the general mechanism for building up types from other types you use them as
components for larger constructions and you create methods that operate on each component separately
let me refine this further and show you more of how it can work let's add another method
also named data but which operates on actor instead of person when we do this any calls
to data from an instance of actor get passed to this new method instead so as you can see
one of the other things that we're doing in the data method for actor
is invoking the data method for person we get persons data method output and we reuse it in
actor's data method output this way we have an inheritance of behavior between the types
except that it's explicit and manually controlled instead of automatic so when we run this code
you're going to see the data calls for person and for actor are no different here is another
wrinkle on the same idea let's say we want to create a standalone function not a method just a
standalone function that can take either person or actor as input and return its data output
now to do this we'd first create an interface that has the data method in it and then we'd create a
function that takes in anything satisfying that interface and returns the data method output for
whatever is passed in our existing methods and structure definitions don't have to change at all
but when we call to get data as we do here and pass in a reference to our structures we will
get back the output from the data methods for each type that we pass in and if we run this
code you can see that happening the idea then is that when we create new types and go from old ones
we think about the behaviors and the interfaces for those structures separately from the
structures themselves and we also don't rely on a types components to give it functionality we use
methods specific to a given type to do that or we construct interfaces that provide behaviors
for groups of types that satisfy certain actions so that's it for this video if you liked it please
leave a comment below don't forget to subscribe to the tech talk channel on youtube and for more
smart go and more dev with serdar be sure to follow me on facebook youtube and infoworld.com