Lets Go ~ A Complete guide (Part VI)

Himanshu Singh
MindOrks
Published in
2 min readAug 9, 2018

So till now we covered the basics of Go, declaration , functions , structs , control statements in Go. Now, in this chapter we will discuss more about the Object Oriented Aspect of the Go Language.If you have not followed the previous parts the click here to get started.

The Index for this series is as followed,

  1. Introduction to Go
  2. Go Environment Setup
  3. Go Basics
  4. Control Statement and Functions
  5. Structs
  6. Object Oriented Programming in Go
  7. Concurrency

Method

  • Go Lang is typically not an object oriented programming language , but using type and methods give a feel of OOP in Go.

Note: A frequently asked question is “what is the difference between a function and a method”. A method is a function that has a defined receiver, in OOP terms, a method is a function on an instance of an object

  • A method is nothing but a special type of function which has a receiver.
  • Methods are able to access fields within receivers.

Syntax of Method,

func (r ReceiverType) funcName(parameters) (results)

Example,

Why do we need methods when we can do the same thing using Function?

  • We can write the above example using the functions and still we will get the same output.
  • Since, Go is not a Object oriented programming language and does not support classes behaviour. Methods come as a way to achieve behaviour similar to classes.
  • We can have same method names with different receiver types, but this same can’t be possible in Functions

We can also pass Pointer receivers in place of value receivers using the syntax,

func (r *ReceiverType) funcName(parameters) (results)
  • We pass pointer receivers in methods as Receivers because we can also change the value of the ReceiverType in pointer receivers

Example,

  • In Methods, we can use embedded in structs as well as we used in Structs in pervious part and it will work the same. It is called the Inheritance of Methods

Interfaces

  • Interface are group of methods that we use to define a set of actions.

Syntax,

type interfaceName interface {
//Custom Methods to implement
}
  • We don’t have to declare func keyword to declare at the beginning of each method.
  • Go already knows that an interface can only contains methods.

Example,

  • We can also implement empty interface,
var interfaceName interface{}

Embedded interfaces

  • Go in particular doesn’t offer inheritance by default. But to do this we can do it by creating new interfaces by embedding other interfaces inside the parent interface.

Syntax,

//First Interface
type DisplayFirstName interface {
DisplayFName()
}

//Second Interface
type DisplayLastName interface {
DisplayLName() string
}
//Embedded Interface
type EmployeeOperations interface {
DisplayFirstName
DisplayLastName
}
  • It is embedded same as the embedded structs works which was explained last part of the series

PS: This is all about the Object Oriented programming in Go. Coming up next is the most amazing and awesome feature of the Go Programming language.Concurrency,Channels and Go- Routines. To follow Part — VII click here.

--

--