Golang — how it is different

Himanshu Chaudhary
Analytics Vidhya
Published in
4 min readMar 16, 2021

Golang is easy to understand and faster to compile. There is a famous joke in Go; Golang is created when waiting for C++ to compile. It seems a joke but it is somewhat true, Rob Pike one of the author of golang was waiting for C++ code to compile then he decided, he can not handle it anymore and golang is born.

Golang is good for industrial projects and most companies are starting to move towards golang and grpc services. Simple to understand and easy to maintain should be the basis of golang project. If the code we create in golang is hard to understand by another programmer after looking at it, it is an example of bad code, it should be refactored by using go tools and its powerful packages.

StorieI have started learning golang 4 years ago and since then I am trying to use it to make my code simple while efficient and erratic to add features. I myself was working on node and python before golang. Although programmers coming from OOPS background trying to learn go, still implements Object Oriented Concepts into structural programming language (nothing wrong with it) but if it was created by keeping OOPS in mind, then the language will not be structural language that too derived from C.

A big example of golang not to be used as other OOPS language is the way it treats interfaces. We all are aware of interface to be used as abstraction of methods. In golang we use interface in two ways:- One as an interface to define the behaviour of methods rather than abstraction of methods as in other object oriented languages. In other way it is used as a generic type toStorie capture the values of any primitive or complex data type.

Let start with an example of interface in go:-

In above example, I have created an interface having Read method due to which it will implement reader interface and can capture any type implementing reader interface.

Reason why I have implemented a reader interface and using it inside CustomRead interface to explain how golang is different from oops language when it comes to interfacing. In this code I have defined the behaviour of CustomRead interface which is implementing Reader interface.

Go works better with UNIX philosophy, create small tasks to achieve bigger task. Trying to implement oops concepts in golang makes project code more messy sometimes and hard to maintain for future. The best example is how a package or a function is defined in golang viz. Reader interface or net/http package. Http can be part of net package but in golang it is created as a separate package inside net package just to work on http part.

When in go, do as Gophers do

Another unique feature in golang, everything is passed by value. Golang creates a copy of argument passed to a method and work on it. Whether it as an argument to a method or receiver to a method everything is considered as pass by value.

Above code is a good example to show that everything it passed by value. The issue with above code is it will not work and throw an error:

error.go:18:7: cannot use dog (type Dog) as type Animal in argument to Speak:
Dog does not implement Animal (Speak method has pointer receiver)

Mentioned code will work by passing Dog struct as pointer not as value. But what if I pass cat as pointer will it work ? Surprisingly it will work even if the cat receiver is not a pointer type.

The reason behind the code is working by passing cat struct as pointer but not by passing dog struct as value is - when passing Dog struct as value, a copy will be created but it will point to a different address of new Dog struct value hence the error. While passing Cat as pointer the value can be created using the address pointer provided for Cat.

Concluding all, there are few points to consider when working with golang:-

  • Interfaces should be used for behaviours not for abstractions.
  • Interfaces are used a generic type as in empty interface.
  • Interface should contains methods for a specific behaviour.
  • Always accept interface and return structs.
  • A pointer type variable can call the methods of value type but not vice-versa.
  • everything is passed by value, even the receiver of a method.

If you have any questions in golang and understanding its architecture. Email me on himanshusdec8@gmail.com

Follow https://golang-geek.medium.com/ for updates…

--

--

Himanshu Chaudhary
Analytics Vidhya

Cloud Engineer, Kubernetes, Docker, CI/CD, Golang, gRPC, Micro services, git, Infrastructure as a Service, Storage, Compute, Protobuf, REST.