50 Golang Foundation Interview Questions

Setup & Ecosystem

1. What does the go run command do, and when should you use it versus go build?
Answer: go run compiles and executes your code on the fly in a temporary directory (best for development/testing). go build compiles your code into a standalone, persistent executable binary file (best for production/deployment).

2. What is the output of the go build command?
Answer: An executable binary file specific to your target operating system and architecture.

3. What is the primary purpose of the go fmt command?
Answer: It automatically formats your Go source code to adhere to the official standard formatting rules (spacing, indentation, bracket placement, etc.).

4. Why is go fmt considered a cornerstone of Go’s culture compared to formatting in other languages?
Answer: It eliminates debates over “code style” among developers. Almost all Go code in the world looks exactly the same, making it incredibly easy to read other people’s code.

5. What is the traditional entry-point function for a standalone Go application?
Answer: func main()

6. Which package must the entry-point function belong to?
Answer: package main

7. What is the standard file extension used for Go source code?
Answer: .go

8. How do you initialize a new Go project tracking dependencies via the command line?
Answer: go mod init <module-name> (e.g., go mod init github.com/user/myproject).

9. What is the go.mod file, and what is its role in a Go project?
Answer: A file that defines your project’s module path and tracks all your third-party dependencies and their specific versions.

10. How can you check which version of Go is currently installed on your machine?
Answer: Run go version in your terminal.

11. What are GOOS and GOARCH, and how are they used when compiling Go code?
Answer: They are environment variables used for cross-compilation. GOOS specifies the target Operating System (e.g., linuxwindows), and GOARCH specifies the architecture (e.g., amd64arm64).

12. How do you format all Go files in your current directory and all subdirectories at once?
Answer: go fmt ./...


Variables & Data Types

13. What is the difference between declaring a variable with var x int and x := 0?
Answer: var x int explicitly declares a variable of type integer (it gets a default value of 0). x := 0 is the short declaration operator; it infers the type as int and assigns it 0 in one step.

14. Can the short variable declaration operator (:=) be used at the package level (outside of a function)? Why or why not?
Answer: No. Every statement at the package level (outside of a function) must start with a Go keyword (like varfuncconst).

15. What is a “zero value” in Go?
Answer: The default value Go assigns to a variable if you declare it without explicitly giving it a value. Go does not have null or undefined for basic types.

16. What are the zero values for intstring, and bool?
Answer: 0"" (empty string), and false.

17. How do you declare a constant in Go?
Answer: Using the const keyword (e.g., const pi = 3.14).

18. Can you use the short declaration operator (:=) to declare a constant?
Answer: No. := can only be used for variables.

19. What happens if you declare a local variable inside a function but never use it?
Answer: The Go compiler will throw an error and refuse to compile. (Go strictly enforces removing unused variables to keep code clean).

20. How do you declare multiple variables of the same type on a single line using var?
Answer: var x, y, z int

21. What is the difference between intint32, and int64?
Answer: int32 is strictly a 32-bit integer, and int64 is strictly 64-bit. int changes size depending on the architecture of the machine running the code (32-bit on 32-bit systems, 64-bit on 64-bit systems).

22. How do you write a multi-line string literal in Go?
Answer: Use backticks (`) instead of double quotes (").

23. What is a rune in Go, and which basic data type is it an alias for?
Answer: It represents a single Unicode character (code point). Under the hood, it is just an alias for the int32 data type.

24. How do you explicitly convert a variable of type int to type float64?
Answer: Use type conversion syntax: float64(x).

25. Is Go a statically typed or dynamically typed language?
Answer: Go is a statically typed language (types are checked at compile time).


Control Structures

26. What is the correct syntax for an if statement in Go? Are parentheses required around the condition?
Answer: if x > 0 { ... }. Parentheses are not required around the condition, but curly braces {} are strictly mandatory.

27. How do you declare and initialize a temporary variable on the same line as an if statement?
Answer: if err := doSomething(); err != nil { ... }

28. What is the scope of a variable declared within the initialization block of an if statement?
Answer: It is scoped only to the if block and any subsequent else if or else blocks attached to it.

29. Since Go does not have a while keyword, how do you write the equivalent of a while loop using for?
Answer: You use a for loop with only a condition: for x < 10 { ... }.

30. How do you write an infinite loop in Go?
Answer: for { ... }

31. What are the three components of a traditional for loop in Go (initialization, condition, post)?
Answer: Initialization, Condition, Post-statement (e.g., for i := 0; i < 10; i++ { ... }).

32. What does the break keyword do when placed inside a for loop?
Answer: It completely halts the loop and moves execution to the code immediately following the loop.

33. What does the continue keyword do when placed inside a for loop?
Answer: It skips the rest of the current loop iteration and immediately jumps to the next iteration.

34. In a Go switch statement, do cases automatically “fall through” to the next case if there is no break statement?
Answer: No. Unlike C or Java, Go switch cases do not fall through by default. They break automatically after a match executes.

35. Which keyword must you use if you want a switch case to fall through to the next block of code?
Answer: fallthrough

36. Can a single case in a switch statement evaluate multiple comma-separated expressions?
Answer: Yes. case 1, 2, 3: will execute the block if the expression matches 1, 2, or 3.

37. What is a “tagless” switch (a switch without an expression next to the word switch), and what is it equivalent to?
Answer: A switch without a variable to evaluate, like switch { case x > 0: ... }. It is functionally equivalent to switch true { ... } and is often used as a cleaner alternative to long if/else if chains.

38. Can you use strings or floating-point numbers as the evaluation expression in a switch statement?
Answer: Yes, Go’s switch statement is very flexible and can evaluate strings, floats, and other types.


Functions

39. What is the basic syntax for defining a function in Go that takes two integers and returns an integer?
Answer: func add(x int, y int) int { return x + y } (or shortened to func add(x, y int) int).

40. How does Go allow a single function to return multiple values? Provide a conceptual example.
Answer: You define multiple return types inside parentheses. Example: func divide(a, b int) (int, error) { ... return result, err }.

41. What is the blank identifier (_), and how is it useful when calling a function that returns multiple values?
Answer: It is used to ignore a return value. If a function returns two values and you only need the first, you write: val, _ := myFunc(). This prevents the “unused variable” compiler error.

42. What are “named return values” in a Go function signature?
Answer: You can define names for your return values in the signature (e.g., func doWork() (result string, err error)). Go treats them as local variables initialized to their zero values.

43. What is a “naked return” in Go?
Answer: In a function with named return values, a naked return is simply the word return by itself. It automatically returns the current values of the named variables.

44. Why is it generally considered bad practice to use naked returns in long or complex functions?
Answer: In long functions, it hurts readability because the developer has to scroll back up to the function signature to figure out exactly what variables are being returned.

45. What is a variadic function?
Answer: A function that can accept a variable (unlimited) number of arguments for a specific parameter (e.g., fmt.Println is variadic).

46. How do you specify a variadic parameter in a function signature (e.g., a function that takes an arbitrary number of strings)?
Answer: Place an ellipsis (...) before the type. Example: func printNames(names ...string).

47. If a function has both standard parameters and a variadic parameter, where must the variadic parameter be positioned in the argument list?
Answer: It must be the absolute last parameter in the function signature.

48. How do you pass an existing slice of items into a variadic parameter?
Answer: You “unpack” the slice by adding ... after the slice variable name. Example: printNames(mySlice...).

49. Are arguments passed to functions in Go by value or by reference by default?
Answer: Arguments in Go are passed by value by default. A copy of the data is given to the function. (To pass by reference, you must explicitly use pointers).

50. Can you define a function inside of another function (an anonymous function) in Go?
Answer: Yes, you can define a function inside another function without a name, assign it to a variable, or execute it immediately (closures).

Please follow and like us:

Leave a Comment