Skip to content

🟣 Go Interview Questions Answered to help you get ready for your next backend developer interview.

Notifications You must be signed in to change notification settings

bsraya/golang-interview-questions

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 

Repository files navigation

πŸ–² 49 Most asked Go interview questions and answers in 2021

Golang was first published in 2009 with a combined effort of a lot of contributors and a dedicated team at Google. Go is used for creating big data systems, cloud-native applications, distributed systems, blockchain software, etc. Here are some of the most asked python questions you will probably get asked on your next interview in 2021.



You can also find all 68 answers here πŸ‘‰πŸΌ https://devinterview.io/dev/golang-interview-questions


πŸ”Ή 1. Is Go a new language, framework or library?

Answer:

Go isn't a library and not a framework, it's a new language.

Go is mostly in the C family (basic syntax), with significant input from the Pascal/Modula/Oberon family (declarations, packages). Go does have an extensive library, called the runtime, that is part of every Go program. 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.

Source:Β golang.orgΒ  Β 


πŸ”Ή 2. What is Go?

Answer:

Go is a general-purpose language designed with systems programming in mind. It was initially developed at Google in 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.

Source:Β tutorialspoint.comΒ  Β 


πŸ”Ή 3. What is static type declaration of a variable in Go?

Answer:

Static type variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable. A variable declaration has its meaning at the time of compilation only, compiler needs actual variable declaration at the time of linking of the program.

Source:Β tutorialspoint.comΒ  Β 


πŸ”Ή 4. What are some advantages of using Go?

Answer:

Go is an attempt to introduce a new, concurrent, garbage-collected language with fast compilation and the following benefits:

  • It is possible to compile a large Go program in a few seconds on a single computer.
  • Go provides a model for software construction that makes dependency analysis easy and avoids much of the overhead of C-style include files and libraries.
  • Go's type system has no hierarchy, so no time is spent defining the relationships between types. Also, although Go has static types, the language attempts to make types feel lighter weight than in typical OO languages.
  • Go is fully garbage-collected and provides fundamental support for concurrent execution and communication.
  • By its design, Go proposes an approach for the construction of system software on multicore machines.
Source:Β golang.orgΒ  Β 


πŸ”Ή 5. What kind of type conversion is supported by Go?

Answer:

Go is very strict about explicit typing. There is no automatic type promotion or conversion. Explicit type conversion is required to assign a variable of one type to another.

Consider:

i := 55      //int
j := 67.8    //float64
sum := i + int(j) //j is converted to int
Source:Β golangbot.comΒ  Β 


πŸ”Ή 6. Can you declared multiple types of variables in single declaration in Go?

Answer:

Yes. Variables of different types can be declared in one go using type inference.

var a, b, c =  3,  4,  "foo"  
Source:Β tutorialspoint.comΒ  Β 


πŸ”Ή 7. Why the Go language was created?

Answer:

Go was born out of frustration with existing languages and environments for systems programming.

Go is an attempt to have:

  • an interpreted, dynamically typed language with
  • the efficiency and safety of a statically typed, compiled language
  • support for networked and multicore computing
  • be fast in compilation

To meet these goals required addressing a number of linguistic issues: an expressive but lightweight type system; concurrency and garbage collection; rigid dependency specification; and so on. These cannot be addressed well by libraries or tools so a new language was born.

Source:Β golang.orgΒ  Β 


πŸ”Ή 8. What are Goroutines?

Answer:

Goroutines are functions or methods that run concurrently with other functions or methods. Goroutines can be thought of as light weight threads. The cost of creating a Goroutine is tiny when compared to a thread. Its common for Go applications to have thousands of Goroutines running concurrently.

Source:Β golangbot.comΒ  Β 


πŸ”Ή 9. Does Go have exceptions?

Answer:

No, Go takes a different approach. For plain error handling, Go's multi-value returns make it easy to report an error without overloading the return value. Go code uses error values to indicate an abnormal state.

Consider:

func Open(name string) (file *File, err error)
f, err := os.Open("filename.ext")
if err != nil {
    log.Fatal(err)
}
// do something with the open *File f
Source:Β golang.orgΒ  Β 


πŸ”Ή 10. What are the benefits of using Go programming?

Answer:

Following are the benefits of using Go programming:

  • Support for environment adopting patterns similar to dynamic languages. For example type inference (x := 0 is valid declaration of a variable x of type int).
  • Compilation time is fast.
  • In built concurrency support: light-weight processes (via goroutines), channels, select statement.
  • Conciseness, Simplicity, and Safety.
  • Support for Interfaces and Type embedding.
  • The go compiler supports static linking. All the go code can be statically linked into one big fat binary and it can be deployed in cloud servers easily without worrying about dependencies.
Source:Β tutorialspoint.comΒ  Β 


πŸ”Ή 11. What is a pointer?

Answer:

A pointer variable can hold the address of a variable.

Consider:

var x =  5  var p *int p =  &x
fmt.Printf("x = %d",  *p)

Here x can be accessed by *p.

Source:Β tutorialspoint.comΒ  Β 


πŸ”Ή 12. Can you return multiple values from a function?

Answer:

A Go function can return multiple values.

Consider:

package main
import "fmt"

func swap(x, y string) (string, string) { return y, x } func main() { a, b := swap("Mahesh", "Kumar") fmt.Println(a, b) }

Source:Β tutorialspoint.comΒ  Β 


πŸ”Ή 13. What is dynamic type declaration of a variable in Go?

Answer:

A dynamic type variable declaration requires compiler to interpret the type of variable based on value passed to it. Compiler don't need a variable to have type statically as a necessary requirement.

Source:Β tutorialspoint.comΒ  Β 


πŸ”Ή 14. How to efficiently concatenate strings in Go?

Answer:

Problem

In Go, a string is a primitive type, which means it is read-only, and every manipulation of it will create a new string.

So if I want to concatenate strings many times without knowing the length of the resulting string, what's the best way to do it?

Beginning with Go 1.10 there is a strings.Builder. A Builder is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use.

package main

import ( "strings" "fmt" )

func main() { var str strings.Builder

<span class="token cVar">for</span> i <span class="token cBase">:=</span> <span class="token cNum">0</span><span class="token cBase">;</span> i <span class="token cBase">&lt;</span> <span class="token cNum">1000</span><span class="token cBase">;</span> i<span class="token cBase">++</span> <span class="token cBase">{</span>
    str<span class="token cBase">.</span><span class="token cMod">WriteString</span><span class="token cBase">(</span><span class="token cString">"a"</span><span class="token cBase">)</span>
<span class="token cBase">}</span>

fmt<span class="token cBase">.</span><span class="token cMod">Println</span><span class="token cBase">(</span>str<span class="token cBase">.</span><span class="token cMod">String</span><span class="token cBase">(</span><span class="token cBase">)</span><span class="token cBase">)</span>

}

Source:Β stackoverflow.comΒ  Β 


πŸ”Ή 15. Explain this code

Answer:

Problem

In Go there are various ways to return a struct value or slice thereof. Could you explain the difference?

type MyStruct struct {
Val int
}

func myfunc() MyStruct { return MyStruct{Val: 1} }

func myfunc() *MyStruct { return &MyStruct{} }

func myfunc(s *MyStruct) { s.Val = 1 }

Shortly:

  • the first returns a copy of the struct,
  • the second a pointer to the struct value created within the function,
  • the third expects an existing struct to be passed in and overrides the value.
Source:Β stackoverflow.comΒ  Β 


πŸ”Ή 16. Let's talk variable declaration in Go. Could you explain what is a variable "zero value"?

Answer:

Variable is the name given to a memory location to store a value of a specific type. There are various syntaxes to declare variables in go.

// 1 - variable declaration, then assignment
var age int
age = 29

// 2 - variable declaration with initial value var age2 int = 29

// 3 - Type inference var age3 = 29

// 4 - declaring multiple variables var width, height int = 100, 50

// 5 - declare variables belonging to different types in a single statement var (
name1 = initialvalue1, name2 = initialvalue2 ) // 6 - short hand declaration name, age4 := "naveen", 29 //short hand declaration

If a variable is not assigned any value, go automatically initialises it with the zero value of the variable's type. Go is strongly typed, so variables declared as belonging to one type cannot be assigned a value of another type.

Source:Β golangbot.comΒ  Β 


πŸ”Ή 17. Have you worked with Go 2?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 18. What would you do if you need a hash displayed in a fixed order?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 19. How to copy map in Go?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 20. Name some advantages of Goroutines over threads

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 21. Is Go an object-oriented language?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 22. What is so special about constants in Go?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 23. What is the difference between C.sleep() and time.Sleep()?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 24. How do you swap two values? Provide a few examples.

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 25. What is "rune" type in Go?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 26. Can Go have optional parameters?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 27. How to check if a map contains a key in Go?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 28. What are the differences between unbuffered and buffered channels?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 29. What is the preferred way to handle configuration parameters for a Go program?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 30. What is the difference between the = and := operator?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 31. How to initialise a struct in Go?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 32. Why would you prefer to use an empty struct{}?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 33. Is there a foreach construct in the Go language?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 34. Implement a function that reverses a slice of integers

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 35. What is an idiomatic way of representing enums in Go?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 36. What are the use(s) for tags in Go?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 37. How can I check if two slices are equal?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 38. List the functions can stop or suspend the execution of current goroutine, and explain their differences.

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 39. When is the init() function run?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 40. What is the difference, if any, in the following two slice declarations, and which one is more preferable?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 41. What is $GOROOT and $GOPATH?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 42. Briefly describe how GC works in GO?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 43. How does Go compile so quickly?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 44. How to find a type of an object in Go?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 45. When go runtime allocates memory from heap, and when from stack?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 46. How to compare two interfaces in Go?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 47. What is the malloc threshold of Map object? How to modify it?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 48. What is the idiomatic Go equivalent of C's ternary operator?

πŸ‘‰πŸΌ Check all 49 answers


πŸ”Ή 49. What might be wrong with the following small program?

πŸ‘‰πŸΌ Check all 49 answers


About

🟣 Go Interview Questions Answered to help you get ready for your next backend developer interview.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published