hi there serdar gagalop here for infoworld idg in this episode of smartgo we're going
to look at the concept of the method a function that receives values only of a specific type
for creating something like object-oriented behaviors in go now compared to some other
languages go has a good deal fewer object-oriented constructions but it does have mechanisms that let
you deal with data objects in many of the same ways as those other languages
in python java and c for instance we all have the concept of a class a data type with a collection
of methods that can be applied to it in go the closest parallel to a class is a struct
it's much closer to the same concept in c plus the use of methods also in go methods aren't
directly attached to a given structure they're written side by side along with it so here's an
extended example that we can use to demonstrate how this works in this code i have here a struct
named movie with several fields one of them roles is a slice of another struct cast member
i've also defined a few methods here for both movie and cast member and the way you define
a method is to simply define a function but before the function name you supply a method
type in parentheses like this you can then supply any additional arguments to the method
as you would for a normal function the first method i've created for movie is add role
this takes a pointer to an existing movie object which here is supplied in the variable named m
and we then append to the roles field in m a passed in instance of cast member now note that
this method can accept a value as a copy but it can also accept a pointer to the original value
in this case we're taking a pointer because we need access to the original value to modify it
this other method here details can accept a copy since it doesn't change anything about the value
it just prints the contents to the console however it's generally a good idea to create a pointed
receiver to avoid copying by default especially when you have a struct that's very large
to invoke these methods we just use an instance of the type they're bound to
then the dot and the name of the method the saw to be a familiar pattern to most anyone
who's worked with similar constructions in other languages as you might expect
roll.details will run the details method and automatically supply it with the value from roll
m.adroll does the same thing with m as an instance of a movie struct and with role passed in as an
argument note that when we call these methods we can supply either a pointer or a value regardless
of what the method receives by default although we always want to supply what's appropriate
as i described earlier you don't want to pass in a copy of something very large now the details
method here for instance receives the value by default but we can pass in a pointer to it as
if we want to as we do here in this last call to details this is a way to avoid unneeded copying
and then in this call to add role we supply a pointer but we don't have to do that explicitly
in the call since the method signature states that we receive a pointer by default and go
will automatically provide a pointer to match now i should also point out something else
we have two methods here named details and we can do this because each one accepts a different
type for its method one for movie and one for cast member we've invoked both of these methods
in different places here and so in each case it's clear what kind of object they accept there's no
naming conflict because technically they're not the same name they're referring to different
things so to sum up methods in go provide us with a way to create object-oriented behaviors
for custom types they can work with values or pointers depending on the behavior you want
and they can share names as long as they refer to different types now in future videos i'll
explore some advanced ways to use methods such as using them with types that aren't structs
so that's it for this video if you liked it please leave a comment below don't forget
to subscribe to the idg tech talk channel on youtube and inform more smart grow and for more
dev with serdar be sure to join me on facebook youtube and infoworld.com