Go 47: Interfaces with Code Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello everyone! Welcome to Code and  Learn. In this GoLang lesson 47 we   will be covering the topic of interfaces.  So let's get started. So what exactly are   interfaces? Interfaces are types that  define the functionality of a type,   they specify what the type should do but not  how it should do it. I mentioned this before   but it's worth mentioning again abstract types  such as interfaces only specify functionality   whereas concrete types like struct with methods  specify both functionality and implementation. To write an interface we use the type and the  interface keyword and inside the interface we   declare the signature of methods for that  interface. The methods declared inside the   interface are known as method set of interface.  To implement an interface a type must implement   all the methods that are defined in it, as long  as the type implements the methods specified in   the interface it is considered to implement the  interface. In Go standard lib interfaces names   typically end with ..er such as io.writer this  is a convention widely used in Go community. Here are few examples of creating interfaces  here we say type shape interface and inside   it we define the method in the first interface  shape we have a method called area which returns   a float next we have a interface car which  has two methods the first is start and the   other one is stop returning a bool so that's  essentially how you create interfaces in Go. Now let's look at few examples of how interfaces  are created and implemented with code first let   us create our interface I'm going to create a  shape interface which has a area method type   shape interface and then inside here the  method that the interface has area which   is a method that returns the area in float64  so that's exactly how you write an interface   type the name of the interface interface keyword  and then all the methods that the interface has. Interfaces in Go are implicit unlike other  languages where you have to specify if your   type is implementing an interface in  the type declaration in Go you don't   have to do that for example in Java you  say class X implements interface Y. A   type is considered to be implementing the  interface as long as it has implemented   all the methods declared in the interface  the type can have additional methods that   are not declared in the interface and it will  still meet the requirements of the interface. Please note that a type can implement multiple  interfaces that has different set of methods as   long as all those methods are implemented by  the type. Now let us expand on our previous   example now let's create a rectangle structure  to implement this interface here I have created a   rectangle structure with fields width and height  to implement the interface shape it has to has   a method area so let's create a method area on  rectangle here we have created the area method   with the same signature as that of interface  this essentially means our rectangle structure   is implementing the shape interface I'm going  to create another type structure which also   implements the shape interface we have created  Circle type with radius which is a float64 to   implement the shape interface we need to create  a area method on the circle so let's create one   here we have created the area method which  returns float64 matching the signature of   area method inside the shape interface now  let us see how interfaces work first I'm   going to create an instance of rectangle and  circle. Rectangle has a width of five height   of three while Circle has a radius of four  now I'm going to create a shape slice which   has elements of type shape which is our interface  and then add both rectangle and circle since both   the rectangle and circle satisfy the interface  shape by implementing the area method we can use   both inside the shape slice let's now range  over shapes to print the area here what I'm   doing is I'm ranging through shapes and then  printing the area since shape is a interface   type which has a method on area I can expect to  call area on any shape inside the loop let us   run our program to verify the same go on main.go  and as you can see the area is indeed printed for   both circle and rectangle that is basically  how you implement an interface you can also   change this functionality to say func printArea  which accepts an interface shape and then let's   just copy this to print the area here we are  printing the area of the shape we can do this   since shape is a interface that has a method  area so instead of doing this we can also do   printArea shape and this will work as it work  before now if I create another type square and   say width float64 initialize a square and then  try to print the area from the square we cannot   do this because square is not implementing the  method inside the interface but if we implement   the method s square area which returns float64  you can see the error has now gone away if we   run a program now you will see it works  as expected, area of square was printed. Another thing I mentioned was it does not  matter if the struct or the type has other   methods along with the area as long as it  has the area method it will keep on meeting   the shape interface so I can say print  area which is a function that prints the   area of a square and then I can do print  area pass the square even though I have   created print area method on the square  it still meets the interface as long as   the area method is implemented if I comment  it out you will see Go will complain again. How are interfaces used in Go interfaces are  very useful in coding because they allow you   to focus on the functionality instead of the  implementation details since interfaces are   implicit in Go they make it easy to add or  remove functionality making it real easy   to refactor your code for example consider a  database interface your application requires   storage but you don't care about which database is  used you create a database interface that declares   the method required by your application such  as finding or deleting a record then you can   code around the interface if you start with  a memory store when you are developing your   application you can later switch to Postgress  or any DB of your liking as your dependent code   doesn't care how the methods are implemented as  long as they are implemented this means that if   you want to change how things are done tomorrow  it will be easy for you to do so so interfaces   provide the flexibility to do that you can also  use embedding while creating interfaces let's   look at an example of the same first we have the  shape interface with area method then we create   a coloured shape interface embedding shape and  then adding a another method color which gives   out a string let us look at embedding interfaces  with an example now here I can create another   interface type object which is a interface has a  method name which gives the the name of the object   and I can embed the shape interface in here let's  now remove everything from here now I create cunk   printObject with takes o of type object returning  nothing just printing fmt.printf area and then the   name and we can do object.area object.name now  if we create our rectangle variable rectangle   which is a rectangle of width 5 and height 10  then if we pass it to print object it won't   work since object now has a name method along with  the area method which has been promoted from shape   to object because of embedding so I'm just going  to copy this method and then return a string say   it's name and then return rectangle as both the  area and name methods have been implemented from   the object interface it's no longer complaining if  you run the program now it should work as expected   as you can see the area and name were printed in  Go it's usually recommended that interfaces have   small number of methods maybe four or five that's  where embedding comes into play you can create a   larger interface by embedding smaller interfaces  while also keeping your code concise and precise. In Go programming language an empty interface  is a type of interface that doesn't have a   specific type or value assigned to it with  the introduction of Go generics in go 1.18 a   pre-declared identifier any was introduced as an  alias for an empty interface the empty interface   is used when the type of value is not known but  it's necessary to specify that the value can be of   any type since Go is a statically typed language  an empty interface is used to declare values that   can accept any type of data so an example of this  would be reading a JSON document where the values   could be of any type in the next lesson we will  cover type assertion which allows us to convert an   empty interface value to a value with the type  that's it for the lesson let's now summarize   everything we have learned in this lesson first  we went over how interfaces are abstract types   which only specify the functionality and not  how the functionality is implemented it leads   to a very maintainable code we also saw that  interfaces are implicit by defining a method   a type meets an interface you don't have to  explicitly say that your type is implementing   a interface it will meet the interface as long  as the type has implemented the method that's   it for this lesson please feel free to ask any  question you may have by posting a comment under   the video or reaching out to the Discord server I  hope you enjoyed it until next time happy coding!
Info
Channel: Code & Learn
Views: 1,172
Rating: undefined out of 5
Keywords: GoLang Tutorial, Interfaces, Code Examples, Programming, Learning Go, Go Programming Language, GoLang Development, Coding Tutorial, Code Demos, GoLang Best Practices, Software Development, GoLang Educational Video, Coding Examples, India
Id: 9dSgXiFuiEI
Channel Id: undefined
Length: 10min 22sec (622 seconds)
Published: Thu Jan 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.