Interview Questions

Get ready for your next interview with our comprehensive question library

Golang Interview Questions

Filter by Difficulty

1.

What is Go and what are its main characteristics?

beginner

Go (or Golang) is an open-source programming language developed by Google. Its main characteristics include:

  • Statically typed: Type checking at compile time
  • Compiled: Produces native machine code
  • Garbage collected: Automatic memory management
  • Concurrent: Built-in support for concurrent programming with goroutines
  • Simple syntax: Minimalist design with fewer keywords
  • Fast compilation: Quick build times
  • Cross-platform: Compiles to multiple operating systems and architectures
2.

How do you declare variables in Go?

beginner

Go provides multiple ways to declare variables:

// Explicit type declaration
var name string = "John"
var age int = 30

// Type inference
var name = "John"
var age = 30

// Short variable declaration (inside functions only)
name := "John"
age := 30

// Multiple variable declaration
var a, b, c int = 1, 2, 3
x, y := 10, "hello"
3.

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

beginner
  • var can be used at package and function level, supports explicit type declaration, and initializes with zero values if not assigned
  • := (short variable declaration) can only be used inside functions, always requires initialization, and uses type inference
// Package level - must use var
var globalVar string

func main() {
    // Both are valid inside functions
    var localVar string    // Zero value: ""
    shortVar := "hello"    // Type inferred as string
}
4.

What are Go's basic data types?

beginner

Go's basic data types include:

Numeric types:

  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • complex64, complex128

Other basic types:

  • bool (true/false)
  • string (UTF-8 encoded)
  • byte (alias for uint8)
  • rune (alias for int32, represents Unicode code point)
5.

How do you create and use constants in Go?

beginner

Constants are declared using the const keyword and cannot be changed after declaration:

const Pi = 3.14159
const MaxUsers = 100

// Grouped constants
const (
    StatusOK     = 200
    StatusError  = 500
    StatusPending = 202
)

// iota for auto-incrementing constants
const (
    Sunday = iota  // 0
    Monday         // 1
    Tuesday        // 2
)
6.

What are slices and how do they differ from arrays?

beginner

Arrays have fixed size, while slices are dynamic:

// Array - fixed size
var arr [5]int = [5]int{1, 2, 3, 4, 5}

// Slice - dynamic size
var slice []int = []int{1, 2, 3, 4, 5}
slice = append(slice, 6) // Can grow

// Creating slices
slice1 := make([]int, 5)      // length 5, capacity 5
slice2 := make([]int, 5, 10)  // length 5, capacity 10

Slices are more commonly used because of their flexibility and built-in functions like append().

7.

How do you work with maps in Go?

beginner

Maps are key-value pairs similar to hash tables:

// Creating maps
ages := make(map[string]int)
ages["Alice"] = 30
ages["Bob"] = 25

// Map literal
ages := map[string]int{
    "Alice": 30,
    "Bob":   25,
}

// Checking if key exists
age, exists := ages["Alice"]
if exists {
    fmt.Println("Alice's age:", age)
}

// Deleting keys
delete(ages, "Bob")
8.

What are structs in Go and how do you use them?

beginner

Structs are custom types that group related data:

type Person struct {
    Name string
    Age  int
    City string
}

// Creating struct instances
p1 := Person{Name: "Alice", Age: 30, City: "NYC"}
p2 := Person{"Bob", 25, "LA"} // positional
p3 := &Person{Name: "Charlie", Age: 35} // pointer to struct

// Accessing fields
fmt.Println(p1.Name)
p1.Age = 31
9.

How do pointers work in Go?

beginner

Pointers store memory addresses of variables:

var x int = 42
var p *int = &x  // p points to x

fmt.Println(*p)  // Dereference: prints 42
*p = 21          // Change value through pointer
fmt.Println(x)   // prints 21

// Zero value of pointer is nil
var ptr *int
if ptr == nil {
    fmt.Println("Pointer is nil")
}
10.

What are functions in Go and how do you define them?

beginner

Functions are defined using the func keyword:

// Basic function
func add(a, b int) int {
    return a + b
}

// Multiple return values
func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

// Named return values
func calculate(a, b int) (sum, product int) {
    sum = a + b
    product = a * b
    return // naked return
}
11.

How does error handling work in Go?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
12.

What are the control structures in Go?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
13.

What is the difference between `make` and `new` in Go?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
14.

How do you iterate over collections in Go?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
15.

What are packages in Go and how do you use them?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
16.

What is the `init()` function in Go?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
17.

How do you handle string manipulation in Go?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What are method receivers in Go?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
19.

What is type assertion in Go?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
20.

How do you work with JSON in Go?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 67 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime