GO(Golang) programming language

Compiled
Garbage Collected
Concurrent

Image for post
Image for post
Image for post
Image for post

Disadvantages of Inheritance:-

  1. One of the main disadvantages of inheritance is the increased time/effort it takes the program to jump through all the levels of overloaded classes. If a given class has ten levels of abstraction above it, then it will essentially take ten jumps to run through a function defined in each of those classes
  2. Main disadvantage of using inheritance is that the two classes (base and inherited class) get tightly coupled. This means one cannot be used independent of each other.
  3. Also with time, during maintenance adding new features both base as well as derived classes are required to be changed. If a method signature is changed then we will be affected in both cases (inheritance & composition)
  4. If a method is deleted in the “super class” or aggregate, then we will have to re-factor in case of using that method.Here things can get a bit complicated in case of inheritance because our programs will still compile, but the methods of the subclass will no longer be overriding superclass methods. These methods will become independent methods in their own right.

Language specifics:

  1. Entry point: Package with name main and a function with name main.
  2. Data types:
Image for post
Image for post
Image for post
Image for post

3. Very important to understand the difference between Method and Function.
The term “method” came up with object-oriented programming. In an OOP language (like C++ for example) you can define a “class” which encapsulates data and functions which belong together. Those functions inside a class are called “methods” and you need an instance of that class to call such a method.

In Go, the terminology is basically the same, although Go isn’t an OOP language in the classical meaning.

Methods in Go:

In Go, a function which takes a receiver is usually called a method. The method receiver appears in its own argument list between the func keyword and the method name.

Look at this example below:

Image for post
Image for post

Greeting() method is actually a part of User type since it takes receiver of type User.

Functions in Go:

A function declaration tells the compiler about a function name, return type, and parameters. A function definition provides the actual body of the function.

func function_name( [parameter list] ) [return_types]
{
body of the function
}

Returning multiple values from Function:
func swap(x, y string) (string, string) {
return y, x
}

Note: Go uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.

Go programming language provides the flexibility to create functions on the fly and use them as values.

Go programming language supports anonymous functions which can acts as function closures. Anonymous functions are used when we want to define a function inline without passing any name to it.

Interfaces in Go:

An interface type is defined by a set of methods. A value of interface type can hold any value that implements those methods.

Image for post
Image for post

Explanation:
Greet function takes an interface type as argument.
User type has implemented a Name function.
So Greet function can be invoked with User instance as argument.

Concurrency in Go:

Go encourages a different approach in which shared values are passed around on channels and, in fact, never actively shared by separate threads of execution. Only one goroutine has access to the value at any given time.Data races cannot occur, by design. A goroutine is a lightweight thread managed by the Go runtime.To encourage this way of thinking we have reduced it to a slogan:

Do not communicate by sharing memory; instead, share memory by communicating.

Channels are a typed conduit through which you can send and receive values with the channel operator, <-.
ch <- v // Send v to channel ch.
v := <-ch // Receive from ch, and assign value to v.

By default, sends and receives block wait until the other side is ready. This allows goroutines to synchronize without explicit locks or condition variables.

Suffering from Knowledge Quest

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store