Golang Interview questions

Golang Interview Questions

If you’re going for a career in Go, then there are a few essential fundamentals that are regularly asked in GoLang interviews. Here I bring you some of the more conceptual GoLang interview questions and their appropriate answers. So read on.

1. So I see that you are interested in Go. Can you explain to me what it is, and why I would need to Go?

Yes, I am highly interested in Golang, and I think large companies should be excited about this language. It is a programming language made by Google and released as Open Source a decade ago, that has been specifically designed for scaling businesses. It has inbuilt concurrency support, which means your applications run on multiple cores by default – reducing time and resources, esp. if you’re a huge company with millions of application requests per hour.

2. What is the meaning of this concurrency? Can you explain with an example?

In Golang, every analogy uses a Gopher, which is like a small rabbit. So let me try to use the same. Consider there are two bookshelves on either side of a big room, and there is one gopher with a trolley who has to shift the books from one shelf to the other in order. He cannot just throw the books. So he’ll take some x amount of time. Now if we increase the number of gophers to two, then we have a linear decrease in the time since one gopher will almost always be alternating taking a book trolley.

We can optimize this setup further. We could increase one gopher and create a designated pile in the middle. One gopher would bring the books from one shelf to the middle, the second gopher will stack it in order and the third will bring the books from the middle stack to the other bookshelf. It may seem like we’re using more gophers, so more resources. But that’s not it. Goroutines (the analogy for these gophers) are actually very cheap. 10000 Goroutines run within 8 seconds.

3. Does Go support Object Oriented Programming?

Even though Go is Object Oriented, there are no specific keywords like Class, extends, or implements or similar. In Go, every data type is equal, i.e, we can define methods on any data type. We have a struct type, which is a list of fields and can be considered closest to a class.

4. What is the package system in Go like? And how do you like the syntax compared to Python or Java?

Go’s packages are essential to any Go program. We always start with package main and then import any packages that we may need with the keyword import. There are packages for everything – printing, opening and reading files, performing math operations, etc. Also regarding the syntax, as you point out, we have to define the complete path to a package to use it. This avoids confusion. Also, the Go language is statically typed, so it has much faster compile times than a language like Python.

5. What is the meaning of static typing, as you mentioned? Is it the same as interpreted language?

A static typed language like Go wouldn’t allow its users to change data types for a variable that has been defined. For example, you cannot pass a variable of int32 into a function that accepts int64. Go is also NOT an interpreted language, which means we have to define the datatype of each variable, otherwise there will be an error. Compared to this, Python is a dynamic typed and interpreted language, which means at runtime, it does several tasks internally that it uses to “figure out” the various data types. Go never does this.

6. Don’t you think this is a disadvantage?

No, it is not a disadvantage if we think about the extra time taken by Python to do these tasks. While it may be negligible for 1-2 variables, the difference in times for Go and Python for larger programs is a lot significant.

7. Very well. Can you tell me what are the built-in support in Go?

Yes, Go has in built support for a lot of important tasks that are required by companies. Some of them are:

  • Container: container/heap, container/list
  • Web Server: net/http
  • Cryptography: Crypto/md5 ,crypto/sha1
  • Compression: compress/ gzip
  • Database: database/sql

These packages have been optimised by a global community of dedicated Go developers, using Goroutines for multithreading.

8. What is multithreading? Is it the same as parallelism?

  • Concurrency is when two tasks can start, run, and complete in overlapping time periods. Parallelism is when tasks literally run at the same time, eg. on a multi-core processor.
  • Concurrency is the composition of independently executing processes, while parallelism is the simultaneous execution of (possibly related) computations.
  • Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.
  • An application can be concurrent – but not parallel, which means that it processes more than one task at the same time, but no two tasks are executing at same time instant.
  • An application can be parallel – but not concurrent, which means that it processes multiple sub-tasks of a task in multi-core CPU at same time.
  • An application can be neither parallel – nor concurrent, which means that it processes all tasks one at a time, sequentially.
  • An application can be both parallel – and concurrent, which means that it processes multiple tasks concurrently in multi-core CPU at same time .

9. What about testing? Can we perform testing in Go?

Yes, there is a testing framework that comprises the go test command and the testing package.
In order to write a test, you are required to create a file whose name ends with _testing. Go contains the function named TestXXX with the signature func(t*testing.T).

10. What are pointers in Go used for?

Pointers are variables that hold the address of any variable. Pointers in Go are also called special variables. There are two operators in pointers they are

  • * operator which is also called a de-referencing operator used to access the value in the address
  • & operator which is also called as address operator this is utilised to return the address of the variable

Other things you should know

Even, though these questions above are more of the broad conceptual questions, there are several technical questions that can be asked in an interview – for example, on the various data types, how arrays are created, the difference between the various print functions, what are ASTs and how to parse them, what are array slices, etc.