Go is a programming language created at Google in 2009 by Robert Griesemer, Rob Pike, and Ken Thompson. It’s very easy to learn, and yet incredibly powerful, especially for concurrency. Apps built on this language can perform better than most of the other languages for similar purpose especially against dynamically typed language like PHP, ruby, python.,

Go is a decent choice for writing servers that handle thousands of queries per second with latencies of a few milliseconds.

Since Go as a programming language is a great choice to learn but the concept of object-oriented programming is revisited in Golang. Go language does not have the concept of class(instead it contain Struct), does not have support for inheritance(instead of support composition). Go language has been criticised for lack of generic support.

Go doesn’t have classic Object Oriented concept of classes and inheritance. Interfaces in Go provide a way to specify the behavior of an object: do like this and reuse it.

In Go programming language Interfaces are types that just defines a set of methods (the method set), but these methods do not contain code. This method is never implemented by the interface type directly. Also, an interface cannot contain variables. Go’s interface type provides a lot of extensible and compos-ability for Go applications. Interface types express generalizations or abstractions about the behaviors of other types.

Comparing instance of Java with Go lang, Java interface must be explicitly implemented by Java-like shown below:


public class Foo implements InterfaceName{

}

In Golang, the interface is not implemented explicitly like java but satisfied by type method.

Go also doesn’t have the typical class-like inheritance you’re normally used to, but it does have a couple of things that are pretty close.


# interface_demo.go
package main

import "fmt"

type Foo struct {

	}

func (f Foo) String() string  {
	return fmt.Sprintf("Empty Foo")
}

func main()  {
	f := Foo{}
	fmt.Println(f)
}

When I run above code with the command `go run interface_demo.go`, it will print `Empty Foo`

As I have already mentioned that Golang does not class instead it has type and struct. On above example, method ‘string’ added to Foo struct. ‘String()’ method is defined inside stringer interface defined in Golang. fmt.Printf calls the String method if available (checks if the provided variable satisfies Stringer interface).

Now we are going see(below code) how to duck type one subtype into superclass. We can substitute the struct variable with `interface` variable since all struct implements interface defined function.


package main

import "fmt"

type Animal interface{
	walk()
}
type Human struct  {

	}

type Dog struct {

}

func (Human) walk(){
	fmt.Println("its human walking")
}

func (Dog) walk(){
	fmt.Println("its Dog	 walking")
}

func printall_walker(a [] Animal){
  for _, a := range a {
  	a.walk()
  }
}

func main()  {

	printall_walker([] Animal{Human{}, Dog{}})
}

As seen above, Animal is an interface where method `walk` has been defined. Human and Dog struct added walk method, its mean implicitly implemented `Animal` interface. As we can see, now Human and Dog can be passed into Animal interface object.

Originally published in https://narutosanjiv.wordpress.com

Leave a Reply

Your email address will not be published. Required fields are marked *