← Back to list

Go (Golang) Interview Questions: Essential Set 2 with Examples

1. What are maps in Go?

Ikbalmahmudsohan · 2024-11-27 07:09 · 16 claps · 1.9 min read
#go #interview #backend #go-tutorial #go-interview
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Go (Golang) Interview Questions: Essential Set 2 with Examples

1. What are maps in Go?

Maps in Go are built-in data structures used to store key-value pairs. Keys must be unique, and values are associated with their respective keys for efficient retrieval.

Example:

package main

import "fmt"

func main() {
 capitals := map[string]string{
  "Bangladesh": "Dhaka",
  "France":     "Paris",
  "Germany":    "Berlin",
  "Australia":  "Canberra",
 }
 capitals["India"] = "Delhi"
 capitals["Japan"] = "Tokyo"

 for key, val := range capitals {
  fmt.Printf("The capital of %v is %v \n", key, val)
 }

 if capital, exists := capitals["Japan"]; exists {
  fmt.Printf("Capital of Japan is %v \n", capital)
 } else {
  fmt.Println("Japan is not in the map.")
 }

}

2. How does Go achieve concurrency?

Go achieves concurrency using goroutines and channels:

  • Goroutines: Lightweight threads of execution managed by the Go runtime.
  • Channels: Used to safely share data between goroutines.

Example:

package main

import (
 "fmt"
 "time"
)

func printNumbers() {
 for i := 0; i <= 5; i++ {
  fmt.Println(i)
  time.Sleep(500 * time.Millisecond)
 }
}

func main() {
 go printNumbers()
 fmt.Println("Main function continues")
 time.Sleep(3 * time.Second)
}

3. Can you explain the use of interfaces in Go?

Interfaces define a set of method signatures that a type must implement. They enable polymorphism and allow functions to work with any type that satisfies the interface.

Example:

package main

import "fmt"

type Shape interface {
 Area() float64
}

type Rectangle struct {
 Width  float64
 Height float64
}

func (r Rectangle) Area() float64 {
 return r.Height * r.Width
}

type Circle struct {
 Radius float64
}

func (c Circle) Area() float64 {
 return 3.1416 * c.Radius * c.Radius
}
func PrintArea(s Shape) {
 fmt.Println("Area: ", s.Area())
}

func main() {
 r := Rectangle{
  Width:  5,
  Height: 10,
 }
 c := Circle{
  Radius: 7,
 }
 PrintArea(r)
 PrintArea(c)
}

4. What is a pointer in Go, and how is it used?

A pointer holds the memory address of a value. They allow functions to modify variables outside their local scope.

Example:

package main

import "fmt"

func updateValue(val *int) {
 *val = 20 // Dereferencing pointer to update value
}

func main() {
 num := 10
 fmt.Println("Before update:", num)

 // Passing address of num
 updateValue(&num)
 fmt.Println("After update:", num)
}

5. Describe the scope rules in Go.

Go uses block-level scope:

  • Variables declared inside a block (e.g., a function or loop) are limited to that block.
  • Variables declared outside any function (package-level) are accessible throughout the package.

Example:

package main

import "fmt"

// Package-level variable
var globalVar = "I am global"

func main() {
 // Block-level variable
 localVar := "I am local"

 fmt.Println(globalVar) // Accessible
 fmt.Println(localVar)  // Accessible

 {
  innerVar := "I am inside a block"
  fmt.Println(innerVar) // Accessible only within this block
 }
 // fmt.Println(innerVar) // Error: undefined
}

메타데이터
post_id
e14f4ecb66b3
slug
go-golang-interview-questions-essential-set-2-with-examples-e14f4ecb66b3
url
https://medium.com/@ikbalmahmudsohan/go-golang-interview-questions-essential-set-2-with-examples-e14f4ecb66b3
canonical_url
https://medium.com/@ikbalmahmudsohan/go-golang-interview-questions-essential-set-2-with-examples-e14f4ecb66b3
author_url
https://medium.com/@ikbalmahmudsohan
status
ok
fetched_at
2026-06-24 13:29:15