Go's Common Data Structures
Posted on October 11, 2024
|
1. Strings Strings in Go are immutable sequences of bytes, typically used to represent UTF-8 encoded text.
Structure type stringStruct struct { str unsafe.Pointer len int } Memory Layout stringStruct +----------------+ | str (uintptr) | ---> [byte array] | len (int) | +----------------+ Detailed Implementation Creation When a string literal is used, the compiler allocates the bytes in read-only memory. Runtime string creation (e.g., string concatenation) allocates new memory and copies bytes.
[Read More]Gin
Posted on October 5, 2024
|
What is Gin Web Framework? Gin is a lightweight, high-performance web framework for Go, inspired by Martini but with a focus on speed and efficiency.
Key Features of Gin Gin is packed with features that make web development in Go both enjoyable and efficient. Here are some of its standout features:
1. High Performance Gin is designed for speed. It outperforms many other Go frameworks by optimizing routing and minimizing middleware overhead, ensuring rapid request processing.
[Read More]Go's `http.ListenAndServe`
Posted on October 4, 2024
|
Implementation Overview The ListenAndServe function is part of Go’s standard library in the net/http package. Its primary purpose is to start an HTTP server that listens on a specified address and handles incoming requests.
func ListenAndServe(addr string, handler Handler) error { server := &Server{Addr: addr, Handler: handler} return server.ListenAndServe() } This function creates a Server instance and calls its ListenAndServe method.
Detailed Implementation The core logic is in the Server.ListenAndServe and Server.
[Read More]Go Memory Allocation
Posted on October 2, 2024
|
How does Go handel memory allocation Memory Allocator Overview The Go memory allocator uses a hierarchical structure:
Heap: The main memory area where dynamically allocated objects reside. Spans: Large blocks of memory (usually 8KB) used to allocate objects. Objects: Individual allocated pieces of memory. graph TD Heap --> Span1[Span 1] Heap --> Span2[Span 2] Heap --> Span3[Span 3] Span1 --> Obj1[Object 1] Span1 --> Obj2[Object 2] Span2 --> Obj3[Object 3] Span2 --> Obj4[Object 4] Span3 --> Obj5[Object 5] Size Classes Go uses size classes to group objects of similar sizes.
[Read More]Go's GMP Model
Posted on October 1, 2024
|
What is the GPM model in Go? GMP Model The GMP model is the cornerstone of Go’s runtime scheduler.
G (Goroutine):
Represents a goroutine, which is a lightweight thread of execution. Contains the stack, the instruction pointer, and other information important for scheduling. Many Gs can exist at the same time. P (Processor):
Represents a logical processor, which can be thought of as a context for scheduling. Acts as a local scheduler, managing a queue of runnable goroutines.
[Read More]Golang Interview
Posted on September 14, 2024
|
How does Go handle dependencies? Go uses a module system for dependency management. The go.mod file specifies the module’s dependencies and their versions. The go get command is used to download and install dependencies.
What is the difference between go run and go build ? Feature go run go build Purpose Compile and run in one step Compile to a permanent executable Output Temporary executable (deleted) Permanent executable on disk Use Case Quick testing of small programs Building applications for deployment Performance Slower due to temporary compilation Faster execution of compiled binary Debugging Limited debugging capabilities Supports debugging and profiling Configuration Options None Various options for customization Use go run for quick tests and development.
[Read More]Go Pprof
Posted on September 12, 2024
|
Overview of Pprof pprof is a tool that comes with Go’s standard library and is used for collecting and viewing profiling data. It can collect different types of profiles including:
CPU Profile: Measures where the program spends most of its time. Memory Profile: Measures the amount of memory allocated and retained. Block Profile: Measures where the program spends time waiting for synchronization primitives. Mutex Profile: Measures contention on mutexes. Setting Up pprof To use pprof, you need to import the net/http/pprof package and set up HTTP server to serve the profiling data.
[Read More]Go Garbage Collection
Posted on September 7, 2024
|
How does garbage collection work in Go? Garbage Collection Go uses a concurrent, tri-color mark-and-sweep garbage collector with write barriers.
Garbage Collection Phases Mark Setup: Preparation for marking phase. Marking: Identifying live objects. Mark Termination: Completion of marking phase. Sweep: Reclaiming memory from dead objects. graph LR A[Mark Setup] --> B[Marking] B --> C[Mark Termination] C --> D[Sweep] D --> A Tri-Color Algorithm Objects are divided into three sets:
White: Potentially garbage objects.
[Read More]Go's Sync
Posted on September 7, 2024
|
sync.Mutex // Mutex provides mutual exclusion for shared resources. type Mutex struct { state int32 // State of the mutex (locked/unlocked) sema uint32 // Semaphore for blocking goroutines } // Lock acquires the mutex, blocking if necessary. func (m *Mutex) Lock() { // Check if already locked; if so, block until unlocked. } // Unlock releases the mutex. func (m *Mutex) Unlock() { // Update state and unblock waiting goroutines if any.
[Read More]