Go Interview Questions

1. What is Golang?

Go, also known as Golang, is a statically typed, open-source programming language developed by Google. It is designed for efficiency, simplicity, and concurrency, making it ideal for system programming, web development, and cloud applications.

2. Why should one learn Golang? What are its advantages over other languages?

Golang is known for its fast execution speed, built-in concurrency, garbage collection, and easy-to-read syntax. Compared to languages like Python or Java, it provides better performance and compilation speed, making it suitable for scalable applications.

3. What are Golang packages?

A package in Go is a collection of related Go files grouped together to promote code reusability and modularity. The main package is essential for executable programs, while others are used as libraries.

4. What do you understand by the scope of variables in Go?

The scope of a variable defines the areas in which it can be accessed. In Go, variables can have block scope (inside functions or loops), package scope (accessible across files in the same package), or global scope (accessible throughout the program).

5. Is it possible to declare variables of different types in a single line of code in Golang?

Yes, Go allows multiple variable declarations in one line:

var x, y, z = 150, "TopfreeCorse", true

6. What is a "slice" in Go?

A slice provides a dynamic and flexible view of an underlying array. Unlike arrays, slices can change in size by expanding or shrinking.

7. What are Golang pointers?

Pointers store the memory address of a variable. They enable efficient memory management and modification of variables by reference.

8. What is the syntax used for the for loop in Golang?

Go uses a simplified for loop:

for i := 0; i < 10; i++ {
    fmt.Println(i)
}

It can also be used as a while loop by omitting the initialization and increment.

9. Can a function in Go return multiple values?

Yes, Go supports returning multiple values:

10. What do you understand by goroutine in Golang?

A goroutine is a lightweight thread managed by the Go runtime, allowing concurrent execution of functions.

11. What are Go channels and how are they used?

Channels facilitate safe communication between goroutines, preventing race conditions:

ch := make(chan int)
go func() { ch <- 10 }()
fmt.Println(<-ch)

12. What are Go Interfaces?

Interfaces define method sets without implementation, allowing polymorphism and flexibility in Go programs.

13. How can we check if a Go map contains a key?

Use the comma-ok idiom:

val, exists := myMap["key"]
if exists {
    fmt.Println("Key exists:", val)
}

14. Why is Golang fast compared to other languages?

Go compiles directly to machine code, has efficient garbage collection, and supports lightweight concurrency through goroutines.

15. What do you understand by Type Assertion in Go?

Type assertion retrieves the concrete type from an interface:

var i interface{} = "hello"
s := i.(string)
fmt.Println(s)

16. How will you check the type of a variable at runtime in Go?

Using reflect or type switches:

import "reflect"
fmt.Println(reflect.TypeOf(myVar))

17. What do you understand by variadic functions in Go?

Variadic functions accept multiple arguments of the same type:

func sum(nums ...int) int {
    total := 0
    for _, num := range nums {
        total += num
    }
    return total
}

18. What do you understand by byte and rune data types? How are they represented?

  • byte: Alias for uint8, represents a single ASCII character.
  • rune: Alias for int32, represents Unicode characters.

19. How can we copy a slice and a map in Go?

  • Slices: copy(dest, src)
  • Maps: Manually iterate and copy key-value pairs.

20. How can you sort a slice of custom structs with an example?

Use sort.Slice():

sort.Slice(persons, func(i, j int) bool {
    return persons[i].Age < persons[j].Age
})

21. Which is safer for concurrent data access: Channels or Maps?

Channels are safer for concurrent access, while maps require explicit synchronization using sync.Mutex or sync.Map.

22. In Go, are there any good error handling practices?

Use error as a return type, and check for errors explicitly:

23. How is GOPATH different from GOROOT in Go?

  • GOPATH: Specifies the workspace where Go projects reside.
  • GOROOT: Points to the Go installation directory.

24. Write a Go program to find the factorial of a number.

package main
import "fmt"

func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n-1)
}

func main() {
    fmt.Println(factorial(5)) // Output: 120
}

25. Can you format a string without printing?

Yes, you can format a string without printing it by using string formatting methods and storing the result in a variable. Here are some ways to do it in Python:

Example 1: Using f-strings

name = "Alice"
formatted_string = f"Hello, {name}!"

Example 2: Using .format()

name = "Alice"
formatted_string = "Hello, {}!".format(name)

Example 3: Using % Formatting

name = "Alice"
formatted_string = "Hello, %s!" % name