July 15, 2024
Basic Golang Interview Questions Part 1 # What are the pros and cons of using Golang? Pros: Efficient Concurrency: Goroutines and channels make it easy to write concurrent programs, leveraging multi-core processors efficiently. Goroutines: Lightweight threads managed by the Go runtime, making concurrent programming easier and more efficient. Channels: Facilitate safe communication between goroutines, promoting clean concurrency patterns. GC: Automatic memory management reduces the risk of memory leaks and manual memory management errors.
...
July 10, 2024
Variadic Parameters and Argument Unpacking in Go # In Go, variadic parameters and argument unpacking/expanding involve some specific behind-the-scenes mechanisms that allow for flexible and dynamic function calls. Here’s a detailed explanation of how these mechanisms work:
Variadic Parameters # A variadic parameter allows a function to accept an arbitrary number of arguments. In Go, this is denoted by an ellipsis (...) followed by the type. For example:
func sum(numbers .
...
July 9, 2024
Understanding Closure Functions in Go # In Go, closure functions are powerful constructs that allow functions to capture and remember the variables from their surrounding scope even after that scope has exited. This capability is essential for creating more dynamic, flexible, and maintainable code. Let’s delve into closure functions with a simple example, understand why variables like sum are escaped to the heap, and explore their use cases in the standard library and web API development.
...
July 9, 2024
Go Empty Struct # Introduction # An empty struct has zero memory allocation, same memory addresses for multiple instances, and stateless.
Zero Memory Allocation # Empty structs do not occupy memory, making them useful for memory optimization:
package main import ( "fmt" "unsafe" ) func main() { var e struct{} fmt.Println(unsafe.Sizeof(e)) // Output: 0 } Same Memory Addresses # Multiple empty structs share the same address:
package main import "fmt" func main() { var e, e2 struct{} fmt.
...
July 9, 2024
Difference between new and make in Golang # In Go, make and new are built-in functions used for memory allocation.
make not only allocates memory but also initializes the data, which is necessary because the data types supported by make (like slices, maps, and channels) require initialization before use. On the other hand, new allocates memory space that is filled with zero values. new allocates a single block of memory, while make may allocate multiple blocks of memory.
...
May 28, 2024
formatting verbs # %s vs %q? Answer: %s prints the string without enclosing it in quotes. %q prints the string enclosed in double quotes, %q can print string, byte slice, runes and int. %v vs %+v vs %#v? Answer: %v: Default format, prints the value as is. For structs, it prints only the field values. %+v: Detailed format, includes field names in structs. %#v: Go-syntax representation, includes type information and can be used to recreate the value in Go code.
...
May 28, 2024
How to Implement Set Using hMap in Golang # 1. What is a Set? How is Set Different from HashMap? # A set is a data structure that stores a collection of unique elements. Sets do not allow duplicate values and do not maintain any particular order of the elements.
A hashmap is a data structure that stores key-value pairs. Each key in a hashmap is unique, and it maps to a specific value.
...