Frequently asked: Go(Golang) Interview Questions and Answers

Vigo Webs
8 min readNov 30, 2022

Q1. What is Go?

Go is a general-purpose language designed with systems programming in mind. It was initially developed at Google in the year 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is strongly and statically typed, provides inbuilt support for garbage collection, and supports concurrent programming. Programs are constructed using packages, for efficient management of dependencies. Go programming implementations use a traditional compile and link model to generate executable binaries.

Q2. What are the benefits of using Go compared to other languages?

  1. Unlike other languages which started as academic experiments, Go code is pragmatically designed. Every feature and syntax decision is engineered to make life easier for the programmer.
  2. Golang is optimized for concurrency and works well at scale.
  3. Golang is often considered more readable than other languages due to a single standard code format.
  4. Automatic garbage collection is notably more efficient than Java or Python because it executes concurrently alongside the program.

Q3. How do you check a variable type at runtime?

The Type Switch is the best way to check a variable’s type at runtime. The Type Switch evaluates variables by type rather than value. Each Switch contains at least one case, which acts as a conditional statement, and a default case, which executes if none of the cases are true. For example, you could create a Type Switch that checks if interface value i contains the type int or string:

Q4. Explain the Array and Structs in Go?

Array: Arrays are used to store multiple values of the same type in a single variable, instead of declaring separate variables for each value. In Go, there are two ways to declare an array: 1. With the var keyword 2. With the := sign. Example:

Structs: A struct (short for structure) is used to create a collection of members of different data types, into a single variable. While arrays are used to store multiple values of the same data type into a single variable, structs are used to store multiple values of different data types into a single variable. A struct can be useful for grouping data together to create records. To declare a structure in Go, use the type and struct keywords: Example:

Q5. Explain the steps of testing with Golang.

Golang supports automated testing of packages with custom testing suites. To create a new suite, create a file that ends with _test.go and includes a TestXxx function, where Xxx is replaced with the name of the feature you’re testing. For example, a function that tests login capabilities would be called TestLogin. You then place the testing suite file in the same package as the file you wish to test. The test file will be skipped on regular execution but will run when you enter the go test command.

Q6. What are function closures?

Function closures is a function value that references variables from outside its body. The function may access and assign values to the referenced variables. For example: adder() returns a closure, which is each bound to its own referenced sum variable.

Q7. What are the looping constructs in Go?

Go has only one looping construct: the for loop. The for loop has 3 components separated by semicolons: The Init statement, which is executed before the loop begins. It’s often a variable declaration only visible within the scope of the for loop. The condition expression is evaluated as a Boolean before each iteration to determine if the loop should continue. The post statement is executed at the end of each iteration.

Q8. Explain the difference between concurrent and parallelism in Golang

Concurrency is when your program can handle multiple tasks at once while parallelism is when your program can execute multiple tasks at once using multiple processors. In other words, concurrency is a property of a program that allows you to have multiple tasks in progress at the same time, but not necessarily executing at the same time. Parallelism is a runtime property where two or more tasks are executed at the same time. Parallelism can therefore be a means to achieve the property of concurrency, but it is just one of many means available to you. The key tools for concurrency in Golang are goroutines and channels. Goroutines are concurrent lightweight threads while channels allow goroutines to communicate with each other during execution.

Q9. How do I migrate from Dep to Go Modules?

Migrating from Dep to Go Modules is the following steps.

  1. Run go version and make sure that using Go version 1.11 or later.
  2. Move your code outside of GOPATH or set export GO111MODULE=on.
  3. go mod init [module path]: This will import dependencies from Gopkg.lock.
  4. go mod tidy: This will remove unnecessary imports, and add indirect ones.
  5. (Optional) Delete your vendor folder (rm -rf vendor/ or move to trash)
  6. go build: Do a test build to see if it works.
  7. rm -f Gopkg.lock Gopkg.toml: Delete the obsolete files used for Dep

Go has imported my dependencies from Dep by reading the Gopkg.lock file and also created a go.mod file.

If you want to keep your vendor folder:

7. Run go mod vendor to copy your dependencies into the vendor folder.

8. Run go build -mod=vendor to ensure go build uses your vendor folder.

Q10. Explain When do we use break Statement, continue Statement and go Statement.

  • Break Statement: It is used for the termination of the switch statement or for loop and transfers the execution to the next statement following the switch statement or for a loop.
  • Continue Statement: It makes the loop easier to jump the rest of its body and immediately reassess one’s state before repeating it.
  • Go Statement: Using this, the control is transferred to the labeled statement.

Q11. What are looping constructs?

Go has only a single loop construct: ‘the for loop’. There are three components in the for loop separated by semicolons: Init statement, condition expression, and query statement.

  • The Init statement that is executed prior to the start of the loop. This is usually a variable statement visible only in the for loop scope.
  • The condition expression will be evaluated as a Boolean prior to each iteration to decide whether the loop should continue or not.
  • The query statement will be executed at the end of every iteration.

Q12. Does Go have a runtime?

Go does have an extensive library, called the runtime, that is part of every Go program. The runtime library implements garbage collection, concurrency, stack management, and other critical features of the Go language. Although it is more central to the language, Go’s runtime is analogous to libc, the C library. It is important to understand, however, that Go’s runtime does not include a virtual machine, such as is provided by the Java runtime. Go programs are compiled ahead of time to native machine code (or JavaScript or WebAssembly, for some variant implementations). Thus, although the term is often used to describe the virtual environment in which a program runs, in Go the word “runtime” is just the name given to the library providing critical language services.

Q13. How does garbage collector work in Go?

The Go garbage collector helps developers by automatically freeing the memory of their programs when it is not needed anymore. However, keeping track of the memory and cleaning could impact the performances of our programs. The Go garbage collector has been designed to achieve those goals, and focus on:

  • reducing as much as possible in the two phases when the program is stopped, also called “stop the world.”
  • a cycle of the garbage collector that takes less than 10ms.
  • the garbage collection cycle should not take more than 25% of the CPU.

Q14. Can I convert []T1 to []T2 if T1 and T2 have the same underlying type?

This last line of this code sample does not compile.

In Go, types are closely tied to methods, in that every named type has a (possibly empty) method set. The general rule is that you can change the name of the type being converted (and thus possibly change its method set) but you can’t change the name (and method set) of elements of a composite type. Go requires you to be explicit about type conversions.

Q15. Why does Go not have variant types?

Variant types, also known as algebraic types, provide a way to specify that a value might take one of a set of other types, but only those types. A common example in systems programming would specify that an error is, say, a network error, a security error, or an application error and allow the caller to discriminate the source of the problem by examining the type of the error. Another example is a syntax tree in which each node can be a different type: declaration, statement, assignment, and so on. We considered adding variant types to Go, but after discussion decided to leave them out because they overlap in confusing ways with interfaces. What would happen if the elements of a variant type were themselves interfaces? Also, some of what variant types address are already covered by the language. The error example is easy to express using an interface value to hold the error and a type switch to discriminate cases. The syntax tree example is also doable, although not as elegantly.

To read more Golang Interview Questions and answer check out our Android App from play store:

Our app contains 1700+ Interview Questions and answers with clear code examples from trending technologies.