Loading ...

Go (Golang) Guide: Top 30 Interview Questions & Answers

Go (Golang) is a statically typed, compiled programming language developed by Google. Known for its simplicity, concurrency support, and fast execution, Go is widely used in cloud-native development, microservices, and system programming. This guide provides 30 top Go interview questions with detailed explanations and code examples for beginners and experienced developers.

1. What is Go (Golang)?

Explanation:
Go is an open-source programming language designed for simplicity, performance, and concurrency. It supports garbage collection, strong typing, and built-in concurrency with goroutines.

Example:

package main import "fmt" func main() { fmt.Println("Hello, Go!") }

2. Difference between Go and other languages like Java or C++

FeatureGoJava/C++
CompilationCompiled to machine codeJava: bytecode, C++: native
ConcurrencyGoroutines & channelsThreads, Executor services
Memory managementGarbage-collectedManual or GC (Java)
SimplicityMinimalisticComplex syntax

3. What are Goroutines?

Explanation:
Goroutines are lightweight threads managed by the Go runtime. They are used for concurrent programming.

Example:

package main import "fmt" func sayHello() { fmt.Println("Hello from goroutine") } func main() { go sayHello() // runs concurrently fmt.Println("Hello from main") }

4. What are Channels in Go?

Channels are used to communicate between goroutines safely.

Example:

package main import "fmt" func main() { messages := make(chan string) go func() { messages <- "Hello Channel" }() msg := <-messages fmt.Println(msg) }

5. Difference between buffered and unbuffered channels

Channel TypeBehavior
UnbufferedSend blocks until receiver receives
BufferedSend does not block until buffer full

Example:

ch := make(chan int, 2) // buffered channel

6. What is a Go Module?

Explanation:
Go modules manage dependencies and versions in Go projects.

Example:

go mod init myproject go get github.com/gin-gonic/gin

7. How to handle errors in Go

Go does not use exceptions; instead, it returns errors as values.

Example:

package main import ( "errors" "fmt" ) func divide(a, b int) (int, error) { if b == 0 { return 0, errors.New("division by zero") } return a / b, nil } func main() { result, err := divide(10, 0) if err != nil { fmt.Println("Error:", err) } else { fmt.Println("Result:", result) } }

8. What is the difference between := and var in Go?

  • := – short variable declaration, infers type

  • var – explicit declaration, optional initialization

x := 10 // type inferred var y int = 20 // type specified

9. What are Structs in Go?

Structs are used to group related fields together.

Example:

type Person struct { Name string Age int } func main() { p := Person{Name: "Alice", Age: 25} fmt.Println(p) }

10. What are Interfaces in Go?

Interfaces define method sets that a type must implement.

Example:

type Speaker interface { Speak() } type Dog struct{} func (d Dog) Speak() { fmt.Println("Woof!") } func main() { var s Speaker = Dog{} s.Speak() }

11. Difference between Array and Slice

FeatureArraySlice
SizeFixedDynamic
Declarationvar arr [3]intarr := []int{1,2,3}

12. What are Maps in Go?

Maps are key-value pairs, similar to dictionaries in Python.

Example:

m := make(map[string]int) m["Alice"] = 25 fmt.Println(m["Alice"])

13. How to use Go’s defer keyword?

Defer postpones execution of a function until surrounding function returns.

Example:

func main() { defer fmt.Println("This runs last") fmt.Println("This runs first") }

14. Explain go fmt

go fmt formats Go code according to standard conventions. It ensures consistency across projects.


15. How to use Go’s select statement?

select is used to wait on multiple channel operations.

Example:

select { case msg1 := <-ch1: fmt.Println(msg1) case msg2 := <-ch2: fmt.Println(msg2) default: fmt.Println("No communication") }

16. What is panic and recover?

  • panic stops normal execution and begins unwinding stack

  • recover stops a panic inside a deferred function

defer func() { if r := recover(); r != nil { fmt.Println("Recovered:", r) } }() panic("something went wrong")

17. How to implement concurrency safely?

  • Use goroutines for parallelism

  • Use channels or sync package to avoid race conditions

var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() fmt.Println("Concurrent Task") }() wg.Wait()

18. What are Go Packages?

Packages organize related functions, types, and interfaces.

package mathutils func Add(a, b int) int { return a + b }

19. How to run tests in Go

Use Go’s built-in testing package:

func TestAdd(t *testing.T) { if Add(2,3) != 5 { t.Error("Expected 5") } }

Run:

go test

20. What is the difference between new and make?

FunctionUsage
newAllocates memory, returns pointer
makeInitializes slices, maps, channels

21. How to handle JSON in Go

Example:

type User struct { Name string `json:"name"` Age int `json:"age"` } data := []byte(`{"name":"Alice","age":25}`) var u User json.Unmarshal(data, &u) fmt.Println(u.Name)

22. Difference between copy and append

  • copy – copies elements from one slice to another

  • append – adds elements to a slice

s1 := []int{1,2} s2 := append(s1, 3)

23. What are Pointers in Go?

Pointers store the memory address of a variable.

x := 10 p := &x fmt.Println(*p) // 10

24. Explain iota in Go

iota is used for enumeration constants.

const ( A = iota // 0 B // 1 C // 2 )

25. What is Go’s init() function?

init() executes before main() and is used for setup.

func init() { fmt.Println("Initializing...") }

26. How to handle time and date in Go

t := time.Now() fmt.Println("Current time:", t.Format("2006-01-02 15:04:05"))

27. Explain Go Interfaces vs Abstract Classes

  • Go interfaces only define methods; no fields

  • Go does not have abstract classes; interfaces + composition are used instead


28. How to implement REST API in Go

Using Gin framework:

r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{"message": "pong"}) }) r.Run(":8080")

29. Difference between Go routines and Threads

FeatureGoroutineThread
MemoryLow (~2KB)High (~1MB)
SchedulingManaged by Go runtimeOS managed
CreationCheapExpensive

30. How to handle environment variables

port := os.Getenv("PORT") if port == "" { port = "8080" }

✍️ By Ashish | 2025-10-21T09:00:56.147Z

Call Our Course Advisors

IND: +91-98018 30173 / +91-70429 28331

US: +1-252 490 1033

UK: +44-121 3871751