Some concepts of Golang asked in Interviews

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

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.

  • 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).

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

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?

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.

Writing or applying is the best way to validate your learning.

Writing or applying is the best way to validate your learning.