Go Skills

Golang Interview Questions

Why Go

1.Simplicity : simple and easy to understand, syntax is clean.

2.Concurrency : build-in support through goroutines and channels.

3.Efficiency: compiles to machine code, GC manages memory automatically.

4.Scalability: handle large no of connections to perform concurrent operations without sacrificing performance.

5.Strong community and ecosystem : standard library provides robust functionality for common tasks.

6.Cross-platform development

  1. Googles backing

Goroutines

A goroutine is a function or method that can be executed concurrently with other goroutines.

Goroutines are lightweight threads that allow concurrent execution of functions.

Channels

Channels facilitate communication and synchronization between goroutines.

They are used to send and receive values between goroutines.

Types channels:

  1. Buffered channel
  2. Unbuffered channel

Buffering:

Buffered channels have a specified buffer size, allowing them to hold multiple values before blocking the sender.

Unbuffered channels have a capacity of 0 and can only hold a single value at a time.

Blocking Behavior:

In an unbuffered channel, both the sender and receiver block until they can complete the communication, ensuring synchronization between the goroutines.

In a buffered channel, the sender will only block if the buffer is full, and the receiver will block if the buffer is empty.

Synchronous vs. Asynchronous:

Unbuffered channels provide synchronous communication, meaning both the sender and receiver must be ready at the same time for the communication to proceed.

Buffered channels provide asynchronous communication, allowing the sender and receiver to operate independently as long as the buffer is not full or empty.

Level of Coupling:

Unbuffered channels tightly couple the sender and receiver, requiring them to be available simultaneously.

Buffered channels allow for looser coupling, as they can store values in the buffer for later retrieval, allowing the sender and receiver to operate at different speeds.

Wait Groups

WaitGroup is a synchronization primitive that allows you to wait for a collection of goroutines to complete their execution before continuing.

It provides a way to coordinate and wait for the completion of multiple goroutines.

Mutexes

A mutex is a type of synchronization mechanism that allows multiple goroutines to access shared data concurrently

Interfaces

In go, an interface is a collection of method signatures that a type can implement.

An interface specifies a set of behaviors or actions that a type can perform,Without specifying how those behaviors are implemented.

This allows different types to implement the same interface in their own way.

Interfaces used for :

1.Where Polymorphism is needed

2.Mock object in testing

3.To achieve better abstraction

4.Code decoupling

5.Interoperability

Slices

In Go, a slice is a flexible and dynamic data structure that provides a more powerful and convenient way to work with collections of elements compared to arrays.

A slice is built on top of an underlying array and represents a variable-length sequence of elements of the same type.

Slices are reference types

A slice has two properties: length and capacity.

The length of a slice represents the number of elements it currently holds.

The capacity of a slice is the maximum number of elements it can hold without reallocation.

Reference types in Go

  • Slices
  • Maps
  • Channels
  • Pointers

Nil

In Go, nil is a special value that represents the absence of a value or a zero value for certain types.

It is commonly used to indicate that a pointer, slice, map, channel, or interface does not currently refer to any underlying value or object.

Pointers: A nil pointer does not point to any memory address.

It is the zero value for pointer types and is represented as nil.

Slices, Maps, and Channels: A nil slice, map, or channel represents an uninitialized or empty value of that type.

Interfaces: An interface value that is nil does not hold any underlying value or type.

Maps

In Go, a map is a built-in data structure that represents an unordered collection of key-value pairs.

make function is used to create an empty map with an initial capacity, while a map literal allows you to initialize a map with key-value pairs directly.

Struct

In Go, a struct is a composite data type that allows you to group together zero or more values of different types into a single entity.

It is a way to define custom data structures that can represent real-world objects or concepts.

Structs are value types in Go, which means that when you assign a struct to a new variable or pass it as a function argument,

a copy of the entire struct is made.

Modifications made to the copied struct will not affect the original struct.

Differentiate between new() function and make() function

new: The new built-in function allocates memory but does not initialize it .

make: The make built-in function allocates and initializes an object of type slice, map, or chan (only).

Differentiate between Byte and Rune

A byte in Go is an unsigned 8-bit integer uint8

rune, which has type int32

Embedded structs

embedded struct is a way to include a struct type as a field within another struct.

This allows the outer struct to inherit the fields and methods of the embedded struct, providing a form of composition and code reuse.

Struct composition

Struct composition refers to the practice of creating a new struct type by combining or embedding existing struct types as fields.

It allows you to build more complex data structures by composing multiple smaller structures together.

function closures

A closure is a function that references variables outside its own function body (scope).

Methods

Method is a function that is associated with a specific type.

It enables you to define behavior and functionality for a particular type by attaching functions to it.

Pass by Value

When a variable is passed by value, a copy of its value is made and assigned to a new variable within the function.

Any changes made to the new variable will not affect the original variable outside the function.

Pass by Reference

When a variable is passed by reference, the memory address of the variable is passed to the function. This means that changes made to the variable within the function will affect the original variable outside the function.

In Go, all variables are passed by value by default. However, you can achieve pass-by-reference behavior by using pointers.