Some concepts of Golang asked in Interviews


1. Explain the go workspace. (src , bin , and pkg in GOPATH)

Riteek Srivastav
Oct 9, 2018 · 3 min read

In go you have to first specify the location of your workspace before any development and GOPATH is an environment variable which specifies the location of workspace of your go projects. By default, it is assumed to be $HOME/go.
GOPATH has src , bin and pkg directories.

  • src contains your go source files as packages.
  • bin contains executable commands like golint , gocov , glide , dep etc.
  • pkg contains package objects (suppose your project is in location $GOPATH/src/X/Y then the binary of the package with import path X/Y will be inpkg/{some-directory}/X/Y . {for more read this and this}

2. What are goroutines and how are they different from threads?

For this to answer you should have a good knowledge of goroutines. In short, we can say Goroutines are a way of doing tasks concurrently in golang. For more about goroutines and their difference with threads please please read this. After the reading the mentioned article you will have enough content to answer the questions related to goroutines.

3. What are function closures? When should we use those? Can you give one example?

A closure is a function value that references variables from outside its body. The function may access and assign to the referenced variables; in this sense, the function is “bound” to the variables. (reference).

There are lots of use cases of closure. Like isolating data is one of them. Suppose if you want a function that has access to the data the persist even after the function exists. You can understand this with an example for implementation of Fibonacci series.

In the above gist

return func() int {
n1, n2 = n2, (n2 + n1)
return n1}

this is called closure, and n1 and n2 are the variables that persist even after the execution of the function.

I have taken this example from here, you can visit this to see more example.

4. What are interfaces in GO?

In golang interface is a data structure which represents the signature of set of methods. <Will add the detailed information>

5. What is reflect? When should we use it?

The above two links will just give you the overview of reflection in golang, and its pros and cons. For the internal implementation you should read about official blog of reflection in go.

6. How to copy a slice, map and an interface?

<I am working on some examples, will add soon>

7. What are channels in go? What kind of data-structure do they follow?

Please refer this for the best understanding of channels in golang.

8. Does golang support pointer arithmetic?

No, golang does not support pointer arithemetic.

9. Does golang support inheritance?

No, golang does not support inheritance.

10. What is the difference between concurrency and parallelism?

Concurrency is dealing with multiple things at once (does not need to be done at the same time) with some time schedule while parallelism is doing the things at once at same time. For example if two threads are running on two different cores then it will be called parallelism but when two threads are scheduled on a single core with some scheduling algorithm then it will be called as concurrency.

If you want to add some more questions then please raise a PR at https://github.com/RiteekS/Golang-concepts, I will add those here also with answers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade