<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bhaumik Maan on Medium]]></title>
        <description><![CDATA[Stories by Bhaumik Maan on Medium]]></description>
        <link>https://medium.com/@bhaumikmaan?source=rss-b1e3116982ce------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZZmpd0CR8vK0e8c7s0GRhw.jpeg</url>
            <title>Stories by Bhaumik Maan on Medium</title>
            <link>https://medium.com/@bhaumikmaan?source=rss-b1e3116982ce------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 18:51:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bhaumikmaan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Go & Kafka: From Theory to Practice]]></title>
            <link>https://medium.com/@bhaumikmaan/go-kafka-from-theory-to-practice-96e0757e02ca?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/96e0757e02ca</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Wed, 11 Jun 2025 05:15:43 GMT</pubDate>
            <atom:updated>2025-06-11T05:15:43.165Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qaHrbcroeCsdN1YdhzUB2Q.png" /></figure><p>Welcome back, fellow developers and stream enthusiasts! If you’ve been following my recent series, you’ve already laid a strong foundation in the world of Go programming along with a comprehensive understanding of Apache Kafka’s distributed messaging power. The implementation for the same can be found on <a href="https://github.com/bhaumikmaan/go-kafka-application"><strong>Github</strong></a>!</p><blockquote>If not check out the series here to get an insight on the basics of both<br>GoLang Blogs: <a href="https://medium.com/@bhaumikmaan/list/golang-92f51617836c">Here</a><br>Kafka Blogs: <a href="https://medium.com/@bhaumikmaan/list/kafka-549d42bfaa31">Here</a></blockquote><p>Now, it’s time to bridge the gap between theoretical knowledge and practical implementation. In this blog post, we’re going to roll up our sleeves and build a hands-on Go application that integrates directly with Kafka. Our goal is to create a simple yet powerful Command-Line Interface (CLI) tool that demonstrates core Kafka operations: listing topics, producing messages to a Kafka topic, and consuming messages from it.</p><p>To make things even more exciting, our producer won’t just generate dummy data; it will connect to a real-time, fascinating data source: the <a href="https://stream.wikimedia.org/v2/stream/recentchange"><strong>Wikimedia recent changes stream</strong></a>. This means our Kafka topic will be populated with live events from Wikipedia and its sister projects worldwide!</p><p>For our Kafka integration in Go, we’ll be using the <a href="https://pkg.go.dev/github.com/segmentio/kafka-go">[segmentio/kafka-go]</a> library. It&#39;s a robust, pure Go client that directly speaks the Kafka protocol, making it a popular choice for building high-performance Go applications.</p><p>Let’s dive in and transform our theoretical knowledge into a tangible, working application!</p><h3>The Application’s Command Center</h3><p>main.go is our application&#39;s central hub, orchestrating all Kafka operations. It parses command-line inputs and dispatches tasks to specialized packages.</p><h4>CLI Input and Command Dispatch</h4><p>When you run the application, main.go checks for arguments using os.Args.</p><ul><li><strong>No arguments?</strong> It displays a helpful menu() listing all available commands.</li><li><strong>Arguments present?</strong> The first argument defines the action (e.g., list-topics, start-producer). A switchstatement then directs execution to the relevant function.</li></ul><p>Here’s how it handles common commands:</p><ul><li><strong>list-topics</strong>: Calls producer.PrintTopics() to display available Kafka topics.</li><li><strong>start-producer &lt;topic_name&gt;</strong>: Initiates the producer for the specified topic. It validates that a topic name is provided before calling producer.ProduceMessages().</li><li><strong>start-consumer &lt;topic_name&gt;</strong>: Starts a consumer for a given topic. It also validates the topic name before invoking consumer.ConsumeMessages().</li><li><strong>start-consumer-group &lt;topic&gt; &lt;group-id&gt;</strong>: A more advanced consumer mode. This requires both a topic name and a consumer group ID, which are validated before calling consumer.ConsumeWithGroup().</li><li><strong>Unknown actions</strong>: Fallback to the menu() with an error message.</li></ul><h4>Modular Design with Package Imports</h4><p>The import statements at the top of main.go are key:</p><pre>import (<br>    &quot;fmt&quot;<br>    &quot;go-kafka-application/consumer&quot; // Imports consumer logic<br>    &quot;go-kafka-application/producer&quot; // Imports producer logic<br>    &quot;os&quot;<br>)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*We6uG591PbFBp_57.png" /><figcaption>Imports in Go</figcaption></figure><p>By importing go-kafka-application/consumer and go-kafka-application/producer, main.go gains access to their <strong>exported functions</strong> (those starting with a capital letter, like producer.PrintTopics). This modular approach keeps main.go lean and focused on dispatching, while delegating the specific Kafka operations to their respective, specialized packages.</p><h4>Dependency Management</h4><ul><li>Our project’s dependencies are meticulously managed by the go.mod file, which is the cornerstone of Go Modules. The line module go-kafka-application defines our project&#39;s module path, essentially its unique identifier. go 1.24.3indicates that this project is built using Go version 1.24.3 or newer, ensuring compatibility.</li><li>The require block lists all external packages our application depends on. Crucially, github.com/segmentio/kafka-go v0.4.48 is our direct dependency for interacting with Kafka.</li><li>The other entries, marked with // indirect, are transitive dependencies—packages that kafka-go itself relies on, ensuring all necessary components for our build are accounted for.</li><li>This file guarantees that our application&#39;s dependencies are consistent and builds are reproducible across different environments.</li></ul><h3>The Kafka Producer — Ingesting Real-time Data</h3><p>Our producer package is the engine that collects data from an external source and pushes it into our Kafka cluster. It&#39;s designed to be robust in handling an incoming data stream and efficiently sending it downstream to Kafka topics.</p><h4>Understanding the Incoming Data: The Event Structure</h4><p>Before we can send data, we need to understand its shape. The Wikimedia recent changes stream provides events in JSON format. To work with this data effectively in Go, we define an Event struct:</p><ul><li><strong>Mapping JSON to Go Structs</strong>: The Event struct is a direct representation of the JSON payload we receive from the Wikimedia stream. Each field in the struct corresponds to a key in the JSON object. The json:&quot;field_name&quot; tags are essential here; they tell Go&#39;s encoding/json package how to correctly map JSON keys (which often use snake_case or different casing) to our Go struct&#39;s PascalCase field names.</li><li><strong>Nested </strong><strong>Meta Struct</strong>: The stream&#39;s JSON includes a nested meta object containing metadata like URI, request ID, domain, etc. We mirror this structure with a nested Meta struct within our Event struct. This approach preserves the original data&#39;s hierarchy, making access to specific metadata (e.g., event.Meta.Domain) clear and type-safe.</li></ul><h4>Discovering Topics: PrintTopics()</h4><p>This utility function provides a way for our CLI tool to inspect the Kafka cluster and list all available topics. <a href="https://github.com/bhaumikmaan/go-kafka-application/blob/9cb555bb655a81284ea70d386d775a387af4fa7a/producer/main.go#L54-L79">Refer Here</a></p><ul><li><strong>Direct Kafka Connection</strong>: We use kafka.Dial(&quot;tcp&quot;, &quot;localhost:9092&quot;) to establish a direct TCP connection to our Kafka broker. This demonstrates segmentio/kafka-go&#39;s philosophy of being a pure Go client, communicating directly via the Kafka protocol without external C dependencies. This simplifies deployment and management.</li><li><strong>Reading Partition Metadata</strong>: Kafka organizes data into topics, which are further divided into partitions. To get a complete list of topics, we read all partitions from the connection (conn.ReadPartitions()). Since multiple partitions belong to the same topic, we collect unique topic names by storing them as keys in a Go map, effectively acting as a set. This ensures we only print each topic name once.</li></ul><h4>The Stream Reader: readStream() – Embracing Concurrency</h4><p>This is where we connect to the Wikimedia stream and prepare its data for Kafka. This function is a prime example of Go’s powerful concurrency features. <a href="https://github.com/bhaumikmaan/go-kafka-application/blob/9cb555bb655a81284ea70d386d775a387af4fa7a/producer/main.go#L82-L141">Refer Here</a></p><ul><li><strong>Connecting to the SSE Stream</strong>: We initiate an HTTP GET request to https://stream.wikimedia.org/v2/stream/recentchange. This URL provides a Server-Sent Events (SSE) stream, meaning it&#39;s a long-lived HTTP connection over which the server continuously pushes new &quot;events&quot; (Wikimedia changes) to our client.</li><li><strong>Efficient Line-by-Line Reading</strong>: The bufio.NewScanner(resp.Body) is used to read the incoming HTTP response body line by line. This is particularly efficient for continuous streams, as it buffers data and processes it incrementally, preventing large memory consumption for potentially infinite streams.</li><li><strong>The Power of Goroutines and Channels</strong>: This is the most critical design choice here:<br>1. <strong>Asynchronous Processing with </strong><strong>go func() {}</strong>: Reading from a live network stream is a blocking operation – it waits for data to arrive. If readStream ran in the main execution flow of our producer, it would halt everything else. By launching this stream reading logic within a separate goroutine (go func() {}), built as a <strong>function literal</strong>, we allow it to run concurrently in the background. It fetches data without blocking the ProduceMessagesfunction.<br>2. <strong>Safe Communication with </strong><strong>chan Event</strong>: The changesChannel is a Go channel that acts as a safe, synchronized conduit between the readStream goroutine (the sender) and the ProduceMessages function (the receiver). Goroutines communicate by sending and receiving values on channels, which inherently handles locking and synchronization.</li><li>This elegant pattern decouples the stream reading process from the stream processing (producing to Kafka), improving responsiveness and preventing race conditions. Once the stream ends or an error occurs, the channel is close()d, signaling to the receiver that no more events will be sent.</li><li><strong>Parsing the Stream Data</strong>: The Wikimedia SSE stream prefixes each data line with “data: “. We use strings.TrimPrefix to remove this protocol overhead, obtaining the raw JSON payload. json.Unmarshal then converts this JSON string into our Go Event struct for structured access.</li></ul><h4>Sending to Kafka: ProduceMessages()</h4><p>This is the core function where processed Wikimedia events are transformed into Kafka messages and sent to a topic. <a href="https://github.com/bhaumikmaan/go-kafka-application/blob/9cb555bb655a81284ea70d386d775a387af4fa7a/producer/main.go#L143-L186">Refer Here</a></p><ul><li><strong>Connecting to a Specific Partition</strong>: Instead of using the higher-level kafka.NewWriter (which automatically handles load balancing across partitions and retries), this example uses kafka.DialLeader(context.Background(), &quot;tcp&quot;, &quot;localhost:9092&quot;, topic, 0). This connects directly to the leader of a specific partition (partition 0 in this case). While simpler for this demonstration, kafka.NewWriter is generally recommended for production producers as it offers more robust abstractions for resilience and performance (e.g., automatic retries, batching, and load distribution).</li><li><strong>Setting a Write Deadline</strong>: conn.SetWriteDeadline(time.Now().Add(10 * time.Second)) is a critical defensive programming technique. Network operations can hang if the Kafka broker is unresponsive. This deadline ensures that our WriteMessages call won&#39;t block indefinitely. If the write doesn&#39;t complete within 10 seconds, it will time out, allowing our application to handle the error, rather than becoming stuck.</li><li><strong>Consuming from the Channel</strong>: The for event := range changesChannel loop is a Go idiom that continuously reads events from the channel until the readStream goroutine closes it. This ensures that every event from the Wikimedia stream is processed and sent to Kafka.</li><li><strong>Constructing Kafka Messages</strong>: Each Event from the stream is transformed into a kafka.Message.<br>1. <strong>Message </strong><strong>Value</strong>: This is the actual payload of the Kafka message. We format relevant fields from our Eventstruct into a human-readable string and convert it to a byte slice, as Kafka messages are byte arrays.<br>2. <strong>Message </strong><strong>Key</strong>: While not explicitly set in the provided snippet (kafka.Message{Value: []byte(messageValue)}), for robust Kafka producers, it&#39;s highly recommended to set a Key (e.g., using event.Title). The Key is used by Kafka for partitioning: all messages with the same key are guaranteed to go to the same partition. This is crucial for maintaining message order for related events, which is often a requirement in stream processing.</li><li><strong>Sending Messages</strong>: conn.WriteMessages() is called to send the prepared Kafka message to the broker. Error handling here is important: if a message fails, we log it and continue to the next message, implying that transient failures for individual messages should not halt the entire production process. Unrecoverable errors might lead to a log.Fatal call, exiting the producer.</li></ul><p>All of these operations performed by the Go Application can be done by Kafka CLI commands as shared in the Kafka Blogs or within the codebase above the functions</p><h3>The Kafka Consumer — Processing Streamed Data</h3><p>The consumer package in our application is responsible for reading messages from Kafka topics. We&#39;ll explore two distinct approaches to consuming messages, each with its own use case and implications for scalability and reliability.</p><h4>1. Low-Level Consumption: ConsumeMessages()</h4><p>This function demonstrates a basic, single-partition consumer. <a href="https://github.com/bhaumikmaan/go-kafka-application/blob/9cb555bb655a81284ea70d386d775a387af4fa7a/consumer/main.go#L17-L51">Refer Here</a></p><ul><li><strong>Direct Partition Connection</strong>: We use kafka.DialLeader to connect directly to a <strong>specific Kafka partition&#39;s leader</strong> (partition 0). This offers granular control but means this consumer operates in isolation—it doesn&#39;t participate in Kafka&#39;s consumer group coordination, leading to duplicate reads if multiple instances run, and lacks automatic load balancing or fault tolerance.</li><li><strong>Preventing Indefinite Blocking</strong>: conn.SetReadDeadline(time.Now().Add(60 * time.Second)) is crucial. It sets a timeout, preventing the consumer from hanging indefinitely if no new messages arrive, ensuring the loop can continue or handle errors.</li><li><strong>Efficient Batch Reading</strong>: conn.ReadBatch(10, 1e6) (in bytes) fetches messages in chunks. This <strong>batching</strong> significantly improves efficiency by reducing network overhead. We then iterate through this fetched batch to process individual messages.</li><li><strong>Resource Management</strong>: It’s important to defer conn.Close() for the connection and batch.Close() after processing to release resources cleanly.</li></ul><p>While ConsumeMessages provides direct control for specific needs, it&#39;s generally not suited for scalable production systems due to its lack of consumer group integration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UR1Vg9Z00MT4ssNA" /></figure><h4>2. Scalable Consumption: ConsumeWithGroup() – Embracing Consumer Groups</h4><p>This function showcases the standard, robust way to consume Kafka messages, leveraging <strong>Kafka Consumer Groups</strong>. <a href="https://github.com/bhaumikmaan/go-kafka-application/blob/9cb555bb655a81284ea70d386d775a387af4fa7a/consumer/main.go#L53-L79">Refer Here</a></p><ul><li><strong>Higher-Level Abstraction</strong>: We use kafka.NewReader(kafka.ReaderConfig{...}). This is the <strong>recommended client</strong> for production consumers. It abstracts away much of the complexity, handling broker discovery, offset management, and crucial consumer group coordination automatically.</li><li><strong>The Power of </strong><strong>GroupID</strong>: The GroupID parameter is fundamental here. All consumers using the same GroupID form a <strong>consumer group</strong>. Kafka ensures that messages from topic partitions are distributed among consumers within this group.<br>1. <strong>Scalability</strong>: Messages are processed in parallel across consumer instances.<br>2. <strong>Fault Tolerance</strong>: If a consumer in the group fails, Kafka automatically <strong>rebalances</strong> its assigned partitions to other active members, ensuring continuous processing and preventing data loss or duplication.</li><li><strong>Simplified Message Reading</strong>: r.ReadMessage(context.Background()) simply blocks until a message is available from any of its assigned partitions. The NewReader handles offset commits automatically, ensuring your progress is saved and messages aren&#39;t re-read after restarts.</li><li><strong>Graceful Shutdown</strong>: defer r.Close() ensures a clean disconnection and resource release when the consumer shuts down.</li></ul><p>In essence, ConsumeWithGroup with kafka.NewReader and a GroupID is the go-to pattern for building resilient, scalable, and distributed Kafka consumer applications in Go. It offloads complex coordination to the library and Kafka, allowing you to focus on message processing.</p><h3>Ensuring Reliability — Testing Our Kafka Application</h3><p>Building applications with external dependencies like Kafka demands robust testing. In Go, we distinguish between <strong>unit tests</strong> (which isolate and test small pieces of code) and <strong>integration tests</strong> (which verify how different parts of your system, including external services like Kafka, work together). Our application includes examples of both.</p><h4>Unit Testing: Isolating Logic with a Fake Reader</h4><p>Unit tests are fast and don’t require external services to run. They focus on verifying the correctness of individual functions or components.</p><ul><li><strong>TestConsumeWithFakeReader (in </strong><strong>consumer_test.go)</strong> This test exemplifies unit testing for our consumer logic. Instead of connecting to a real Kafka broker, we use a <strong>fakeReader</strong>.</li><li>This fakeReader is a custom Go struct that implements just enough of the kafka.Reader interface to simulate its behavior. It provides predefined kafka.Messages from an in-memory slice.</li><li><strong>Why use a </strong><strong>fakeReader?</strong> It allows us to test the ConsumeMessages function&#39;s logic in isolation. We can verify how our consumer processes messages, handles errors, or logs output, all without the overhead or unreliability of a live Kafka connection. This makes tests quick to run and ensures their results are consistent.</li></ul><h4>Integration Testing: Verifying End-to-End Flow</h4><p>Integration tests are crucial for applications that interact with external systems. They confirm that your code communicates correctly with services like Kafka. These tests <em>do</em> require a running Kafka broker.</p><ul><li><strong>TestProduceMessagesIntegration (in </strong><strong>producer_test.go)</strong> This test verifies that our producer can successfully send messages to a real Kafka topic. It runs ProduceMessages in a separate goroutine for a short duration, allowing it to connect to the Wikimedia stream and start pushing data to Kafka. Then, it uses a kafka.NewReader to connect to the <em>same Kafka topic</em> and attempts to read a message.<br>-&gt; <strong>Why an integration test?</strong> It’s the only way to confirm that the readStream function correctly parses data and that ProduceMessages effectively writes that data into Kafka. If a message is successfully read back, we know the entire production pipeline is working.<br>-&gt; <strong>Prerequisites</strong>: For this test to pass, a Kafka instance must be running, and the specified topic (test-stream-golang) must exist on that broker.</li><li><strong>TestKafkaIntegration (in </strong><strong>consumer_test.go)</strong> This is another essential integration test that verifies the full Kafka write-read cycle. It explicitly creates a kafka.Writer to send a known message to a test topic, then immediately creates a kafka.NewReader to consume that very message.<br>-&gt; <strong>Why this specific flow?</strong> It’s a quick, self-contained check that Kafka is reachable, writable, and readable from our application’s perspective. It confirms the fundamental communication channels are open and functioning.<br>-&gt; <strong>Context with Timeout (</strong><strong>context.WithTimeout)</strong>: Notice the use of context.WithTimeout for both reading and writing. This is a standard Go practice to ensure network operations don&#39;t hang indefinitely. If Kafka is unresponsive, the test will fail gracefully with a timeout error rather than getting stuck.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/0*e-m5ipVY1yZLv_Ga.png" /><figcaption>Testing in Go</figcaption></figure><p>Both unit and integration tests play vital roles. Unit tests ensure the internal logic is sound, while integration tests validate that our application correctly interacts with the external world. Together, they provide confidence in the reliability and correctness of our Go Kafka application.</p><p>We’ve now taken our theoretical knowledge of Go and Kafka and built a tangible, real-world application. You’ve seen firsthand how to craft a Go CLI tool to interact with Kafka, pulling live data from the Wikimedia stream. We leveraged Go’s powerful <strong>goroutines</strong> and <strong>channels</strong> for efficient asynchronous stream processing and explored segmentio/kafka-gofor both basic direct Kafka interactions and the scalable power of <strong>consumer groups</strong>. This hands-on experience should solidify your understanding of how producers, consumers, and consumer groups work together in a distributed messaging system.</p><p>This application provides a robust foundation. Remember to prioritize strong <strong>error handling</strong>, externalize your configurations, and always test diligently — both with unit tests for isolated logic and integration tests for end-to-end flow with Kafka. As you continue your journey, dive into advanced topics like schema registries or Kafka Streams to build even more sophisticated real-time solutions. Keep experimenting, keep building, and happy streaming!</p><p>Do check out my other blogs and follow me on Github for the latest on tech.</p><p>Find The Blog Series Here:</p><ol><li>GoLang Series — <a href="https://medium.com/@bhaumikmaan/list/golang-92f51617836c">Here</a></li><li>Kafka Series — <a href="https://medium.com/@bhaumikmaan/list/kafka-549d42bfaa31">Here</a></li></ol><p>Medium: <a href="https://medium.com/@bhaumikmaan">Bhaumik Maan</a> || Github: <a href="https://github.com/bhaumikmaan">bhaumikmaan</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96e0757e02ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GoLang Advanced 103: Deep Dive into Go’s Power Features]]></title>
            <link>https://medium.com/@bhaumikmaan/golang-advanced-103-deep-dive-into-gos-power-features-2e75788e9e68?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e75788e9e68</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Sat, 07 Jun 2025 07:32:19 GMT</pubDate>
            <atom:updated>2025-06-07T07:32:19.166Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MRHBPULzQCQw6EBa" /></figure><p>Welcome back! In <a href="https://www.google.com/search?q=YOUR_BLOG_1_LINK_HERE">GoLang Fundamentals 101</a>, we built our foundation with Go’s basics, from setup to arrays and slices. Then, in <a href="https://www.google.com/search?q=YOUR_BLOG_2_LINK_HERE">GoLang Fundamentals 102</a>, we dove into custom types, structs, maps, file handling, and testing.</p><p>Now, we’re ready to unlock the powerful features that truly differentiate Go. This post will cover:</p><ul><li><strong>Interfaces:</strong> Go’s unique approach to flexible code design.</li><li><strong>Pointers:</strong> Understanding memory and direct data manipulation.</li><li><strong>Goroutines and Channels:</strong> The core of Go’s famous <strong>concurrency</strong> model.</li><li><strong>HTTP and Web Services:</strong> Building robust web backends.</li></ul><p>By the end, you’ll grasp Go’s advanced capabilities and be ready to build high-performance applications. Let’s unlock Go’s true potential!</p><blockquote>Course Followed: <a href="https://thoughtworks.udemy.com/course/go-the-complete-developers-guide/">Udemy</a><br><a href="https://github.com/bhaumikmaan/Understanding-GoLang">Github Repository: Implementation of Course Examples</a><br>Part 1 of the blog series — <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-101-1cc420f7747a">GoLang Fundamentals 101</a><br>Part 2 of the blog series — <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-102-beyond-the-basics-7f4161f75daf">GoLang Fundamentals 102: Beyond the Basics</a></blockquote><h3>Pointers: Direct Memory Manipulation and Go’s Pass-by-Value Nuance</h3><p>Go is fundamentally a <strong>pass-by-value</strong> language. This means that when you pass an argument to a function, a new copy of that argument’s value is created and given to the function. Any modifications made to this copied value inside the function will not affect the original variable outside of it. While this behavior simplifies reasoning about code, it poses a challenge when you <em>do</em> need a function to modify the original data. This is where <strong>pointers</strong> come in.</p><h4>The Role of Pointers</h4><p>A pointer is a variable that stores the memory address of another variable. By working with a variable’s memory address, a function can directly access and modify the original data, bypassing the “pass-by-value” copying mechanism.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/0*mfk84eCqzXpfc-ku.png" /><figcaption>Pointers in Go</figcaption></figure><p>Go uses two primary operators when dealing with pointers:</p><ul><li><strong>&amp; (Address-of Operator)</strong>: When placed before a variable name, &amp; returns the memory address where that variable&#39;s value is stored. This result is a pointer.</li></ul><pre>name := &quot;Alice&quot;<br>namePointer := &amp;name // namePointer now holds the memory address of &#39;name&#39;<br>fmt.Println(namePointer) // Output: A memory address like 0xc000010210</pre><ul><li><strong>* (Dereference Operator)</strong>: When placed before a pointer variable, * gives you the value stored at the memory address the pointer points to.</li></ul><pre>fmt.Println(*namePointer) <br>// Output: Alice (the value at the address namePointer holds)</pre><h4>⭐️ * as a Type vs. * as an Operator ⭐️</h4><p>This can be a source of confusion for newcomers:</p><ul><li>When * appears <strong>behind a </strong><strong>type</strong> (e.g., *person, *int), it&#39;s part of a <strong>type description</strong>. It signifies that the variable being declared is a pointer to a value of that specific type.</li></ul><pre>var ptrToAge *int        // Declares a variable &#39;ptrToAge&#39; that can hold a pointer to an integer<br>func (p *Person) update() // &#39;p&#39; is a receiver that is a pointer to a Person struct</pre><ul><li>When * appears <strong>behind an actual </strong><strong>pointer variable</strong> (e.g., *pointerToPerson), it acts as an <strong>operator</strong>. It&#39;s used to &quot;dereference&quot; the pointer, allowing you to access or modify the value at the memory address it points to.</li></ul><pre>// Assigns the value 30 to the integer located at the address ptrToAge holds<br>(*ptrToAge) = 30         <br>// Modifies the firstName field of the struct pointed to by pointerToPerson<br>(*pointerToPerson).firstName = name</pre><h4>Pointers with Structs: The Convenience Shortcut</h4><p>Pointers are particularly common when working with structs, especially when methods need to modify the struct instance they are called on. Go provides a convenient shortcut for this:</p><pre>type Person struct {<br>    FirstName string<br>    LastName  string<br>}<br><br>// updateName uses a pointer receiver (*Person) because it modifies the struct&#39;s field<br>func (pointerToPerson *Person) updateName(newFirstName string) {<br>    // Go automatically dereferences the pointer here, so no need for (*pointerToPerson).FirstName<br>    pointerToPerson.FirstName = newFirstName<br>}<br><br>func main() {<br>    jim := Person{FirstName: &quot;Jim&quot;, LastName: &quot;Party&quot;}<br>    fmt.Println(&quot;Before update:&quot;, jim.FirstName) // Output: Jim<br>    // Even though jim is a value, Go automatically takes its address (&amp;jim)<br>    // and passes it as a pointer to the updateName method.<br>    jim.updateName(&quot;Jimmy&quot;)<br>    fmt.Println(&quot;After update:&quot;, jim.FirstName)  // Output: Jimmy<br>}</pre><p>In this example, jim.updateName(&quot;Jimmy&quot;) looks like a regular method call on a value. However, because updateNamehas a pointer receiver (*Person), Go automatically takes the address of jim and passes a pointer. Inside the method, Go also conveniently allows pointerToPerson.FirstName instead of (*pointerToPerson).FirstName for field access, making pointer usage cleaner.</p><h4>⭐️ The “Gotcha”: Slices and Maps are “Reference-Like” Types ⭐️</h4><p>Despite Go’s “pass-by-value” paradigm, there’s a common point of confusion when it comes to slices and maps (and channels, functions, interfaces). These types are often referred to as “reference types” because modifying their <em>contents</em>inside a function <em>does</em> affect the original, <em>without</em> explicitly passing a pointer to the slice or map variable itself.</p><p>This apparent “anomaly” stems from their underlying implementation:</p><ul><li>A <strong>slice</strong> is a struct that contains three fields: a <strong>pointer</strong> to the underlying array, its length, and its capacity. When you pass a slice to a function, a <em>copy of this slice header struct</em> is made. However, the pointer field * within that copied header still points to the <em>same underlying array</em> as the original slice. Therefore, modifying an element (mySlice[i] = value) directly changes the shared underlying array, and this change is visible to all slices referencing it.<br>- <strong>Crucial distinction:</strong> If you <em>re-slice</em> or append() to a slice in a way that requires a new underlying array to be allocated, the <em>slice header itself</em> changes. To make <em>that</em> change visible outside the function, you must either return the new slice or pass a pointer to the slice variable.</li><li>Similarly, a <strong>map</strong> is implemented as a pointer to an underlying hash table data structure. When a map is passed to a function, a copy of this pointer is made, but both the original and the copied map variable point to the <em>same hash table</em>. Hence, adding, deleting, or updating entries in the map within a function directly modifies the shared hash table, affecting the original map.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GA1kij6Q2ptR41ro.png" /><figcaption>Type segregation in Go</figcaption></figure><p>In summary, for basic types (int, string, bool, etc.) and structs (which are value types), you explicitly need to pass a pointer to modify the original. For slices and maps (which are value types whose &quot;value&quot; is a header containing a pointer to shared underlying data), modifications to their <em>contents</em> are automatically visible without needing to explicitly pass a pointer to the slice or map variable itself. This understanding is key to writing effective and predictable Go code.</p><h3>Interfaces: Go’s Approach to Flexible Design and Polymorphism</h3><p>In many programming languages, you might encounter concepts like abstract classes or explicit interface implementations (e.g., using an implements keyword). Go takes a simpler, more implicit approach to defining behavior contracts through <strong>interfaces</strong>.</p><p>At its core, an <strong>interface</strong> in Go is a type that specifies a <strong>set of method signatures</strong> without providing any implementation details. Think of it as a blueprint or a contract: if a concrete type agrees to fulfill all the methods listed in an interface, it implicitly becomes a part of that interface.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P_nEP3CkeA3pq-HO.png" /><figcaption>Interface in Go</figcaption></figure><h4>Declaring an Interface</h4><p>An interface is declared using the type keyword, followed by the interface&#39;s name, the interface keyword, and a list of method signatures it expects:</p><pre>// Define a &#39;Shape&#39; interface<br>type Shape interface {<br>    Area() float64       // Expects a method named Area that returns a float64<br>    Perimeter() float64  // Expects a method named Perimeter that returns a float64<br>}</pre><p>Here, Shape is an interface that dictates that any type implementing it <em>must</em> have an Area() method returning a float64and a Perimeter() method returning a float64.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SQm0rltOrNGCt-lZ.png" /><figcaption>Declaration of an interface</figcaption></figure><h4>⭐️ Concrete Types and Implicit Implementation ⭐️</h4><p>A <strong>concrete type</strong> is any type from which you can create a value, store data, and manipulate it (e.g., int, string, a structlike Person).</p><p>The magic of Go interfaces lies in their <strong>implicit implementation</strong>. Unlike languages where you explicitly declare that a class implements an interface, in Go:</p><p><strong>Any concrete type that defines all the methods specified in an interface, with matching method signatures (name, parameters, return types), automatically implements that interface.</strong></p><p>There’s no special keyword needed. If your Circle struct has both an Area() and a Perimeter() method, it automatically satisfies the Shape interface:</p><pre>type Circle struct {<br>    Radius float64<br>}<br><br>// Circle implicitly implements the Shape interface<br>func (c Circle) Area() float64 {<br>    return 3.14159 * c.Radius * c.Radius<br>}<br><br>func (c Circle) Perimeter() float64 {<br>    return 2 * 3.14159 * c.Radius<br>}<br><br>func main() {<br>    myCircle := Circle{Radius: 5}<br>    var s Shape = myCircle // A variable of interface type &#39;Shape&#39; can hold a &#39;Circle&#39; value<br>    fmt.Println(&quot;Area:&quot;, s.Area())         // Calls Circle&#39;s Area() method<br>    fmt.Println(&quot;Perimeter:&quot;, s.Perimeter()) // Calls Circle&#39;s Perimeter() method<br>}</pre><h4>Interfaces as Types and Contracts</h4><p>Interfaces are themselves types. A variable declared with an interface type can hold any concrete value that implements that interface. This enables <strong>polymorphism</strong>, allowing you to write functions that operate on an interface type, accepting any concrete type that satisfies its contract.</p><p>This “contract” concept is central: an interface defines <em>what</em> a type can do, not <em>how</em> it does it. This promotes:</p><ul><li><strong>Decoupling:</strong> Code that uses an interface doesn’t need to know the specific concrete types it’s working with. This makes your code more modular and easier to maintain.</li><li><strong>Flexibility:</strong> You can easily introduce new concrete types that satisfy an existing interface without altering the code that uses the interface.</li><li><strong>Testability:</strong> Interfaces make it simple to “mock” or “stub” dependencies in tests by providing test implementations of an interface.</li></ul><p><strong>Important Note:</strong> Go interfaces are <em>not</em> generic types. They define behavior based on method signatures, not parameterized data structures that can hold any type (List&lt;T&gt;). While Go 1.18 introduced generics, interfaces remain crucial for polymorphic behavior.</p><p>Go’s implicit interfaces are a powerful tool for writing clean, scalable, and maintainable code by focusing on behavior over concrete type hierarchies. For a deeper dive, consider these resources:</p><ul><li><a href="https://blog.stackademic.com/interfaces-in-golang-a-deeper-look-4a0931481c00">Interfaces in Golang: A Deeper Look</a></li><li><a href="https://medium.com/codex/go-interfaces-for-new-gophers-3d608db99d46">Go Interfaces for New Gophers</a></li><li><a href="https://medium.com/@jamal.kaksouri/understanding-the-power-of-go-interfaces-a-comprehensive-guide-835954101b7e">Understanding the Power of Go Interfaces: A Comprehensive Guide</a></li><li><a href="https://amit-kumar-manjhi.medium.com/interfaces-in-golang-with-examples-ab5c5b7d34b7">Interfaces in Golang with Examples</a></li><li><a href="https://donofden.com/blog/2019/09/20/golang-interface-goroutine-channels">Interface, Goroutines &amp; Channels in Go</a></li></ul><h3>HTTP Operations: Interacting with the Web Through Interfaces</h3><p>Go is incredibly well-suited for building web services and making HTTP requests. The net/http package is a cornerstone for these tasks. While making a simple GET request might seem straightforward, understanding how Go handles the response body introduces us to the fundamental power of interfaces, particularly io.Reader and io.Writer.</p><h4>Making HTTP Requests and Understanding the Response Body</h4><p>When you make an HTTP GET request using http.Get(), the function returns an *http.Response and an error. The http.Response struct contains details about the server&#39;s reply, including the status code, headers, and critically, the <strong>response body</strong>.</p><p>If you inspect the type of http.Response.Body (as you can see in the <a href="https://pkg.go.dev/net/http#Response">official documentation</a>), you&#39;ll find it&#39;s an io.ReadCloser. This isn&#39;t a concrete data type like a []byte directly; it&#39;s an <strong>interface</strong>. Specifically, io.ReadCloser is itself a combination of two simpler interfaces: io.Reader and io.Closer.</p><p>Why this level of abstraction? It provides immense flexibility. By defining the Body as an io.ReadCloser, Go ensures that the response can be handled consistently, regardless of where the actual data is coming from – whether it&#39;s streamed from a network connection, read from a file, or generated from an in-memory buffer. This abstract Reader interface dictates <em>how</em> data can be read, not <em>what</em> the underlying data source is.</p><h4>⭐️ The io.Reader Interface: Abstracting Data Sources ⭐️</h4><p>The io.Reader interface is fundamental in Go for abstracting any source from which data can be read. It defines a single method:</p><pre>type Reader interface {<br>    Read(p []byte) (n int, err error)<br>}</pre><p>Any type that has a Read method with this exact signature implicitly satisfies the io.Reader interface. When you call Read(p []byte), it attempts to fill the provided byte slice p with data. It returns n, the number of bytes read (which might be less than len(p)), and an err, which indicates if an error occurred (nil for success) or if the end of the input has been reached (io.EOF).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IRgPxvXRtRA7tWTRLC2ADA.png" /><figcaption>Working of Reader and Writer</figcaption></figure><p>This abstraction allows functions to operate on io.Readers without caring if they&#39;re reading from a file, a network socket, or a byte buffer in memory. They all conform to the Read contract.</p><h4>The io.Writer Interface: Abstracting Data Destinations</h4><p>The io.Writer interface is the counterpart to io.Reader, abstracting any destination to which data can be written. It also defines a single method:</p><pre>type Writer interface {<br>    Write(p []byte) (n int, err error)<br>}</pre><p>Any type that provides a Write method with this signature implicitly satisfies io.Writer. It takes a byte slice p and attempts to write its contents to the underlying destination. It returns n, the number of bytes written, and an err if the operation fails.</p><p>A common example of an io.Writer is os.Stdout, which represents the standard output stream (your terminal).</p><h4>Creating Custom Writers: Satisfying the Contract</h4><p>The power of interfaces means you can define your own custom types that satisfy an interface simply by providing the required method. For instance, you could create a custom logWriter struct that implements io.Writer but instead of writing to a file, sends data to a logging service or just prints it with a timestamp:</p><pre>type logWriter struct{}<br>// logWriter implicitly implements io.Writer<br><br>func (logWriter) Write(bs []byte) (int, error) {<br>    fmt.Println(time.Now().Format(&quot;2006-01-02 15:04:05&quot;), string(bs))<br>    return len(bs), nil // Always claim to have written all bytes, no error<br>}<br><br>// You could then pass a logWriter to any function expecting an io.Writer<br>// myLogWriter := logWriter{}<br>// fmt.Fprint(myLogWriter, &quot;This message goes to my custom log writer!\n&quot;)</pre><p>This example highlights that the interface only cares that the <em>method signature</em> is met, not <em>what</em> the method actually does internally. A Write function that does nothing would still satisfy the io.Writer interface&#39;s criteria, even if it doesn&#39;t achieve the intended data output. This is the flexibility and responsibility interfaces delegate to the concrete type.</p><h4>Copying Data with io.Copy()</h4><p>Given the ubiquity of io.Reader and io.Writer, Go provides a convenient utility function: io.Copy(). This function efficiently copies data from an io.Reader (source) to an io.Writer (destination) until the source reaches its End-Of-File (EOF) or an error occurs.</p><pre>// Example: Reading an HTTP response body and writing it to standard output<br>resp, err := http.Get(&quot;https://jsonplaceholder.typicode.com/posts/1&quot;)<br>if err != nil {<br>    fmt.Println(&quot;Error fetching URL:&quot;, err)<br>    os.Exit(1)<br>}<br>defer resp.Body.Close() // Ensure the response body is closed<br><br>// Copy the content from resp.Body (an io.ReadCloser) to os.Stdout (an io.Writer)<br>numBytesRead, err := io.Copy(os.Stdout, resp.Body)<br>if err != nil {<br>    fmt.Println(&quot;Error copying response body:&quot;, err)<br>    os.Exit(1)<br>}<br>fmt.Printf(&quot;\nCopied %d bytes from response body.\n&quot;, numBytesRead)</pre><p>This demonstrates the power of interfaces: io.Copy works with any Reader and Writer, regardless of their concrete types, making data streaming operations highly abstract and reusable. Understanding io.Reader and io.Writer is fundamental to mastering file operations, network communication, and data processing in Go.</p><h3>Goroutines: Go’s Lightweight Concurrency Engines</h3><p>Modern applications frequently need to perform multiple operations simultaneously. Think about making a series of HTTP requests: if you send them one after another, each call waits for the previous one to complete, introducing unnecessary delays. This is where <strong>concurrency</strong> becomes vital, and Go’s answer to this is <strong>Goroutines</strong>.</p><h4>⭐️ What are Goroutines? ⭐️</h4><p>Goroutines are lightweight, independently executing functions. They are not operating system threads, but rather functions managed by the Go runtime. They are incredibly cheap to create and manage, allowing you to easily spawn thousands, even hundreds of thousands, within a single application.</p><p>When your Go program starts, the main function runs in its own goroutine, often called the <strong>main goroutine</strong>. When you define a new goroutine using the go keyword, the original goroutine that spawned it continues its execution immediately, while the new goroutine begins running concurrently in the background.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Q-82WgO-Eas6l1kZ.jpeg" /><figcaption>Goroutine</figcaption></figure><pre>package main<br><br>import (<br>   &quot;fmt&quot;<br>   &quot;time&quot;<br>)<br><br>func sayHello() {<br>   time.Sleep(100 * time.Millisecond) // Simulate some work<br>   fmt.Println(&quot;Hello from Goroutine!&quot;)<br>}<br><br>func main() {<br>   go sayHello() // Launch sayHello in a new goroutine<br>   fmt.Println(&quot;Hello from Main Routine!&quot;)<br>   time.Sleep(200 * time.Millisecond) // Give time for goroutine to finish<br>}<br>// Possible Output:<br>// Hello from Main Routine!<br>// Hello from Goroutine!</pre><p>Without the go keyword, sayHello() would execute synchronously, blocking fmt.Println(&quot;Hello from Main Routine!&quot;) until it completes. With go sayHello(), both messages might appear almost simultaneously or in an interleaved order.</p><h4>The Go Scheduler: Orchestrating Concurrency</h4><p>Behind the scenes, the <strong>Go scheduler</strong> is a crucial component of the Go runtime. Its job is to efficiently manage and distribute the execution of thousands of goroutines onto a much smaller number of operating system threads. Even on a single-core CPU, the scheduler rapidly switches between goroutines, giving the <em>appearance</em> of simultaneous execution (concurrency).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*s3OSblDoepTkfNrG.png" /><figcaption>Go Scheduler: Under the Hood</figcaption></figure><p>Take a deeper dive here: <a href="https://medium.com/@sanilkhurana7/understanding-the-go-scheduler-and-looking-at-how-it-works-e431a6daacf">Understanding the Go Scheduler and discovering how it works</a></p><p>For systems with multiple CPU cores, the Go scheduler truly shines. Since Go 1.5, the runtime by default uses all available logical CPU cores (via the GOMAXPROCS environment variable being set to the number of cores). This allows goroutines to run in true <strong>parallelism</strong>, with different goroutines executing simultaneously on different cores, maximizing throughput.</p><h4>Concurrency is Not Parallelism (But Goroutines Enable Both)</h4><p>This distinction is important:</p><ul><li><strong>Concurrency</strong> refers to the ability of a program to handle multiple tasks by making progress on all of them over a period of time. It’s about structuring a program such that multiple tasks can be interleaved and managed independently. A single-core CPU can be concurrent by rapidly switching between tasks.</li><li><strong>Parallelism</strong> refers to the simultaneous execution of multiple tasks. This requires multiple independent processing units (like CPU cores) to run tasks at the exact same moment.</li></ul><p>Go’s goroutines provide the primitives for <strong>concurrency</strong>. You write your code as if multiple tasks are running independently. <strong>Parallelism</strong> is then achieved by the Go runtime if your system has multiple cores and the scheduler can execute goroutines on them simultaneously. Goroutines are a powerful abstraction that allows you to think about concurrent behavior without getting bogged down in the complexities of managing OS threads directly.</p><h3>Channels: Communicating Between Goroutines</h3><p>While <strong>Goroutines</strong> provide the ability to run functions concurrently, they aren’t much use if they can’t communicate or synchronize their efforts. This is where <strong>Channels</strong> come in. Channels are the primary way goroutines send and receive data, ensuring safe and synchronized communication, preventing common concurrency bugs like race conditions.</p><p>Think of a channel as a conduit or a pipeline through which data can be passed between goroutines. Crucially, channels are <strong>typed</strong>, meaning only data of a specific type can be sent through them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FgUStyVWADImx6_k7aluAg.png" /><figcaption>Channels in Go</figcaption></figure><h4>Creating and Using Channels</h4><p>You create a channel using the built-in make function, specifying the type of data it will carry:</p><pre>ch := make(chan string) // Creates a channel that can send/receive strings</pre><p>Once a channel is created, you can send data into it or receive data from it using specialized operators:</p><ul><li><strong>Sending Data</strong>: ch &lt;- data (The arrow points <em>into</em> the channel)</li><li><strong>Receiving Data</strong>: data := &lt;- ch (The arrow points <em>out of</em> the channel) or simply &lt;- ch if you only want to consume the message without storing it.</li></ul><h4>⭐️ Blocking Behavior: Synchronization by Design ⭐️</h4><p>One of the most important aspects of channels is their <strong>blocking behavior</strong>. By default, channels are <em>unbuffered</em>, meaning they can only hold one value at a time.</p><ul><li>A send operation (ch &lt;- data) will <strong>block</strong> the sending goroutine until another goroutine is ready to receive the data from that channel.</li><li>A receive operation (&lt;- ch) will <strong>block</strong> the receiving goroutine until another goroutine sends data to that channel.</li></ul><p>This blocking nature is fundamental to how channels facilitate synchronization. They force goroutines to coordinate, ensuring that data is transferred safely and that one goroutine doesn’t outrun another.</p><pre>package main<br><br>import (<br>   &quot;fmt&quot;<br>   &quot;time&quot;<br>)<br><br>func fetchResource(url string, c chan string) {<br>   time.Sleep(2 * time.Second) // Simulate network delay<br>   c &lt;- fmt.Sprintf(&quot;Finished fetching: %s&quot;, url) // Send message to channel<br>}<br><br>func main() {<br>   c := make(chan string)<br>  <br>   go fetchResource(&quot;http://example.com/api/data&quot;, c) // Start goroutine to fetch data<br>  <br>   fmt.Println(&quot;Waiting for data...&quot;)<br>   result := &lt;-c // Main goroutine blocks here until a message is received from &#39;c&#39;<br>   fmt.Println(result)<br>   fmt.Println(&quot;Main routine continues.&quot;)<br>}</pre><p>In this example, the main goroutine waits at result := &lt;-c until fetchResource sends a message, thus synchronizing their execution. This is the idiomatic way to wait for a goroutine to complete a task or to signal progress, rather than using time.Sleep(), which should typically only be used for demonstrations or debugging.</p><p>Ideally, you’d use channels to wait for <em>all</em> routines to complete their execution, perhaps by sending multiple messages or using a sync.WaitGroup in conjunction with channels for more complex coordination.</p><h4>⭐️ Channels and the “Pass by Value” Nuance: A Clarification ⭐️</h4><p>A common point of confusion arises when discussing channels and Go’s “pass by value” paradigm. While Go <em>is</em> pass-by-value, channels themselves are “reference-like” types. This means that when you pass a channel variable to a function (even a goroutine), a <em>copy of the channel’s header</em> is passed, but this header points to the <em>same underlying channel structure</em>. So, you are effectively working on the same channel, allowing seamless communication between goroutines.</p><p>The “gotcha” that some developers encounter usually relates not to channels themselves, but to other <strong>variables that are captured by closures</strong> when launching goroutines. If a go func(){} (a function literal acting as a closure) accesses a variable from its surrounding scope, it captures that variable <em>by reference</em>. If multiple goroutines then try to <em>modify</em> this shared variable concurrently without proper synchronization (like using channels or mutexes), it leads to subtle and hard-to-debug data races. Channels are precisely the mechanism Go provides to <em>solve</em> these data sharing problems by ensuring data is passed safely between independent goroutines.</p><h4>Function Literals (Anonymous Functions)</h4><p>Often, when working with goroutines and channels, you’ll see <strong>function literals</strong>, also known as anonymous functions or lambda functions in other languages. These are functions defined inline, without a name, and are frequently used to define the task for a new goroutine:</p><pre>// A function literal assigned to a variable and then called<br>multiply := func(x int, y int) int {<br>    return x * y<br>}<br>result := multiply(4, 5) // result is 20<br><br>// A function literal directly invoked as a goroutine<br>go func(message string) {<br>    fmt.Println(message)<br>}(&quot;Hello from an anonymous goroutine!&quot;)</pre><p>The syntax for a function literal is func(args) return_type { implementation } and it can be immediately invoked by adding (params) after the curly braces. They are powerful for encapsulating small, specific tasks, especially for concurrent execution.</p><p>Goroutines and channels form the backbone of Go’s concurrency model, enabling you to write incredibly efficient and concurrent applications that effectively utilize modern multi-core processors. Mastering them is essential for any serious Go developer.</p><p>In this final part, we delved into some of Go’s most powerful and distinguishing features. We gained a solid understanding of <strong>pointers</strong>, clarifying their role in direct memory manipulation and demystifying Go’s “pass-by-value” nuance, especially concerning slices and maps. We then explored <strong>interfaces</strong>, Go’s elegant and implicit approach to polymorphism, learning how they enable flexible and decoupled code design. Finally, we immersed ourselves in <strong>Goroutines and Channels</strong>, the heart of Go’s concurrency model, understanding how these lightweight constructs allow for highly efficient and synchronized parallel programming, and briefly touched upon their application in <strong>HTTP and Web Services</strong>.</p><p>Looking back, our journey has been comprehensive:</p><ul><li>In <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-101-1cc420f7747a"><strong>GoLang Fundamentals 101</strong></a>, we set up your Go environment, grasped core syntax, understood packages and modules, and built foundational knowledge of variables, functions, arrays, and slices.</li><li>In <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-102-beyond-the-basics-7f4161f75daf"><strong>GoLang Fundamentals 102</strong></a>, we advanced to custom types and structs, learned about receivers, explored maps, understood file management, and embraced Go’s built-in testing capabilities.</li><li>And now, in this post, we’ve tackled the power of pointers, the elegance of interfaces, and the efficiency of concurrency.</li></ul><p>You’ve built a robust foundation in Go. You’re now equipped with the knowledge to structure your projects, manage data, interact with the file system, build concurrent applications, and develop web services. The concepts covered in this series are the bedrock for building high-performance, reliable, and scalable Go applications.</p><p>The best way to solidify this knowledge is through practice. Start building! Experiment with Goroutines and Channels, design your own interfaces, and create robust web APIs. The Go community is vibrant and welcoming, and the official documentation is an invaluable resource.</p><p>Thank you for joining me on this journey through GoLang Fundamentals. Keep coding, keep exploring, and keep building amazing things with Go!</p><h4>More to Study: Expanding Your Go Horizons</h4><p>To continue your learning and dive deeper into specific topics, here’s a consolidated list of valuable resources:</p><p><strong>General Go Resources &amp; Fundamentals:</strong></p><ul><li>The <a href="https://go.dev/ref/spec">Go Programming Language Specification</a></li><li><a href="https://pkg.go.dev/">Go Standard Library Documentation</a></li><li><a href="https://dev.to/cduggn/go-build-options-570e">Go Build Options</a></li><li><a href="https://medium.com/@ansujain/building-and-consuming-custom-packages-in-go-a-complete-guide-ee6566bf678%3C3%3E2">Building and Consuming Custom Packages in Go</a></li><li><a href="https://dev.to/cduggn/package-management-in-go-2hjl">Package Management in Go</a></li><li><a href="https://medium.com/sellerapp/golang-project-structuring-ben-johnson-way-2a11035f94bc">Golang Project Structuring — Ben Johnson Way</a></li></ul><p><strong>Deep Dives into Data Structures &amp; Internals:</strong></p><ul><li><a href="https://yuminlee2.medium.com/golang-deep-and-shallow-copy-a-slice-7dbcd9eb044a">Golang Deep and Shallow Copy a Slice</a></li><li><a href="https://medium.com/breaking-tech/internals-of-go-317e60fae1dc">Internals of Go</a></li><li>A Look at <a href="https://medium.com/eureka-engineering/a-look-at-iterators-in-go-f8e86062937c">Iterators in Go</a></li><li>Leveraging <a href="https://engineering.teknasyon.com/leveraging-gos-iterator-pattern-30b7d3be783f">Go’s Iterator Pattern</a></li></ul><p><strong>Interfaces &amp; Polymorphism:</strong></p><ul><li><a href="https://blog.stackademic.com/interfaces-in-golang-a-deeper-look-4a0931481c00">Interfaces in Golang: A Deeper Look</a></li><li>A <a href="https://dev.to/eyo000000/a-straightforward-guide-for-go-interface-16b2">Straightforward Guide for Go Interface</a></li><li><a href="https://medium.com/codex/go-interfaces-for-new-gophers-3d608db99d46">Go Interfaces for New Gophers</a></li><li>Understanding the <a href="https://medium.com/@jamal.kaksouri/understanding-the-power-of-go-interfaces-a-comprehensive-guide-835954101b7e">Power of Go Interfaces: A Comprehensive Guide</a></li><li><a href="https://amit-kumar-manjhi.medium.com/interfaces-in-golang-with-examples-ab5c5b7d34b7">Interfaces in Golang with Examples</a></li><li><a href="https://donofden.com/blog/2019/09/20/golang-interface-goroutine-channels">Interface, Goroutines &amp; Channels in Go</a></li></ul><p>Do check out my other blogs and follow me on Github for the latest on tech.</p><p>Find The Blog Series Here:</p><ol><li>Part 1 — <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-101-1cc420f7747a">GoLang Fundamentals 101</a></li><li>Part 2 — <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-102-beyond-the-basics-7f4161f75daf">GoLang Fundamentals 102: Beyond the Basics</a></li></ol><p>Medium: <a href="https://medium.com/@bhaumikmaan">Bhaumik Maan</a> || Github: <a href="https://github.com/bhaumikmaan">bhaumikmaan</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e75788e9e68" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GoLang Fundamentals 102: Beyond the Basics]]></title>
            <link>https://medium.com/@bhaumikmaan/golang-fundamentals-102-beyond-the-basics-7f4161f75daf?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f4161f75daf</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Fri, 06 Jun 2025 07:32:09 GMT</pubDate>
            <atom:updated>2025-06-06T07:32:09.551Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*uGEBmlQFO2PM6wDX.png" /></figure><p>Welcome back! In <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-101-1cc420f7747a">GoLang Fundamentals 101</a>, we established our Go foundation by exploring its core philosophy, setting up the environment, understanding packages and modules, and mastering basic variable, function, array, and slice declarations. Building on that, this post will deepen your understanding by diving into <strong>custom types and structs</strong> for defining complex data, practical <strong>file management</strong>, Go’s robust built-in <strong>testing</strong> framework, and the efficient key-value data structure known as <strong>maps</strong>. By the end, you’ll be well-equipped to handle more sophisticated data organization and development practices in your Go applications.</p><blockquote>Blog One Of the series: <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-101-1cc420f7747a">GoLang Fundamentals 101</a><br>Course Followed: <a href="https://thoughtworks.udemy.com/course/go-the-complete-developers-guide/">Udemy</a><br><a href="https://github.com/bhaumikmaan/Understanding-GoLang">Github Repository: Implementation of Course Examples</a><br>Blog Three Of the series: <a href="https://medium.com/@bhaumikmaan/golang-advanced-103-deep-dive-into-gos-power-features-2e75788e9e68">GoLang Advanced 103: Deep Dive into Go’s Power Features</a></blockquote><h3>Types in Go: Extending Functionality and Structuring Behavior</h3><p>Go provides a powerful mechanism to define your own custom types, allowing you to encapsulate not just data but also behavior, much like classes in object-oriented programming, but with Go’s distinct approach.</p><h4>⭐️ Defining Custom Types ⭐ ️</h4><p>You can define a new type based on an existing one using the type keyword. This creates a distinct type, even if its underlying structure is the same as another. This is particularly useful when you want to attach specific functions (methods) to your new type or ensure type safety. For example, we can define a deck type that is fundamentally a slice of strings:</p><pre>// Declares &#39;deck&#39; as a new type, which is a slice of strings<br>type deck []string </pre><p>Now, any variable declared as deck will behave like a []string but can also have functions specifically designed to operate on it.</p><h4>⭐️ Receivers in Go: Attaching Behavior to Types ⭐️</h4><p>This is where Go’s approach to “methods” comes into play through <strong>receivers</strong>. In Go, a function can be declared with a <em>receiver</em> argument, which binds that function to a specific type. This is a fundamental concept for making your custom types truly functional and distinct.</p><p>Consider the following print function:</p><pre>func (d deck) print() {<br>	for i, card := range d {<br>		fmt.Println(i, card)<br>	}<br>}</pre><p>Let’s break down func (d deck) print():</p><ul><li><strong>func</strong>: Standard keyword for declaring a function.</li><li><strong>(d deck)</strong>: This is the <strong>receiver</strong>. It indicates that the print function is now associated with the deck type.<br>- d is the actual copy of the deck variable that the function is called on. Within the print function&#39;s body, drefers to that specific deck instance, allowing you to access its elements (like card in the range loop).<br>- deck is the <em>type</em> that this function is now &quot;attached&quot; to. This means that any variable of the deck type will have access to this print function.</li><li><strong>print()</strong>: The name of the function, followed by its parameters (none, in this case).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aDYUMjCTCEAeRjO_.jpeg" /><figcaption>Receivers in Go</figcaption></figure><p>So, if you have a variable myDeck of type deck, you can simply call myDeck.print(). This is how you achieve object-oriented behavior in Go – through composition and functions with receivers, rather than traditional classes and inheritance.</p><p>By convention, the receiver variable is typically a one- or two-character abbreviation of the type name (e.g., d for deck, pfor person, s for service). Deciding whether to use a value receiver ((d deck)) or a pointer receiver ((d *deck)) depends on whether the function needs to modify the original instance of the type or just read from it. This is a crucial design decision for developers based on the function&#39;s purpose and the project&#39;s requirements.</p><h4>Multiple Return Values: Go’s Elegant Solution</h4><p>One of Go’s most distinctive and appreciated features is its support for <strong>multiple return values</strong>. This allows functions to return several values simultaneously, which is incredibly useful for conveying both a result and an error status, among other things.</p><p>When declaring a function that returns multiple values, you specify the types of all returned values in parentheses after the parameter list:</p><pre>// This function returns two values, both of type &#39;deck&#39;<br>func deal(d deck, handSize int) (deck, deck) {<br>    // ... logic to split the deck ...<br>    return d[:handSize], d[handSize:] // Example: returns two new slices<br>}</pre><p>To capture these multiple return values, you use a simple assignment syntax, listing the variables in the same order as the function returns them:</p><pre>cards := deck{&quot;Ace&quot;, &quot;King&quot;, &quot;Queen&quot;, &quot;Jack&quot;, &quot;Ten&quot;}<br>// &#39;hand&#39; will be the first returned &#39;deck&#39;, &#39;remainingDeck&#39; the second<br>hand, remainingDeck := deal(cards, 3) <br>fmt.Println(&quot;Hand:&quot;)<br>hand.print()<br>fmt.Println(&quot;Remaining Deck:&quot;)<br>remainingDeck.print()</pre><p>This feature is core to Go’s idiomatic error handling, where functions commonly return a result and an error value (e.g., value, err := someFunc()). Understanding how to define and capture multiple return values is fundamental to writing clean and effective Go code.</p><h3>Structs: Custom Data Structures in Go</h3><p>While Go doesn’t have classes in the traditional object-oriented sense, it provides <strong>structs</strong> (short for “structures”) as a powerful way to define custom data types that group together related properties. Structs are similar to structs in C++ or objects in other languages, allowing you to create blueprints for composite data.</p><h4>Defining a Struct</h4><p>A struct is declared using the type keyword, followed by the struct&#39;s name and the struct keyword, enclosing its fields (properties) and their respective types:</p><pre>type Person struct {<br>	FirstName string<br>	LastName  string<br>	Age       int<br>	IsStudent bool<br>}</pre><p>By convention, field names that start with an uppercase letter are “exported” (visible and accessible from other packages), while lowercase-initial fields are unexported (private to the package).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*W5CukgF9__PSxF4n.png" /><figcaption>Defining a struct</figcaption></figure><h4>Creating and Initializing Structs</h4><p>When you create a variable of a struct type, you can initialize its fields in a couple of ways:</p><ul><li><strong>Positional Initialization</strong>: If you initialize without explicitly naming the fields, the values must be provided in the same order as they are declared in the struct definition. This is generally less readable and more prone to errors if the struct definition changes.</li></ul><pre>// Not recommended for readability in larger structs<br>alex := Person{&quot;Alex&quot;, &quot;Anderson&quot;, 25, false}</pre><ul><li><strong>Named Field Initialization</strong>: This is the preferred and most readable way to initialize structs. You explicitly specify the field name and its corresponding value. This approach makes your code clearer and is resilient to changes in the struct’s field order.</li></ul><pre>alex := Person{<br>    FirstName: &quot;Alex&quot;,<br>    LastName:  &quot;Anderson&quot;,<br>    Age:       25,<br>    IsStudent: true,<br>}</pre><h4>Zero Values for Struct Fields</h4><p>Just like with basic types, if you declare a struct variable but don’t explicitly initialize all its fields, Go automatically assigns the <strong>zero value</strong> for each uninitialized field. This is a crucial concept in Go, ensuring that variables always have a defined state.</p><ul><li>string fields get &quot;&quot; (empty string)</li><li>int and float fields get 0 (or 0.0 for floats)</li><li>bool fields get false</li><li>Pointer, slice, map, and channel fields get nil</li></ul><pre>var defaultPerson Person // All fields will have their zero values<br>fmt.Println(defaultPerson.FirstName) // Output: &quot;&quot;<br>fmt.Println(defaultPerson.Age)       // Output: 0</pre><p>To print out all the fields and their values in a struct for debugging or inspection, you can use the fmt.Printf function with the %+v verb:</p><pre>fmt.Printf(&quot;%+v\n&quot;, alex)<br>// Output: {FirstName:Alex LastName:Anderson Age:25 IsStudent:true}</pre><h4>Updating Struct Fields</h4><p>You can access and update the value of an individual field within a struct using the . (dot) operator:</p><pre>alex.Age = 26              // Updates the &#39;Age&#39; field of &#39;alex&#39;<br>alex.IsStudent = false     // Updates the &#39;IsStudent&#39; field</pre><p>Remember our earlier discussion on pointers: if you pass a struct variable to a function, a <em>copy</em> is made. To modify the <em>original</em> struct instance within a function, you must pass a <strong>pointer to the struct</strong> (*Person).</p><h4>Embedding Structs: Composition Over Inheritance</h4><p>Go promotes <strong>composition</strong> over inheritance. This means that instead of inheriting properties from a parent class, you can embed one struct type directly into another. This allows you to build complex data structures by composing smaller, focused structs.</p><p>Consider a contactInfo struct:</p><pre>type contactInfo struct {<br>	Email   string<br>	ZipCode int<br>}</pre><p>Now, we can embed this contactInfo struct directly into our Person struct:</p><pre>type Person struct {<br>	FirstName string<br>	LastName  string<br>	// Embedding the contactInfo struct.<br>	// You can optionally give it a field name, e.g., &#39;Contact contactInfo&#39;<br>	// Or omit the field name for &quot;anonymous embedding&quot;<br>	contactInfo // This embeds contactInfo directly<br>}</pre><p>When embedding a struct <em>without</em> providing a field name (like contactInfo above), the fields of the embedded struct are &quot;promoted&quot; to the outer struct. This means you can access Email directly through a Person variable, rather than having to go through an intermediate field like person.contact.Email.</p><pre>jim := Person{<br>	FirstName: &quot;Jim&quot;,<br>	LastName:  &quot;Party&quot;,<br>	contactInfo: contactInfo{ <br>        // When initializing, you still specify the embedded struct name<br>		Email:   &quot;jim@example.com&quot;,<br>		ZipCode: 94000,<br>	},<br>}</pre><h4>Receivers with Structs: Adding Methods</h4><p>Just as we saw with our custom deck type, you can define functions with receivers for your structs. This allows you to attach methods directly to your struct types, enabling them to perform actions related to their data.</p><pre>func (p Person) describe() {<br>	fmt.Printf(&quot;Hello, my name is %s %s. My email is %s.\n&quot;, p.FirstName, p.LastName, p.Email)<br>}<br>// In main function:<br>// jim.describe() <br>// Calls the describe method on the jim Person struct</pre><p>Remember that for methods that need to modify the struct’s fields, you’ll typically use a pointer receiver (e.g., func (p *Person) updateName(newFirstName string)).</p><p>Structs are a cornerstone of Go’s type system, providing a flexible and powerful way to model complex data relationships and attach behavior. Mastering them is key to building well-organized and maintainable Go applications.</p><h3>Maps: Powerful Key-Value Data Structures</h3><p>When you need to store data as key-value pairs, Go provides <strong>maps</strong>. Maps are Go’s equivalent of dictionaries in Python, hash tables in Java, or hashes in Ruby. They are incredibly versatile for storing collections where you want to look up a value based on a unique key.</p><h4>Defining and Initializing Maps</h4><p>A map in Go is a collection of unordered key-value pairs. Crucially, all keys within a single map must be of the same type, and all values must also be of the same type. Go is statically typed, so these types are defined upfront.</p><p>You declare and initialize a map using the following syntax:</p><pre>// A map where keys are strings and values are integers<br>colors := map[string]int{<br>    &quot;red&quot;:   10,<br>    &quot;green&quot;: 20,<br>    &quot;blue&quot;:  30,<br>}<br><br>// Or, to create an empty map using &#39;make()&#39;<br>// This is often preferred when you&#39;ll populate the map later<br>ages := make(map[string]int) // Creates an empty map with string keys and int values<br>ages[&quot;Alice&quot;] = 30<br>ages[&quot;Bob&quot;] = 25</pre><h4>Accessing, Updating, and Deleting Map Entries</h4><ul><li><strong>Accessing Values</strong>: You retrieve a value from a map by providing its key in square brackets:</li></ul><pre>redValue := colors[&quot;red&quot;] // redValue will be 10 fmt.Println(redValue)</pre><ul><li>Go also provides a “comma ok” idiom for checking if a key exists before accessing its value, which helps avoid issues if the key isn’t present:</li></ul><pre>value, ok := colors[&quot;yellow&quot;] <br>// &#39;value&#39; will be zero value (0), &#39;ok&#39; will be false<br>if ok {<br>    fmt.Println(&quot;Yellow found:&quot;, value)<br>} else {<br>    fmt.Println(&quot;Yellow not found!&quot;)<br>}</pre><ul><li><strong>Updating Values</strong>: To update a value associated with an existing key, or to add a new key-value pair, you simply assign a new value to the key:</li></ul><pre>colors[&quot;red&quot;] = 15 // Updates the value for &quot;red&quot;<br>colors[&quot;purple&quot;] = 40 // Adds a new key-value pair<br>fmt.Println(colors) // Output: map[blue:30 green:20 purple:40 red:15]</pre><ul><li><strong>Deleting Entries</strong>: You can remove a key-value pair from a map using the built-in delete() function:</li></ul><pre>delete(colors, &quot;green&quot;) <br>// Removes the &quot;green&quot; entry from the map <br>fmt.Println(colors)     <br>// Output: map[blue:30 purple:40 red:15]</pre><h4>Iterating Over Maps</h4><p>You can iterate over the key-value pairs in a map using a for...range loop, similar to how we iterate over slices. The order of iteration for maps is <em>not guaranteed</em> and can vary with each run.</p><pre>for key, value := range colors {<br>    fmt.Printf(&quot;Key: %s, Value: %d\n&quot;, key, value)<br>}<br>// Example Output (order may vary):<br>// Key: blue, Value: 30<br>// Key: purple, Value: 40<br>// Key: red, Value: 15</pre><p>Similar to slices, if you only need the key or the value, you can use the blank identifier _.</p><h4>Maps as Reference Types</h4><p>It’s important to remember that maps, like slices and channels, are <strong>reference types</strong>. This means that when you pass a map to a function, a copy of the map header is passed, but this header still points to the <em>same underlying hash table data structure</em>. Therefore, any modifications (adding, updating, deleting entries) made to the map inside the function will affect the original map outside the function. You do not need to pass a pointer to a map to modify its contents.</p><h4>When to Use Maps vs. Structs: A Key Distinction</h4><p>Choosing between a map and a struct depends on your use case:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YwrpHLEPhMq7IJ2ZnVAH9A.png" /><figcaption>Comparison between map and structs</figcaption></figure><p>In essence, use a <strong>struct</strong> when you know the exact names and types of properties you need to store for a defined entity. Use a <strong>map</strong> when the specific keys might vary at runtime, or when you’re dealing with a collection of arbitrary key-value pairs</p><h3>Testing in Go: Ensuring Code Quality and Reliability</h3><p>Writing robust software isn’t just about crafting elegant code; it’s also about ensuring that code behaves as expected under various conditions. Go provides a lightweight yet powerful <strong>built-in testing interface</strong> that makes writing and running tests an integral part of the development workflow. You don’t need to import external testing frameworks for basic unit tests; it’s all part of the standard toolchain.</p><h4>Creating Your First Test</h4><p>To create a test file in Go, simply create a new file in the same package directory as the code you want to test, and ensure its name ends with _test.go. For example, if you have main.go, your test file might be main_test.go.</p><p>Inside this _test.go file, you define your test functions. These functions follow a specific naming convention and signature:</p><ul><li>The function name <strong>must start with </strong><strong>Test</strong> (capital &#39;T&#39;).</li><li>It must be followed by a descriptive name that typically starts with a capital letter (e.g., TestCalculateSum, TestNewDeck). This naming convention helps the go test runner identify which functions to execute.</li><li>The function must take a single argument: t *testing.T. The *testing.T type provides methods for reporting test failures, logging messages, and controlling test execution.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/0*6CNi_NjVq9w5p5xw.png" /></figure><p>Here’s a basic example:</p><pre>// In main_test.go (testing a function from main.go)<br><br>package main<br><br>import (<br>   &quot;testing&quot; // Import the built-in testing package<br>)<br><br>func TestNewDeck(t *testing.T) {<br>   // Assume newDeck() is a function from main.go that creates a new deck<br>   d := newDeck() // Your code to test<br>  <br>   if len(d) != 16 {<br>    t.Errorf(&quot;Expected deck length of 16, but got %v&quot;, len(d))<br>   }<br>  <br>   if d[0] != &quot;Ace of Spades&quot; {<br>    t.Errorf(&quot;Expected first card to be Ace of Spades, but got %v&quot;, d[0])<br>   }<br>   // ... more assertions ...<br>}</pre><h4>Running Your Tests</h4><p>Once you’ve written your _test.go file, running your tests is as simple as navigating to your package directory in the terminal and executing the command:</p><pre>go test</pre><p>This command will automatically discover and run all test functions within the current package. It will report on any failures and provide a summary of the test results.</p><p>As a developer, your role is to identify the critical behaviors and edge cases within your functions and write targeted tests to verify their correctness. Go’s integrated testing framework makes this process straightforward, encouraging you to write more reliable and maintainable code from the start.</p><h3>File Management in Go: Interacting with the System</h3><p>A crucial aspect of many applications involves interacting with the file system — reading data from files or writing data to them. Go provides robust and straightforward packages for these tasks, primarily the os and io packages.</p><h4>Reading and Writing Files</h4><p>For common file operations, Go’s os package offers convenient functions. Data is typically handled as a slice of bytes ([]byte), which can represent anything from text (encoded, e.g., UTF-8) to binary data.</p><ul><li><strong>os.WriteFile(filename string, data []byte, perm fs.FileMode) error</strong>: This function writes a slice of bytes (data) to a file specified by filename. If the file doesn&#39;t exist, it&#39;s created with the given file permissions (perm). If it exists, WriteFile truncates it before writing. The perm argument sets the file mode bits (e.g., 0666 for read/write access for owner, group, others). It returns an error if the operation fails.</li><li><strong>os.ReadFile(filename string) ([]byte, error)</strong>: This function reads the entire content of a file specified by filename and returns it as a slice of bytes ([]byte). It also returns an error if the operation fails (e.g., file not found, permission denied).</li></ul><p><strong>Handling Errors is Paramount</strong>: In Go, functions that interact with the file system (and many other operations) often return an error as their last return value. It&#39;s an idiomatic Go practice to check this error immediately:</p><pre>// Example: Writing to a file<br>content := []byte(&quot;Hello, Go File System!&quot;)<br>err := os.WriteFile(&quot;myFile.txt&quot;, content, 0666) // 0666 gives read/write permissions<br>if err != nil {<br>    // Handle the error, e.g., print it and exit<br>    fmt.Println(&quot;Error writing file:&quot;, err)<br>    os.Exit(1) // Exit with a non-zero status indicating failure<br>}<br>fmt.Println(&quot;File &#39;myFile.txt&#39; written successfully.&quot;)<br>// Example: Reading from a file<br>readData, err := os.ReadFile(&quot;myFile.txt&quot;)<br>if err != nil {<br>    fmt.Println(&quot;Error reading file:&quot;, err)<br>    os.Exit(1)<br>}<br>fmt.Println(&quot;File content:&quot;, string(readData)) // Convert byte slice back to string for printing</pre><h4>Other Essential os Package Operations</h4><p>The os package provides a variety of functions for interacting with the operating system:</p><ul><li><strong>os.Exit(code int)</strong>: Terminates the program immediately. A non-zero exit code (e.g., os.Exit(1)) conventionally indicates that the program exited with an error or failure, while 0 indicates success.</li><li><strong>os.Remove(name string)</strong>: Deletes the file specified by name. This is particularly useful in testing scenarios where temporary files are created and need to be cleaned up afterward.</li><li><strong>os.Args</strong>: A slice of strings that holds the command-line arguments passed to the program. os.Args[0] is the program name itself.</li><li><strong>os.Open(name string)</strong>: Opens a file for reading. Unlike ReadFile, os.Open returns a *os.File object, which provides more fine-grained control over reading (e.g., reading line by line, seeking within the file).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*k193q4QIIHKPSik7.png" /></figure><h4>Type Conversion: Bridging Data Forms</h4><p>Since file operations often work with []byte, you&#39;ll frequently need to convert between string data and byte slices. Go provides a simple and direct syntax for type conversion:</p><pre>myString := &quot;This is a string.&quot;<br>myBytes := []byte(myString) // Converts string to []byte<br>myConvertedString := string(myBytes) // Converts []byte back to string</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CplVGPzp4krPomOmlbBokA.png" /><figcaption>Unsafe type conversion</figcaption></figure><h4>Important Built-in Functions</h4><p>Beyond file management, Go offers several general-purpose built-in functions that you’ll use frequently:</p><ul><li><strong>len(collection)</strong>: Returns the length of various collections, including slices, arrays, maps, and strings. For a string, it returns the number of bytes, not necessarily the number of characters (due to Unicode).</li><li><strong>rand.Intn(n int)</strong>: Located in the math/rand package, this function generates a pseudo-random integer between 0(inclusive) and n (exclusive).</li><li><strong>Important Note on Randomness</strong>: For truly different “random” numbers each time your program runs, you must <strong>seed</strong> the random number generator. Without seeding, rand.Intn will produce the same sequence of numbers every time. A common way to seed it is using the current time:</li></ul><pre>import (<br> &quot;fmt&quot;<br> &quot;math/rand&quot;<br> &quot;time&quot;<br>)<br><br>func main() {<br>   source := rand.NewSource(time.Now().UnixNano()) // Create a new source with a unique seed<br>   r := rand.New(source)                           // Create a new random generator<br>  <br>   randomNumber := r.Intn(100) // Generates a number from 0 to 99<br>   fmt.Println(randomNumber)<br>}</pre><p>With these tools for file management, type conversion, and common utility functions, you’re now well-equipped to build more interactive and robust Go applications that can persist and retrieve data.</p><p>With this, we come to an end for “GoLang Fundamentals 102.” You’ve just completed another significant step in your Go programming journey! We equipped you with more sophisticated tools, exploring <strong>custom types and structs</strong> for data modeling, the practicalities of <strong>file management</strong> using the os package, and Go&#39;s built-in framework for <strong>testing</strong>. Finally, we demystified <strong>maps</strong>, Go&#39;s powerful key-value data structure.</p><p>You now possess a stronger understanding of Go’s data structuring, file interaction, and how to write maintainable, tested code</p><h4>What’s Next?</h4><p>In our next blog, we’ll dive into Go’s most powerful and distinctive features: <strong>Interfaces</strong>, <strong>Pointers</strong>, <strong>Go-routines and Channels</strong> for concurrency, and building <strong>HTTP and Web Services</strong>.</p><h4>More to Study:</h4><p>To deepen your understanding and explore further, here are some valuable resources:</p><ul><li><a href="https://blog.devgenius.io/oop-vs-go-4021aa9f11ba?gi=34b475d3c05d">OOP vs Go</a></li><li><a href="https://medium.com/tunaiku-tech/unit-test-in-golang-57a2a896d90d">Unit Test in Golang</a></li><li><a href="https://medium.com/@SabujJanaCodes/leveraging-go-concurrency-building-a-public-file-download-manager-4804d0b19fd0">Leveraging Go concurrency</a></li><li><a href="https://medium.com/lyonas/go-type-casting-starter-guide-a9c1811670c5">Safe Go Type Conversions: Comprehensive Guide</a></li></ul><p>Do check out my other blogs and follow me on Github for the latest on tech.</p><p>Find The Blog Series Here:</p><ol><li>Part 1— <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-101-1cc420f7747a">GoLang Fundamentals 101</a></li><li>Part 3 — <a href="https://medium.com/@bhaumikmaan/golang-advanced-103-deep-dive-into-gos-power-features-2e75788e9e68">GoLang Advanced 103: Deep Dive into Go’s Power Features</a></li></ol><p>Medium: <a href="https://medium.com/@bhaumikmaan">Bhaumik Maan</a> || Github: <a href="https://github.com/bhaumikmaan">bhaumikmaan</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f4161f75daf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GoLang Fundamentals 101]]></title>
            <link>https://medium.com/@bhaumikmaan/golang-fundamentals-101-1cc420f7747a?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cc420f7747a</guid>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Thu, 05 Jun 2025 07:32:22 GMT</pubDate>
            <atom:updated>2025-06-06T05:12:21.911Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lKStCaCCCXgakORe.png" /></figure><p>In today’s software development landscape, performance, efficiency, and concurrency are paramount. <strong>Go</strong>, also known as <strong>Golang</strong>, emerged from Google to address these needs, offering a statically-typed, compiled language that prioritizes simplicity and effectiveness for modern systems. Go’s design philosophy sets it apart; unlike object-oriented languages where you might encapsulate an entire “<em>Card Deck</em>” project within a single class, Go adopts a more declarative approach. You’ll build applications using explicit data structures (structs) and independent functions, often chained together to achieve desired outcomes. This approach fosters explicit, performant, and easily reasoned-about code.</p><p>In this first part of our GoLang Fundamentals series, we’ll dive into the absolute essentials. We’ll cover setting up your environment, understanding Go’s core syntax, and grasping its fundamental data types and control structures. By the end, you’ll have a solid foundation to start building your own Go applications and appreciate why so many developers are embracing this powerful language for everything from web services to system tools.</p><blockquote>Course Followed: <a href="https://thoughtworks.udemy.com/course/go-the-complete-developers-guide/">Udemy</a><br><a href="https://github.com/bhaumikmaan/Understanding-GoLang">Github Repository: Implementation of Course Examples</a><br>Part 2 of the blog series — <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-102-beyond-the-basics-7f4161f75daf">GoLang Fundamentals 102: Beyond the Basics</a><br>Part 3 of the blog series— <a href="https://medium.com/@bhaumikmaan/golang-advanced-103-deep-dive-into-gos-power-features-2e75788e9e68">GoLang Advanced 103: Deep Dive into Go’s Power Features</a></blockquote><h3>Setting Up Go Environment &amp; Understanding Basic Structure</h3><p>Go stands out for its emphasis on <strong>simplicity, readability, and efficiency</strong>. It’s a language meticulously designed for building reliable and scalable software, especially in the era of concurrent and distributed systems. From its straightforward syntax to its opinionated approach to code formatting and robust dependency management, Go aims to minimize cognitive overhead for developers. This section will guide you through the initial setup and introduce you to the fundamental structural elements that underpin every Go application, helping you understand how Go code is organized and executed.</p><h4>First Steps: Running Go Code</h4><p>Go makes it incredibly simple to execute your programs. Once you have Go installed, you can run a single Go source file directly from your terminal using the go run command:</p><pre>go run main.go</pre><p>This command compiles and runs your specified .go file in one step. While go run is great for quick tests and single-file scripts, Go also provides a robust set of command-line tools for managing your projects:</p><ul><li><strong>go build</strong>: This command compiles your Go package and its dependencies, producing an executable binary file. For an executable package, this creates a runnable program. For non-executable packages, it compiles them into reusable archives.</li><li><strong>go fmt</strong>: Go has a strict, opinionated formatter. go fmt automatically formats your Go source code according to the official Go style, ensuring consistency across all Go projects. This eliminates debates over code style and focuses developers on logic.</li><li><strong>go install</strong>: Compiles and installs a package. For executable packages, it places the resulting binary in your $GOPATH/bin or $GOBIN directory, making it accessible from anywhere in your terminal&#39;s path. For non-executable packages, it installs them into the package cache.</li><li><strong>go get</strong>: (Primarily used in older Go versions or specific contexts, largely superseded by Go Modules for general dependency management) This command is used to download and install packages and their dependencies from remote repositories.</li><li><strong>go test</strong>: Runs the tests associated with your Go packages. Go has built-in testing capabilities, which we&#39;ll explore in a later blog post.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*Ww6I5zxuwpZeu6Qe.png" /><figcaption>Compiling a GO Program</figcaption></figure><h4>Go Packages: The Building Blocks</h4><p>In Go, code organization revolves around <strong>packages</strong>. A package is essentially a collection of common source code files that work together to provide a specific set of functionalities. Think of it as a logical grouping for related code. Every .gofile must belong to a package, and its first line always declares the package name it belongs to:</p><pre>package main // This file belongs to the &#39;main&#39; package</pre><p>Go distinguishes between two primary types of packages:</p><ul><li><strong>Executable Packages</strong>: These packages generate a standalone executable file that you can run. They are designed to be the entry point of an application. To create an executable package, you must name it main. An executable mainpackage must also contain a function named main() which serves as the program&#39;s starting point.</li></ul><pre>package main  <br><br>func main() { <br>  // Your program starts execution here <br>}</pre><ul><li><strong>Reusable Packages (or Library Packages)</strong>: These packages contain reusable logic, helper functions, or data structures that can be imported and utilized by other packages. They do not produce an executable file on their own. Any package name other than main will result in a reusable package. This promotes modularity and code reuse across different projects.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XbWnHdALwq70stCw.png" /><figcaption>Custom Packaging in GO</figcaption></figure><h4>Importing External Code: The import Statement</h4><p>To access code written in other packages (whether standard library packages or your own custom ones), you use the import statement. Go&#39;s standard library is incredibly rich and provides a vast array of functionalities for common tasks. For example, the fmt package provides functions for formatted I/O (like printing to the console).</p><pre>package main<br><br>import &quot;fmt&quot; // Imports the &#39;fmt&#39; package<br><br>func main() {<br>    fmt.Println(&quot;Hello, Go!&quot;) // Uses a function from the &#39;fmt&#39; package<br>}</pre><p>You can explore the extensive standard packages at the official Go documentation: <a href="https://pkg.go.dev/std">Here</a></p><h4>Go File Organization: A Strict Order</h4><p>Go mandates a specific order for declarations within a source file, which helps maintain consistency and readability across projects:</p><ol><li><strong>Package Declaration</strong>: Always the very first line.</li><li><strong>Import Statements</strong>: Follows the package declaration. You can group related imports or list them individually.</li><li><strong>Functions and Variable Declarations</strong>: All your functions, global variables, type declarations, etc., come after the imports.</li></ol><pre>package mypackage // 1. Package declaration<br><br>import (<br>    &quot;fmt&quot; // 2. Import statements<br>    &quot;log&quot;<br>)<br><br>var myVariable int = 10 // 3. Global variable declaration<br><br>func myFunction() { // 3. Function declaration<br>   fmt.Println(&quot;Hello from myFunction&quot;)<br>}<br><br>type MyStruct struct { // 3. Type declaration<br>   Name string<br>}</pre><h4>Managing Dependencies with Go Modules</h4><p>Before Go Modules, dependency management could be a bit cumbersome using GOPATH. Modern Go development relies heavily on <strong>Go Modules</strong>, which provide a robust and isolated way to manage project dependencies. Every Go module is defined by a go.mod file, which sits at the root of your project. This file describes the module&#39;s properties, including its unique module path, its Go version requirement, and its dependencies on other modules.<br>To initialize a new Go module in your project directory, simply run:</p><pre>go mod init &lt;your-module-path&gt;</pre><p>The &lt;your-module-path&gt; typically corresponds to where your module would be hosted (e.g., github.com/yourusername/yourproject). This module path becomes the import prefix for all packages contained within your module, ensuring that other projects can uniquely identify and import your code.</p><p>For example, if your go.mod file starts with module github.com/yourusername/mygomodule, then any package inside this module (e.g., mygomodule/utils) would be imported as github.com/yourusername/mygomodule/utils by other Go projects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PMzsPFavTZzqBNzR.jpeg" /><figcaption>Project Structuring in GO</figcaption></figure><p>With these foundational concepts in place, you’re now ready to delve into the core syntax and data types that make up the Go language.</p><h3>Declarations: Variables and Functions in Go</h3><p>With your Go environment set up and an understanding of packages, it’s time to dive into how you declare fundamental building blocks in Go: variables and functions. Go’s approach to declarations is designed for clarity and conciseness, reflecting its overall philosophy.</p><h4>Declaring Variables: Statically Typed Simplicity</h4><p>Go is a <strong>statically typed language</strong>, similar to C++ or Java, but unlike dynamic languages such as JavaScript or Python. This means that when you declare a variable in Go, its type is defined at compile time, determining the kind of values it can store. This strong typing helps catch errors early and contributes to Go’s performance.</p><p>The most explicit way to declare a variable is using the var keyword, specifying its name, type, and an optional initial value:</p><pre>var name string = &quot;Name&quot;   // Declares a string variable &#39;name&#39; with an initial value<br>var age int = 30           // Declares an integer variable &#39;age&#39;</pre><p>Go comes with a set of fundamental or <strong>“basic” data types</strong>:</p><ul><li><strong>bool</strong>: For boolean values (true or false).</li><li><strong>string</strong>: For sequences of characters.</li><li><strong>int</strong>: For whole numbers (its size depends on the underlying system, typically 32 or 64 bits). Go also provides various fixed-size integer types like int8, int16, int32, int64, and their unsigned counterparts (uint, uint8, etc.).</li><li><strong>float64</strong>: For floating-point numbers (standard for most decimal calculations). float32 is also available.</li></ul><p>One of Go’s convenient features is <strong>type inference</strong>. Often, Go’s compiler is smart enough to deduce the variable’s type based on the value you assign to it. This allows for a shorter declaration syntax using the := (short variable declaration) operator:</p><pre>card := &quot;Ace of Spades&quot; // Go infers &#39;card&#39; is a string<br>quantity := 5           // Go infers &#39;quantity&#39; is an int<br>temperature := 25.5     // Go infers &#39;temperature&#39; is a float64</pre><p><strong>Important Note:</strong> The := operator can <strong>only be used when declaring a <em>new</em> variable</strong>. Once a variable is declared, you update its value using the simple assignment operator =:</p><pre>card := &quot;Ace of Spades&quot; // Declaration and assignment<br>card = &quot;Five of Clubs&quot;  // Assignment (updating the value)</pre><p>You can also declare a variable without assigning an initial value. In such cases, Go automatically assigns a “zero value” to the variable based on its type (e.g., 0 for numeric types, &quot;&quot; for strings, false for booleans, nil for slices, maps, and pointers).</p><pre>var deckSize int // Declared, but not assigned. Its value will be 0.</pre><p>A quick rule to remember about variable placement: while you can declare variables outside of functions (these become package-level or “global” variables), you <strong>cannot use the </strong><strong>:= short declaration outside of a function</strong>. Package-level variables must be declared using the var keyword and can be initialized, but not directly assigned to without declaration.</p><pre>// This is valid:<br>var packageVar string = &quot;Hello&quot;<br>// This is also valid:<br>var anotherPackageVar int<br>// This is NOT valid outside a function:<br>// packageVar := &quot;World&quot; // Compile error</pre><h4>Declaring Functions: Actions in Your Program</h4><p>Functions are the workhorses of Go programs, encapsulating blocks of code to perform specific tasks. The basic syntax for a function declaration starts with the func keyword, followed by the function name, its parameters, and optionally, its return type.</p><pre>func newCard() string { <br>    // This function named &#39;newCard&#39; will return a value of type string<br>    return &quot;Five of Diamonds&quot;<br>}<br><br>func printGreeting(name string) { <br>    // This function takes a string parameter and returns nothing<br>    fmt.Println(&quot;Hello,&quot;, name)<br>}</pre><p>A key aspect of Go’s package system is that <strong>files within the same package do not need to explicitly import each other</strong>. If you have two .go files (e.g., main.go and helper.go) that both declare package main, functions defined in helper.gocan be directly called from main.go (and vice-versa), provided they are accessible (i.e., their names start with a capital letter if being called from another package, but within the same package, lowercase initial functions are also accessible).</p><pre>// In main.go (part of package main)<br>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br>   card := newCard() // Calling a function from helper.go (assuming it&#39;s in the same package)<br>   fmt.Println(card)<br>   printGreeting(&quot;Alice&quot;) // Calling another function<br>}</pre><pre>// In helper.go (also part of package main)<br>package main<br><br>func newCard() string {<br>   return &quot;Ace of Spades&quot;<br>}<br><br>func printGreeting(name string) {<br>   fmt.Println(&quot;Greetings, &quot; + name)<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rnf5Uz3dLjxupVtw" /><figcaption>Working of Functions in GoLang</figcaption></figure><p>This direct accessibility within a package simplifies code organization and reduces boilerplate. With variables and functions now under your belt, you have the essential tools to start building more complex logic in your Go applications</p><h3>Arrays and Slices: Handling Collections of Data</h3><p>When working with collections of data, Go offers two primary mechanisms: <strong>Arrays</strong> and <strong>Slices</strong>. While they might seem similar at first glance, they have fundamental differences in how they manage data and flexibility, with slices being far more commonly used in day-to-day Go programming. Both require you to define the data type of the elements they will contain.</p><h4>Arrays: Fixed-Length Collections</h4><p>An <strong>array</strong> in Go is a fixed-length list of elements of a specific type. Once an array is declared with a certain size, its length cannot be changed. This makes arrays somewhat rigid for many common programming tasks where the number of elements is not known beforehand or needs to change dynamically.</p><p>To declare an array, you must specify its length as part of its type:</p><pre>var colors [3]string // Declares an array named &#39;colors&#39; that can hold 3 strings<br>colors[0] = &quot;Red&quot;<br>colors[1] = &quot;Green&quot;<br>colors[2] = &quot;Blue&quot;<br><br>// Or initialized directly:<br>numbers := [5]int{10, 20, 30, 40, 50} // An array of 5 integers</pre><p>Due to their fixed size, arrays are less frequently used directly in Go application code compared to slices, except in very specific scenarios where size immutability is a requirement (e.g., performance-critical fixed-size buffers) or as the underlying data structure for slices.</p><h4>Slices: Dynamic and Flexible Collections</h4><p><strong>Slices</strong> are Go’s powerful and flexible alternative to arrays. They are built on top of arrays but provide a dynamic, “growable” view into an underlying array segment. Think of them as similar to Vectors in C++ or ArrayLists in Java, allowing you to easily add or remove elements.</p><p>A slice itself is a lightweight data structure (a “slice header”) that describes a contiguous segment of an underlying array. This header contains three components: a pointer to the start of the underlying array, its length (the number of elements currently in the slice), and its capacity (the maximum number of elements the underlying array can hold from the pointer’s position).</p><p>You can declare a slice using the := short declaration, often initializing it with literal values:</p><pre>// A slice named &#39;deck&#39; that holds strings<br>deck := []string{&quot;Ace of Spades&quot;, &quot;King of Clubs&quot;, &quot;Queen of Hearts&quot;}</pre><p>Notice the [] without a number, indicating a slice rather than an array.</p><ul><li><strong>Adding Elements</strong> to a Slice: append()<br>To add elements to a slice, you use the built-in append() function. It&#39;s crucial to understand how append() works:<br>- It adds an element to the back of the slice and <strong>returns a <em>new</em> slice</strong>. <br>- It does not modify the original slice in place, especially if the underlying array needs to be reallocated due to insufficient capacity.<br>- Therefore, you almost always need to reassign the result of append() back to the original slice variable.</li></ul><pre>mySlice := []int{1, 2, 3}<br>mySlice = append(mySlice, 4) // Appends 4, and assigns the potentially new slice back to mySlice<br>fmt.Println(mySlice)        // Output: [1 2 3 4]<br><br>mySlice = append(mySlice, 5, 6) // You can append multiple elements at once<br>fmt.Println(mySlice)            // Output: [1 2 3 4 5 6]</pre><ul><li><strong>Accessing Elements</strong> and Sub-Slices: <strong>Indexing</strong></li></ul><p>Like arrays, slices are <strong>zero-indexed</strong>, meaning the first element is at index 0. You access individual elements using square brackets:</p><pre>firstCard := deck[0] // Accesses &quot;Ace of Spades&quot;</pre><p>Go also provides elegant syntax for selecting a subset of elements, creating a “sub-slice.” This operation returns a <em>new slice</em> that references a portion of the <em>same underlying array</em> as the original slice:</p><pre>cards := []string{&quot;Ace&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;, &quot;Five&quot;}<br><br>// Get elements from index 0 up to (but not including) index 2<br>firstTwoCards := cards[0:2] // Result: [&quot;Ace&quot;, &quot;Two&quot;]<br><br>// Get all elements from the beginning up to (but not including) index 3<br>upToThree := cards[:3]      // Result: [&quot;Ace&quot;, &quot;Two&quot;, &quot;Three&quot;]<br><br>// Get all elements from index 2 to the end<br>fromTwoToEnd := cards[2:]   // Result: [&quot;Three&quot;, &quot;Four&quot;, &quot;Five&quot;]</pre><p>This sub-slicing capability is incredibly powerful for manipulating portions of data without creating expensive copies.</p><h4>Iterating Over Slices (and Arrays): The for...range Loop</h4><p>The most common and idiomatic way to iterate over elements in a slice (or an array) is using the for...range loop. This loop provides both the index and the element at each iteration:</p><pre>numbers := []int{10, 20, 30}<br>for index, element := range numbers {<br>    fmt.Printf(&quot;Index: %d, Element: %d\n&quot;, index, element)<br>}<br>// Output:<br>// Index: 0, Element: 10<br>// Index: 1, Element: 20<br>// Index: 2, Element: 30</pre><p>Notice the use of := in for index, element := range numbers. This is because range effectively declares <em>new</em> indexand element variables for each iteration, which are then assigned the current index and value.</p><p><strong>Handling Unused Variables</strong>: Often, you might only need the element and not its index, or vice-versa. In Go, it’s a compile-time error to declare a variable and not use it. To indicate that you intentionally don’t need a returned value, you can use the <strong>blank identifier</strong> (_):</p><pre>// If you only need the element (value)<br>for _, element := range numbers {<br>    fmt.Printf(&quot;Element: %d\n&quot;, element)<br>}<br><br>// If you only need the index<br>for index, _ := range numbers {<br>    fmt.Printf(&quot;Index: %d\n&quot;, index)<br>}</pre><p>Understanding slices is fundamental to writing effective Go programs, as they are the go-to data structure for dynamic collections. With this knowledge, you’re now equipped to manage ordered lists of data efficiently</p><p>And that’s a wrap on the foundational concepts of Go. We started by understanding <strong>why Go is a compelling choice</strong> for modern software development, appreciating its focus on simplicity, performance, and concurrency. We then delved into <strong>setting up your environment</strong> and essential command-line tools like go run and go build. You learned about the structure of <strong>Go packages</strong>, the distinction between executable and reusable types, and the vital role of <strong>Go Modules</strong> in managing dependencies. Finally, we covered <strong>declaring variables and functions</strong>, highlighting Go&#39;s static typing and convenient short declaration syntax, and clarified the distinction between fixed-size <strong>arrays</strong> and the more flexible, dynamic <strong>slices</strong>, including how to append elements and iterate using for...range.</p><p>You now have a solid understanding of how Go code is structured, organized, and executed. This foundational knowledge is essential for tackling more complex concepts.</p><h4>What’s Next?</h4><p>In the upcoming parts of our GoLang Fundamentals series, we’ll dive deeper into some of Go’s most powerful and distinctive features, including <strong>concurrency with Goroutines and Channels</strong>, <strong>Interfaces</strong>, <strong>error handling best practices</strong>, <strong>struct methods</strong>, and <strong>working with files and I/O</strong>.</p><h4>More to Study:</h4><p>To deepen your understanding and explore further, here are some valuable resources:</p><ul><li>The <a href="https://go.dev/ref/spec">Go Programming Language Specification</a></li><li><a href="https://pkg.go.dev/">Go Standard Library Documentation</a></li><li>A Look at <a href="https://medium.com/eureka-engineering/a-look-at-iterators-in-go-f8e86062937c">Iterators in Go</a></li><li>Leveraging <a href="https://engineering.teknasyon.com/leveraging-gos-iterator-pattern-30b7d3be783f">Go’s Iterator Pattern</a></li><li><a href="https://yuminlee2.medium.com/golang-deep-and-shallow-copy-a-slice-7dbcd9eb044a">Golang Deep and Shallow Copy a Slice</a></li><li><a href="https://medium.com/breaking-tech/internals-of-go-317e60fae1dc">Internals of Go</a></li><li><a href="https://medium.com/sellerapp/golang-project-structuring-ben-johnson-way-2a11035f94bc">Golang Project Structuring — Ben Johnson Way</a></li><li>Building and Consuming <a href="https://medium.com/@ansujain/building-and-consuming-custom-packages-in-go-a-complete-guide-ee6566bf6782">Custom Packages in Go</a></li><li><a href="https://dev.to/cduggn/package-management-in-go-2hjl">Package Management in Go</a></li><li><a href="https://dev.to/cduggn/go-build-options-570e">Go Build Options</a></li></ul><p>Do check out my other blogs and follow me on Github for the latest on tech.</p><p>Find The Blog Series Here:</p><ol><li>Part 2 — <a href="https://medium.com/@bhaumikmaan/golang-fundamentals-102-beyond-the-basics-7f4161f75daf">GoLang Fundamentals 102: Beyond the Basics</a></li><li>Part 3 — <a href="https://medium.com/@bhaumikmaan/golang-advanced-103-deep-dive-into-gos-power-features-2e75788e9e68">GoLang Advanced 103: Deep Dive into Go’s Power Features</a></li></ol><p>Medium: <a href="https://medium.com/@bhaumikmaan">Bhaumik Maan</a> || Github: <a href="https://github.com/bhaumikmaan">bhaumikmaan</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cc420f7747a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[KAFKA Basics 102]]></title>
            <link>https://medium.com/@bhaumikmaan/kafka-basics-102-955be11fb63b?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/955be11fb63b</guid>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[apache]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Thu, 29 May 2025 03:32:32 GMT</pubDate>
            <atom:updated>2025-05-29T03:32:32.070Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A_Itbw5xfinl_ZOr.png" /></figure><p>Welcome back to our Kafka journey! In the previous post, we laid the groundwork, exploring Kafka’s core generics, how producers reliably send messages, and how consumers efficiently process them. Now that we’ve got the fundamentals down, it’s time to delve deeper into the operational heart of Kafka and get our hands dirty. In this installment, we’ll unravel the crucial role of Kafka brokers, explore the powerful world of real-time processing with Kafka Streams, highlight some go-to configurations, and finally, roll up our sleeves with essential hands-on CLI commands to interact with our Kafka cluster. Let’s get started!</p><blockquote>Previous Blog: <a href="https://medium.com/@bhaumikmaan/kafka-basics-101-dbd7c2d3a740">KAFKA Basics 101</a><br>Course Followed: <a href="https://www.udemy.com/course/apache-kafka/?srsltid=AfmBOoqfxef-fAqWnwCNlGA5j8SnykPfIExFnjvN_bswEfc2SI4XY-zN">Udemy</a> || Visit their website: <a href="https://learn.conduktor.io/kafka/">Website</a><br><a href="https://github.com/bhaumikmaan/Understanding-Kafka-Basics">Github Repository: Implementation of Course Examples</a></blockquote><h3>The Backbone of the Cluster: Understanding Kafka Brokers</h3><p>At the core of any Apache Kafka deployment is the <strong>Kafka cluster</strong>, a group of one or more servers, each called a <strong>Kafka broker</strong>. These brokers are fundamental to Kafka’s operation, handling message reception, storage, and delivery.</p><ul><li><strong>Bootstrap Servers</strong>: Your applications connect to the Kafka cluster by initially contacting one or more of these brokers, known as bootstrap servers.</li><li><strong>Cluster Discovery</strong>: Once connected to a bootstrap broker, clients automatically discover all other brokers, topics, and partitions, as each broker holds metadata about the entire cluster.</li></ul><p>Brokers are responsible for hosting the <strong>partitions</strong> of your topics. For each partition, the cluster designates one broker as the <strong>leader</strong>.</p><ul><li><strong>Partition Leader</strong>: This is the <em>only</em> broker to which producers can send data for a specific partition.</li><li><strong>Followers</strong>: Other brokers holding copies of the partition’s data are called followers.</li></ul><p>To ensure data durability and high availability, Kafka employs <strong>replication</strong>.</p><ul><li><strong>Replication Factor</strong>: This setting defines how many copies (replicas) of a topic’s partition are maintained across different brokers. For example, a replication factor of 3 means one leader and two followers.</li><li><strong>Data Redundancy</strong>: Followers continuously replicate data from their partition’s leader. If a leader broker fails, a follower is elected as the new leader, ensuring data remains available.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e2eXPhkESgu5i_OCdOo1tg.png" /><figcaption>Kafka Broker architecture</figcaption></figure><h4>Broker Management &amp; Critical Configurations</h4><p>Kafka brokers require coordination and careful configuration for optimal performance and resilience. Historically, this coordination has been the job of Apache Zookeeper, but this is evolving.</p><p><strong>Apache Zookeeper</strong> has traditionally played a crucial part in managing Kafka clusters.</p><ul><li>It keeps track of which brokers are active in the cluster.</li><li>It sends notifications to Kafka clients and brokers about any changes, such as brokers joining or leaving the cluster, or topics being created or deleted.</li><li>Crucially, Zookeeper performs <strong>leader elections</strong> for partitions, deciding which broker replica will be the leader if an existing leader fails.</li></ul><p>However, the dependency on Zookeeper is changing:</p><ul><li>Kafka <strong>2.x versions require Zookeeper</strong> to function.</li><li>Starting with Kafka <strong>3.x, clusters can run in KRaft (Kafka Raft) mode</strong>, which allows Kafka to operate <strong>without Zookeeper</strong>.</li><li>The plan is for Kafka <strong>4.x to completely remove Zookeeper</strong> dependency.</li></ul><p><strong>KRaft</strong> (Kafka Raft Consensus Protocol) is Kafka’s own consensus mechanism designed to replace Zookeeper.</p><ul><li>It addresses <strong>scaling limitations</strong> observed with Zookeeper, particularly in very large clusters (e.g., those with over 100,000 partitions).</li><li>In KRaft mode, a subset of brokers, designated as controllers, form a <strong>quorum</strong>. One of these controller brokers is elected as the <strong>quorum leader</strong>, taking over the cluster metadata management responsibilities previously handled by Zookeeper</li></ul><h3>Beyond Basic Piping: Kafka Streams and Schema Management</h3><p>While Kafka producers and consumers are powerful for getting data into and out of Kafka, many use cases require processing or transforming this data in real-time as it flows through the topics. This is where <strong>Kafka Streams</strong> comes in.</p><p><strong>Kafka Streams</strong> is a client library, not a separate cluster, that allows you to build sophisticated stream processing applications and microservices directly within your Java or Scala applications.</p><ul><li><strong>Higher-Level Abstraction</strong>: It provides a more expressive API than the standard producer/consumer clients, designed specifically for data processing tasks like filtering, mapping, aggregating, joining, and windowing event streams.</li><li><strong>Simplified Processing</strong>: You can define complex data transformation logic that reads from source topics, processes the data, and then writes the results back to other Kafka topics or makes it available for querying.</li><li><strong>Kafka-Native</strong>: Because it’s built on top of Kafka’s core components, it integrates seamlessly and leverages Kafka’s scalability and fault tolerance.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gjWH0tqL3IladPV9Oc3W_A.png" /><figcaption>Kafka Streams Visualization</figcaption></figure><h4>Ensuring Data Integrity: The Kafka Schema Registry</h4><p>When you’re processing streams of data, especially as applications evolve, it’s crucial to ensure that the structure (or “schema”) of your data is consistent and understood by all components. This is where a <strong>Kafka Schema Registry </strong>becomes invaluable.</p><p>The Schema Registry is a centralized service that manages your data schemas (e.g., Avro, JSON Schema, Protobuf). It acts as a contract for the data format, ensuring that producers and consumers agree on the structure of messages.</p><ul><li><strong>Data Validation</strong>: It ensures that data produced to a topic adheres to a predefined schema, rejecting non-compliant “bad data” before it can cause issues downstream.</li><li><strong>Producer &amp; Consumer Interaction</strong>: Both producers and consumers communicate with the Schema Registry.<br>- Producers register schemas and then, when sending messages, often include only a schema ID with the message, reducing payload size.<br>- Consumers use this ID to fetch the schema from the registry to correctly deserialize the message.</li><li><strong>Schema Evolution</strong>: A key feature is support for <strong>schema evolution</strong>. As your data requirements change (e.g., adding a new field), the Schema Registry helps manage these changes with compatibility rules (like backward or forward compatibility), ensuring that new producers can still work with older consumers, or vice versa, without breaking the data pipeline.</li><li><strong>Lightweight Design</strong>: While powerful, schema registries are designed to be efficient and not impose significant overhead on your Kafka infrastructure.</li></ul><p>By using Kafka Streams for processing and a Schema Registry for governance, you can build robust, adaptable, and reliable real-time data pipelines.</p><h3>Choosing the Right Values: Partitions and Replication Factor</h3><p>Two of the most critical decisions when designing your Kafka topics are determining the <strong>number of partitions</strong> and the <strong>replication factor</strong>.</p><p><strong>Partitions</strong>: The number of partitions for a topic impacts its parallelism and throughput.</p><ul><li><strong>More partitions can lead to</strong>:<br>1. Better parallelism, as more consumers in a group can read concurrently.<br>2. Higher overall throughput for the topic.<br>3. Ability to leverage more brokers in the cluster.</li><li><strong>However, consider</strong>:<br>1. Each partition corresponds to open file handles on the Kafka brokers.<br>2. A very large number of partitions can increase the time for leader elections, especially in Zookeeper-based clusters.</li></ul><p><strong>Replication Factor (RF)</strong>: This determines how many copies of your data are stored across the cluster.</p><ul><li><strong>Recommendations</strong>:<br>1. A replication factor of <strong>at least 2</strong> is generally advised for production to avoid data loss if one broker fails.<br>2. <strong>A common value is 3</strong>, providing a good balance between durability and resource usage.<br>3. A maximum of <strong>4 is often sufficient</strong> for most high-availability needs.</li><li><strong>Impact</strong>:<br>1. <strong>Higher RF</strong>: Improves data durability and availability (the topic can tolerate more broker failures).<br>2. <strong>Trade-offs</strong>: Leads to increased network traffic for replication and consumes more disk space across the cluster.</li></ul><h3>Getting Hands-On: Kafka CLI Commands</h3><p>Now that we’ve covered a lot of Kafka theory, it’s time to interact with Kafka directly using its powerful Command Line Interface (CLI) tools. These tools are invaluable for developers and administrators for managing topics, producing/consuming messages for testing, and inspecting the state of the cluster.</p><blockquote><strong>A Quick Note on Paths</strong>: The Kafka CLI tools are scripts (usually ending in .sh) located in the bin/ directory of your Kafka installation. Your exact path to these scripts might vary. For example, you might use kafka-topics.sh or, if your bin directory is in your system&#39;s PATH, just kafka-topics. The examples below use kafka-topics, kafka-console-producer, etc., assuming these scripts are accessible in your PATH. Adjust as per your environment.</blockquote><ul><li><strong>Starting the Kafka Server</strong><br>Before you can run any commands against Kafka topics or producers/consumers, your Kafka server (broker) needs to be running.</li></ul><pre># Example command to start a Kafka server<br># The path to kafka-server-start and server.properties will depend on your installation<br># For instance, if using Homebrew on macOS:<br>/opt/homebrew/bin/kafka-server-start /opt/homebrew/etc/kafka/server.properties<br><br># A more generic representation:<br>&lt;path_to_kafka_home&gt;/bin/kafka-server-start.sh &lt;path_to_kafka_home&gt;/config/server.properties</pre><ul><li><strong>Managing Kafka Topics via CLI</strong> (kafka-topics)<br>The kafka-topics command is your go-to tool for all operations related to topics. You can list, create, describe, alter, and delete topics. <strong>Common Actions</strong>:<br>1. --list: Shows all available topics. <br>2. --describe: Displays details of one or more topics (like partition count, leader, replicas).<br>3. --create: Creates a new topic.<br>4. --alter: Modifies an existing topic (e.g., changing partition count).<br>5. --delete: Removes a topic.</li></ul><p>Let’s see some examples. We’ll assume your Kafka broker is running on localhost:9092.</p><pre># List all Kafka topics<br>kafka-topics --bootstrap-server localhost:9092 --list<br><br># Create a topic named &quot;first_topic&quot; (defaults to 1 partition if not specified otherwise in broker config)<br>kafka-topics --bootstrap-server localhost:9092 --topic first_topic --create<br><br># Create a topic with a specific number of partitions<br>kafka-topics --bootstrap-server localhost:9092 --topic second_topic --create --partitions 5<br><br># Describe the &quot;second_topic&quot; to see its configuration<br>kafka-topics --bootstrap-server localhost:9092 --topic second_topic --describe<br><br># Attempt to create a topic with replication factor &gt; number of available brokers (will fail on a single-node cluster)<br># This command assumes only 1 broker is running.<br>kafka-topics --bootstrap-server localhost:9092 --topic third_topic --create --partitions 3 --replication-factor 2<br># Expected output: Error due to insufficient brokers for replication factor 2<br><br># Create a topic with replication factor 1 (works on a single-node cluster)<br>kafka-topics --bootstrap-server localhost:9092 --topic third_topic --create --partitions 3 --replication-factor 1<br># Describe it to confirm<br>kafka-topics --bootstrap-server localhost:9092 --topic third_topic --describe<br><br># Delete a topic (requires &#39;delete.topic.enable=true&#39; in your server.properties)<br>kafka-topics --bootstrap-server localhost:9092 --topic first_topic --delete</pre><p><strong>Using a Command Config File</strong>: For more complex setups, especially when connecting to secure clusters requiring SSL or SASL configurations, you can pass a properties file to the CLI tools.</p><pre>kafka-topics --command-config /path/to/your_config_file.config --bootstrap-server &lt;your_broker_address&gt;:9092 --list</pre><ul><li><strong>Producing Messages via CLI</strong> (kafka-console-producer)<br>The kafka-console-producer tool lets you send messages to a Kafka topic directly from your terminal. This is very handy for quick tests.</li></ul><pre># Start producing messages to &quot;first_topic&quot;. Type messages and press Enter. Use Ctrl+C to exit.<br>kafka-console-producer --bootstrap-server localhost:9092 --topic first_topic<br>&gt; Hello World<br>&gt; My name is Conduktor<br>&gt; I love Kafka<br>&gt; ^C<br><br># Produce messages with specific properties, e.g., requiring acks from all replicas<br>kafka-console-producer --bootstrap-server localhost:9092 --topic first_topic --producer-property acks=all<br>&gt; some message that is acked<br>&gt; ^C<br><br># Producing to a non-existing topic:<br># If auto.create.topics.enable=true (default is often true) in server.properties, this will create &#39;new_topic&#39;.<br>kafka-console-producer --bootstrap-server localhost:9092 --topic new_topic<br>&gt; hello world!<br>&gt; ^C<br><br># Check the new topic (it will likely have default partition count from server.properties, or 1)<br>kafka-topics --bootstrap-server localhost:9092 --describe --topic new_topic<br><br># Tip: It&#39;s generally better to explicitly create topics with desired partition/replication settings.<br># If you want new auto-created topics to have a different default number of partitions,<br># you can edit &#39;num.partitions&#39; in your config/server.properties (or config/kraft/server.properties)<br># and restart the broker. Then try producing to another new topic:<br>kafka-console-producer --bootstrap-server localhost:9092 --topic new_topic_2<br>kafka-topics --bootstrap-server localhost:9092 --describe --topic new_topic_2<br><br># Produce messages with keys. Each message is key:value.<br># This tells the producer to interpret the input string with a separator for the key.<br>kafka-console-producer --bootstrap-server localhost:9092 --topic first_topic --property parse.key=true --property key.separator=:<br>&gt; example_key:example_value<br>&gt; user_id_123:User clicked a button<br>&gt; ^C</pre><ul><li><strong>Consuming Messages via CLI</strong> (kafka-console-consumer)<br>To read messages from a topic, use kafka-console-consumer</li></ul><pre># Ensure &#39;second_topic&#39; exists, perhaps with multiple partitions<br>kafka-topics --bootstrap-server localhost:9092 --topic second_topic --create --partitions 3 --replication-factor 1<br><br># Start consuming messages from &quot;second_topic&quot;<br># This will only show messages produced after the consumer starts.<br>kafka-console-consumer --bootstrap-server localhost:9092 --topic second_topic<br><br># To consume all messages from the beginning of the topic:<br>kafka-console-consumer --bootstrap-server localhost:9092 --topic second_topic --from-beginning<br># Note: If &#39;second_topic&#39; has multiple partitions, messages from different partitions might appear interleaved,<br># but messages within the same partition will always be in order.<br><br># Display key, value, timestamp, and partition for each message:<br>kafka-console-consumer --bootstrap-server localhost:9092 --topic second_topic --formatter kafka.tools.DefaultMessageFormatter --property print.timestamp=true --property print.key=true --property print.value=true --property print.partition=true --from-beginning</pre><ul><li><strong>Kafka Console Consumers in Groups via CLI<br></strong>Consumers work in groups. You can simulate this with the console consumer</li></ul><pre># First, ensure &#39;third_topic&#39; exists, e.g., with 3 partitions:<br>kafka-topics --bootstrap-server localhost:9092 --topic third_topic --create --partitions 3 --replication-factor 1<br><br># Terminal 1: Start a consumer in a group<br>kafka-console-consumer --bootstrap-server localhost:9092 --topic third_topic --group my-first-application<br><br># Terminal 2: Start a producer and send some messages to &#39;third_topic&#39;<br>kafka-console-producer --bootstrap-server localhost:9092 --topic third_topic<br>&gt; message 1<br>&gt; message 2<br>&gt; message 3<br>&gt; message 4<br>&gt; message 5<br>&gt; message 6<br><br># Terminal 3: Start another consumer in the SAME group &#39;my-first-application&#39;<br>kafka-console-consumer --bootstrap-server localhost:9092 --topic third_topic --group my-first-application<br># You&#39;ll see messages being distributed between the two consumers in Terminal 1 and Terminal 3.<br># Each partition is consumed by only one consumer instance within the group.<br><br># Terminal 4: Start a consumer in a DIFFERENT group, reading from the beginning<br>kafka-console-consumer --bootstrap-server localhost:9092 --topic third_topic --group my-second-application --from-beginning<br># This group will receive all messages independently of &#39;my-first-application&#39;.</pre><ul><li><strong>Consumer Group Management via CLI</strong> (kafka-consumer-groups)<br>The kafka-consumer-groups tool allows you to inspect and manage consumer groups.</li></ul><pre># List all consumer groups<br>kafka-consumer-groups --bootstrap-server localhost:9092 --list<br><br># Describe a specific consumer group to see its members, assigned partitions, and lag<br>kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group my-first-application<br># The &#39;CURRENT-OFFSET&#39; is the last committed offset for a partition.<br># The &#39;LOG-END-OFFSET&#39; is the offset of the latest message in the partition.<br># &#39;LAG&#39; is the difference (LOG-END-OFFSET - CURRENT-OFFSET), indicating how many messages the group is behind.<br><br># If you run a console consumer, it creates a group often named like &#39;console-consumer-XXXXX&#39;.<br># You can find its name using --list and then describe it.<br>kafka-console-consumer --bootstrap-server localhost:9092 --topic third_topic --group my-test-group<br>kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group my-test-group</pre><ul><li><strong>Resetting Consumer Group Offsets via CLI<br></strong>Sometimes you need to reprocess data. You can reset a consumer group’s offsets to an earlier point.</li></ul><pre># First, let&#39;s describe our group to see current offsets (assuming it has consumed some messages from &#39;third_topic&#39;)<br>kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group my-first-application<br><br># Dry Run: Show what the reset operation would do without actually doing it.<br># Reset offsets for &#39;third_topic&#39; for group &#39;my-first-application&#39; to the earliest available offset.<br>kafka-consumer-groups --bootstrap-server localhost:9092 --group my-first-application --reset-offsets --to-earliest --topic third_topic --dry-run<br><br># Execute the reset operation:<br>kafka-consumer-groups --bootstrap-server localhost:9092 --group my-first-application --reset-offsets --to-earliest --topic third_topic --execute<br><br># Describe the group again to see the new offsets (should be at the beginning)<br>kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group my-first-application<br><br># Now, if you start a consumer from this group, it will read messages from the beginning of &#39;third_topic&#39; again.<br>kafka-console-consumer --bootstrap-server localhost:9092 --topic third_topic --group my-first-application<br><br># After it consumes some messages, describe the group again to see offsets advancing.<br>kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group my-first-application</pre><h3>Peeking Under the Hood: Advanced Kafka Concepts &amp; Configurations</h3><p>Beyond the everyday operations, Kafka has a rich set of configurations and internal mechanisms that allow for fine-tuning, specialized use cases, and deeper understanding of its behavior.</p><h4>Advanced Topic Configurations with kafka-configs</h4><p>While many common topic settings like partition count and replication factor are set during creation with kafka-topics, Kafka offers a wide array of other dynamic configurations that can be applied per topic. The kafka-configs CLI tool is used for this.</p><ul><li>You can describe existing configurations or alter them.</li><li>This allows you to override broker-level defaults for specific topics.</li><li>Other configurations you might manage include retention.ms, cleanup.policy, segment.bytes, etc</li></ul><pre># Describe all dynamic configurations for a topic named &#39;configured-topic&#39;<br># (Note: Ensure your bootstrap server address and port are correct, e.g., localhost:9092)<br>kafka-configs --bootstrap-server localhost:9092 --entity-type topics --entity-name configured-topic --describe<br><br># Alter a topic&#39;s configuration: Add/update min.insync.replicas for &#39;configured-topic&#39;<br># This requires at least 2 replicas to be in sync for a producer with acks=all to succeed.<br>kafka-configs --bootstrap-server localhost:9092 --entity-type topics --entity-name configured-topic --alter --add-config min.insync.replicas=2</pre><h4>Understanding Log Segments</h4><p>We know topics are divided into partitions. But how is data stored within a partition on a broker’s disk? Partitions are actually made up of <strong>log segments</strong>, which are individual files.</p><ul><li><strong>Appending Data</strong>: Data is always appended to the <em>active segment</em> file for a partition.</li><li><strong>Segment Rolling</strong>: Once a segment reaches a certain size or age, it’s closed, and a new active segment is created. This is controlled by:<br>1. log.segment.bytes: The maximum size (in bytes) of a single segment file before it&#39;s closed (e.g., 1GB).<br>2. log.roll.ms (or log.roll.hours): The maximum time a segment remains open before being rolled, even if it hasn&#39;t reached its size limit.</li><li><strong>Indices</strong>: Each segment has associated index files (one for offsets, another for timestamps) that help Kafka quickly locate messages within that segment.</li></ul><p>This segmentation strategy helps with efficient data management, cleanup, and indexing.</p><h4>Log Compaction: Keeping a Snapshot</h4><p>By default, Kafka retains data in topics for a configured period (e.g., 7 days, based on retention.ms) or size (retention.bytes). However, for some use cases, you might only care about the <em>latest value for each message key</em>. This is where <strong>log compaction</strong> comes in.</p><ul><li><strong>How it Works</strong>: For a compacted topic, Kafka ensures that it retains at least the last known value for each unique message key within a partition. Older messages with the same key are eventually cleaned up.</li><li><strong>Use Cases</strong>:<br>1. Maintaining a snapshot of state (e.g., the latest user profile update, current product inventory).<br>2.Change data capture (CDC) streams.</li><li><strong>Key Points</strong>:<br>1. Message order is still guaranteed for new messages (when tailing the log).<br>2. Compaction happens in the background on closed segments.<br>3. Messages with null values are treated as &quot;tombstones&quot; and can lead to the deletion of all previous messages for that key.</li><li><strong>Further Reading</strong>: For a detailed explanation, check out Conduktor’s article: <a href="https://learn.conduktor.io/kafka/kafka-topic-configuration-log-compaction/">Kafka Topic Configuration: Log Compaction</a></li></ul><p>To enable compaction, you set the topic’s cleanup.policy to compact. You might also configure it with delete (e.g., cleanup.policy=compact,delete) to both compact and eventually delete old data based on retention time if needed.</p><h4>Handling Large Messages in Kafka</h4><p>Kafka is optimized for relatively small messages. The default maximum message size (controlled by message.max.byteson the broker and max.request.size on the producer) is typically <strong>1MB</strong>. Sending messages significantly larger than this can strain broker resources and impact performance. If you need to handle large data objects:</p><ul><li><strong>Store Externally, Send a Reference</strong>: This is the most common and recommended approach.<br>1. Upload the large file/object to an external storage system (e.g., S3, HDFS, a database).<br>2. Send a message to Kafka containing only a <em>reference</em> (like a URL or an ID) to this external object.<br>3. Consumers then use this reference to fetch the large object directly from the external store.</li><li><strong>Change Kafka Configurations (with caution)</strong>: You <em>can</em> increase Kafka’s limits for message size. This involves several settings on brokers, topics, producers, and consumers:<br>1. Broker: message.max.bytes, replica.fetch.max.bytes.<br>2. Topic: max.message.bytes (can override broker default).<br>3. Producer: max.request.size.<br>4. Consumer: fetch.message.max.bytes.</li><li><strong>Warning</strong>: Significantly increasing these values can lead to performance issues, increased memory usage on brokers and clients, and longer GC pauses. It should be done carefully and with thorough testing</li></ul><h3>Important Links &amp; Further Learning</h3><p>Here are some valuable resources mentioned, which can help you dive deeper into specific Kafka topics and see it in action:</p><ul><li><strong>Topic Naming Conventions</strong>: A practical guide on how to choose meaningful and consistent topic names: <a href="https://cnr.sh/posts/2017-08-29-how-paint-bike-shed-kafka-topic-naming-conventions/">How to paint the bike shed — Kafka Topic Naming Conventions</a></li><li><strong>Kafka Monitoring</strong>: Official documentation on metrics and approaches for monitoring your Kafka cluster: <a href="https://kafka.apache.org/documentation/#monitoring">Apache Kafka Documentation — Monitoring</a></li><li><strong>Real-time Data Streams (Examples)</strong>:<br>1. Wikimedia Recent Changes Stream: <a href="https://stream.wikimedia.org/v2/stream/recentchange">Wikimedia EventStreams</a><br>2. Wikimedia EventSource Demo 1: <a href="https://codepen.io/Krinkle/pen/BwEKgW?editors=1010">Codepen Demo</a><br>3. Wikimedia EventSource Demo 2: <a href="https://esjewett.github.io/wm-eventsource-demo/">GitHub Demo</a></li><li><strong>OpenSearch Quickstart</strong>: If you’re looking to integrate Kafka with OpenSearch for analytics: <a href="https://docs.opensearch.org/docs/latest/#docker-quickstart">OpenSearch Docker Quickstart</a></li><li><strong>Kafka Connectors</strong>: Explore pre-built connectors for integrating Kafka with various other systems: <a href="https://www.confluent.io/product/connectors/">Confluent Hub — Kafka Connectors</a></li></ul><p>We’ve covered a lot of ground in this post! From understanding the crucial role of Kafka brokers and the shift towards KRaft, to exploring the power of Kafka Streams and Schema Registry for real-time processing. We then understood essential CLI commands for managing topics, producing/consuming messages, and overseeing consumer groups. Finally, we touched upon advanced configurations like log compaction and handling large messages.</p><p>Hopefully, this deeper dive has solidified your understanding and equipped you to work more effectively with Kafka. There’s always more to learn in the vast Kafka ecosystem, but these building blocks are key to mastering event streaming!</p><p>Do check out my other blogs and follow me on Github for the latest on tech. <br>Medium: <a href="https://medium.com/@bhaumikmaan">Bhaumik Maan</a> || Github: <a href="https://github.com/bhaumikmaan">bhaumikmaan</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=955be11fb63b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[KAFKA Basics 101]]></title>
            <link>https://medium.com/@bhaumikmaan/kafka-basics-101-dbd7c2d3a740?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbd7c2d3a740</guid>
            <category><![CDATA[apache]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Wed, 28 May 2025 03:31:28 GMT</pubDate>
            <atom:updated>2025-05-28T03:31:28.520Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A_Itbw5xfinl_ZOr.png" /></figure><p>In today’s data-driven world, the ability to handle vast streams of information in real-time isn’t just a bonus; it’s a necessity. If you’ve ever wondered how companies manage massive flows of messages, from website clicks to sensor data, chances are Kafka is working its magic behind the scenes. This blog post is my way of consolidating what I’ve learned and sharing the core concepts of Kafka that I found most fascinating. It will be a two-part series. Let’s dive in!</p><blockquote>Course Followed: <a href="https://www.udemy.com/course/apache-kafka/?srsltid=AfmBOoqfxef-fAqWnwCNlGA5j8SnykPfIExFnjvN_bswEfc2SI4XY-zN">Udemy</a> || Visit their website: <a href="https://learn.conduktor.io/kafka/">Website</a><br><a href="https://github.com/bhaumikmaan/Understanding-Kafka-Basics">Github Repository: Implementation of Course Examples</a></blockquote><h3>Kafka 101: Understanding the Core Components</h3><p>Before you can harness the power of Kafka, it’s crucial to get a grip on its fundamental building blocks. At its heart, Kafka is designed for high-throughput, fault-tolerant, and scalable event streaming. One of the first things to understand is a core principle: data in Kafka is <strong>immutable</strong>. Once data is written, it cannot be changed, ensuring data integrity and simplifying many aspects of its operation.</p><p>Let’s break down the key terms:</p><ul><li><strong>Topics</strong>: These are named streams of records. Think of them like categories or feeds where related messages are published. For instance, a website_clicks topic would hold all click data.</li><li><strong>Partitions</strong>: Topics are broken down into <strong>partitions</strong>. These are the actual storage units. Dividing topics into partitions allows Kafka to scale and handle data in parallel. Data within a partition is <strong>immutable</strong> and typically stored for a limited time.</li><li><strong>Offsets</strong>: Messages within each partition are <strong>ordered</strong>. Each message gets an incremental ID called an <strong>offset</strong>. This offset uniquely identifies a message within its partition and defines its sequence (e.g., offset 0 is before offset 1).</li><li><strong>Kafka Data Streams</strong>: This term simply refers to the continuous flow of messages through Kafka topics.</li><li><strong>Producers and Consumers</strong>: You don’t query topics directly.<br>1. <strong>Kafka Producers</strong> are client applications that write/send messages to Kafka topics.<br>2. <strong>Kafka Consumers</strong> are client applications that read/subscribe to messages from Kafka topics.</li></ul><p>Understanding these fundamental <em>generics</em> is the first step to appreciating the elegance and power of Apache Kafka. They form the foundation upon which all its advanced features and capabilities are built.</p><h3>Sending the Signals: A Deep Dive into Kafka Producers</h3><p>Now that we know about topics and partitions, how does data actually get into them? That’s where <strong>Kafka Producers </strong>come into play. These are client applications responsible for writing streams of records to the Kafka cluster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U5oec27paRiRBkcoKo_XGw.png" /><figcaption>Kafka Producers Flow</figcaption></figure><h4>Directing Traffic: Keys, Partitions, and the Partitioner</h4><p>Producers are smart; they don’t just blindly fire off messages. They need to know <em>which partition</em> within a topic to write to. This decision is crucial for data distribution and retrieval.</p><ul><li><strong>Message Keys</strong>: Producers can send messages with an optional <strong>key<br>- </strong><strong>null key</strong>: Messages are typically sent <strong>round-robin</strong> across topic partitions for even distribution.<br>- <strong>Not-Null</strong> <strong>key</strong>: Messages with the <strong>same key always go to the same partition</strong>. This is done by hashing the key and is vital for ordered processing of related messages.</li><li><strong>Kafka Partitioner</strong>: This is the component containing the logic to assign messages to partitions. Default behavior uses key hashing, but custom partitioners are possible.</li><li><strong>Broker Failure</strong>: Producers are designed to <strong>auto-recover</strong> from Kafka broker failures, redirecting writes to available brokers, contributing to Kafka’s fault tolerance.</li></ul><h4>Ensuring Data Delivery: Acknowledgments (acks)</h4><p>Producers can configure the level of <strong>acknowledgment</strong> they require from the Kafka broker after writing a message, balancing durability and throughput:</p><ul><li><strong>acks=0 (Fire and Forget — No Ack)</strong>: Producer sends data without waiting for broker confirmation. Offers lowest latency but highest risk of data loss if the broker fails before writing.</li><li><strong>acks=1 (Leader Ack - Default for Kafka v1.0 - v2.8)</strong>: Producer waits for the partition leader to acknowledge the write. Data can be lost if the leader fails before replication to followers.</li><li><strong>acks=all (or </strong><strong>-1) (Leader + Replicas Ack - Default for Kafka v3.0+)</strong>: Producer waits for the leader and all in-sync replicas to acknowledge. This provides the strongest data safety guarantee.</li></ul><p>Choosing the right acks setting is a trade-off between performance (lower acks is faster) and data safety (higher acks is safer).</p><h4>Handling Failures: Retries and Idempotence</h4><p>Distributed systems face transient errors, like what if a producer sends data, the broker commits it, but the acknowledgment back to the producer is lost due to a network error? The producers handle as follows:</p><ul><li><strong>Retries</strong>: Producers automatically <strong>retry sending messages</strong> upon encountering recoverable errors. Kafka &gt;= 2.1 defaults to a very high number of retries (2147483647) but within a delivery timeout (default 2 mins). Retries can potentially lead to message reordering within a batch if an earlier message fails and is retried after later messages succeed.</li><li><strong>Idempotent Producers (Preventing Duplicates)</strong>: Network errors can cause producers to retry sending messages that were already committed but whose acknowledgment was lost, leading to duplicates.</li><li>Idempotent producers (default since Kafka 3.0) prevent this. By setting enable.idempotence=true, each message gets a sequence number, allowing brokers to detect and discard duplicates. This setting implies acks=all and high retries.</li></ul><h4>Optimizing Producer Performance</h4><p>Several factors contribute to how efficiently producers can send data:</p><ul><li><strong>Message Serialization</strong>: Kafka brokers expect messages as <strong>byte arrays</strong>. Producers (and <em>consumers</em>) use <strong>serializers</strong> (and <em>deserializers</em>) to convert your application’s objects (e.g., JSON, Avro) into bytes before sending them over the network, and <em>vice-versa</em>.</li><li><strong>Message Compression</strong>: Compressing messages (especially text like JSON) at the producer reduces network load and storage requirements, improving throughput and latency. Common codecs include Gzip, Snappy, LZ4, and ZSTD. The larger the batch, the better the compression.</li><li><strong>Batching</strong>: By default, Kafka producers try to send records as soon as possible. However, sending messages one by one can be inefficient. Producers can <strong>batch messages</strong> together destined for the same partition. Key settings for batching:<br>1. linger.ms (default 0ms): Time a producer waits to fill a batch. Higher values increase latency but improve throughput.<br>2. batch.size (default 16KB): Maximum batch size in bytes. Batches are sent if this size is reached before linger.ms</li><li><strong>Sticky Partitioning</strong>: For messages without keys, producers often use “sticky partitioning.” They pick a partition and send the current batch there, then switch for the next batch. This creates larger, more efficiently compressed batches.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AXzKUUjXNGpeMcNf.png" /><figcaption>Sticky Partitioning</figcaption></figure><h4>Managing Producer Buffer</h4><ul><li>Producers have an internal buffer (buffer.memory, default 32MB) to hold records that are waiting to be sent. If the producer is generating messages faster than they can be transmitted to the broker (e.g., due to network latency or broker load), this buffer will fill up.</li><li>Once the buffer is full, calls to the send() method can block for a duration specified by max.block.ms. If the buffer doesn&#39;t free up within this time, an exception is thrown.</li></ul><p>By understanding and configuring these aspects, you can build robust and high-performing Kafka producers tailored to your application’s specific needs for data integrity and throughput.</p><h3>Receiving the Data: Understanding Kafka Consumers</h3><p>Once data is in Kafka topics, <strong>Kafka Consumers</strong> are the applications responsible for reading and processing it. They operate on a <strong>pull model</strong>, meaning they actively fetch data from the brokers. Within each partition, consumers read messages in the order they were stored, from lower to higher <strong>offsets</strong>. It’s also crucial that consumers use the same <strong>deserializer</strong> (e.g., for JSON, Avro) that the producer used to serialize the data, ensuring the data can be correctly interpreted</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u850EWbDKLrWe9XpHj7RNg.png" /><figcaption>Kafka Consumer and Consumer Groups</figcaption></figure><h4>Teamwork: Consumer Groups and Parallel Processing</h4><p>Kafka achieves high throughput and parallel processing for consumers through <strong>consumer groups</strong>. A consumer group consists of one or more consumer instances that jointly subscribe to one or more topics. Kafka ensures that each partition of a topic is consumed by <strong>only one consumer instance within a particular group</strong>. This way, the load for a topic is distributed among the members of the group.</p><p>It’s perfectly fine, and often common, to have <strong>multiple different consumer groups reading from the same topic independently</strong>. For example, one group might be for real-time analytics, while another is for archiving data to a data lake, both consuming the same stream of events without interfering with each other.</p><h4>Keeping Track: Consumer Offsets</h4><p>To manage what data has been read, Kafka stores the <strong>offsets</strong> for each consumer group. Think of an offset as a bookmark for each partition a group is consuming. When a consumer processes a batch of messages, it periodically <strong>commits these offsets</strong> back to Kafka. This is a critical feature because if a consumer instance fails or is shut down, it (or another instance taking over its partitions) can resume reading from where it left off, thanks to these committed offsets.</p><h4>Delivery Semantics</h4><p>The timing of offset commits defines message delivery guarantees:</p><ul><li><strong>At Least Once</strong>: Offsets are typically committed <em>after</em> the messages are processed. If the consumer processes a message but crashes before committing the offset, that message might be processed again when the consumer restarts (or a new consumer takes over). This requires your message processing logic to be <strong>idempotent</strong>. This is often the case when enable.auto.commit is true.</li><li><strong>At Most Once</strong>: Offsets are committed <em>as soon as</em> messages are received, <em>before</em> processing. If the processing logic then fails, the message will be lost because Kafka thinks it was already successfully processed. This is less common due to the risk of data loss.</li><li><strong>Exactly Once</strong>: This provides the strongest guarantee, ensuring each message is processed precisely one time. Achieving this in Kafka typically involves more advanced configurations, often leveraging Kafka’s transactional capabilities or Kafka Streams.</li></ul><h4>Rebalancing and Partition Assignment</h4><p>When consumers join or leave a group, or when topic metadata changes (like the number of partitions), a <strong>rebalance </strong>occurs. This process redistributes partitions among the active consumers in the group.</p><ul><li><strong>Eager Rebalance</strong>: This was the traditional approach. All consumers in the group temporarily stop processing, give up their assigned partitions, and then partitions are reassigned. While simple, this can cause a noticeable pause in consumption (“stop-the-world” effect).</li><li><strong>Cooperative Rebalance (Incremental Rebalance)</strong>: A more modern approach where only a subset of partitions is moved during each rebalance step. This allows consumers not directly affected by the reassignment to continue processing, leading to less downtime. It might take several iterations to reach a stable assignment.</li><li><strong>Partition Assignment Strategies</strong>: Consumers use a configured strategy (partition.assignment.strategy) to decide how partitions are divided among members. Common strategies include RangeAssignor, RoundRobinAssignor, StickyAssignor (which tries to minimize partition movement across rebalances to keep assignments &quot;sticky&quot;), and CooperativeStickyAssignor (which combines stickiness with cooperative rebalancing). The default is often a list like [RangeAssignor, CooperativeStickyAssignor], allowing Kafka to pick the best available.</li><li><strong>Static Membership</strong>: To reduce rebalances caused by transient restarts, consumers can be configured as static members of a group. If a static consumer leaves, its partitions are not immediately reassigned, allowing it to rejoin and reclaim its previous partitions without triggering a full rebalance, up to a certain timeout.</li></ul><h4>Consumer Health &amp; Key Configurations</h4><p><strong>Offset Committing</strong>:</p><ul><li>enable.auto.commit=true (default): Offsets are committed automatically in the background at regular intervals (auto.commit.interval.ms) or implicitly when you call consumer.poll(), usually leading to at-least-once.</li><li>enable.auto.commit=false: The application takes full responsibility for committing offsets manually. This provides more control, allowing you to commit only after messages have been fully and successfully processed.</li></ul><p><strong>Liveness</strong>: An internal <strong>heartbeat thread</strong> runs within the consumer, sending signals to the Kafka broker (specifically, the Consumer Coordinator) to indicate it’s still alive (default heartbeat interval is 3 seconds). If the broker doesn’t receive a heartbeat within the session.timeout.ms (default 45 seconds), the consumer is considered dead, and its partitions are reassigned</p><p><strong>Polling Behavior</strong>:</p><ul><li>max.poll.interval.ms: Max time between poll() calls. Exceeding this deems the consumer failed.</li><li>max.poll.records: Max records per poll() request, controlling batch processing size.</li></ul><p>And that’s a wrap on the foundational concepts of Kafka, from its core components to how producers and consumers operate! Grasping these building blocks is key to harnessing Kafka’s power. Stay tuned for the next post where we’ll dive into more advanced topics!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbd7c2d3a740" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[One Stop Core Java Guide Pt. 1]]></title>
            <link>https://medium.com/@bhaumikmaan/one-stop-core-java-guide-pt-1-c3900a76657f?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3900a76657f</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java-interview-questions]]></category>
            <category><![CDATA[core-java]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Sat, 28 Dec 2024 02:32:05 GMT</pubDate>
            <atom:updated>2024-12-28T02:32:05.986Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zzRcU3I2mOTtp9b5.png" /></figure><p>In the constantly changing landscape of software development, one thing remains unchanged — the lasting significance of Java. This robust and adaptable language has endured over time, solidifying its position as a key tool for both seasoned developers and those just starting out.</p><p>In this blog series, we’ll break down the core principles of Java from foundational concepts such as JVM and JDK to more advanced topics like synchronization and multithreading, we’ll cover a range of subjects to help build efficient, high-performing applications.</p><p>Essential Java Terminologies:</p><ol><li><strong>Java Virtual Machine(JVM)</strong>: The JVM is an integral part of the Java platform, responsible for executing Java bytecode. It ensures that the output of Java programs is consistent across different platforms.</li><li><strong>Bytecode</strong>: Bytecode is the intermediate representation of Java code, generated by the Java compiler. It is platform-independent and can be executed by the JVM.</li><li><strong>Java Development Kit(JDK)</strong>: It is a complete Java development kit that includes everything including compiler, Java Runtime Environment (JRE), Java Debuggers, Java Docs, etc.</li><li><strong>Garbage Collector</strong>: To delete or recollect that memory JVM has a program called Garbage Collector. Garbage Collectors can recollect the objects that are not referenced.</li><li><strong>Thread &amp; Multi-threading</strong>: A lightweight process that allows concurrent execution of tasks. Java provides a powerful multi-threading mechanism to perform multiple tasks simultaneously.</li><li><strong>Collection Framework &amp; Stream API</strong>: A set of classes and interfaces that implement commonly used data structures (like lists, sets, and maps). Features introduced to allow functional-style operations on sequences of elements.</li></ol><p>In the upcoming blogs, we will explore these topics in detail, along with the various methodologies and functions that Java provides. In this blog, we will go over JVM Architecture and Synchronization in Java</p><h3>JVM Architecture</h3><p><a href="https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html">Java Virtual Machine</a> runs Java applications as a run-time engine. JVM is the one that calls the main method present in a Java code. JVM is a part of JRE(Java Runtime Environment).</p><p>To implement the Java Virtual Machine correctly, you need only be able to read the class file format and correctly perform the operations specified therein</p><blockquote>Compiled code to be executed by the Java Virtual Machine is represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the class file format.</blockquote><blockquote>The class file format precisely defines the representation of a class or interface, including details such as byte ordering that might be taken for granted in a platform-specific object file format. When we compile a .java file, .class files(contains byte-code) with the same class names present in .java file are generated by the Java compiler.</blockquote><p>Java applications are called <strong>WORA</strong> (Write Once Run Anywhere). This means a programmer can develop Java code on one system and expect it to run on any other Java-enabled system without any adjustment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/649/0*RJfvunEGaexKY3PY.png" /><figcaption>JVM Architecture | Source: JavaTPoint</figcaption></figure><h4>JVM Class Loader</h4><ul><li>The Class loader reads the .class file, generate the corresponding binary data and save it in the method area. After loading the .classfile, JVM creates an object of type Class to represent this file in the heap memory.</li><li>JVM allocates memory for class static variables and initializing the memory to default values. Replaces symbolic references from the type with direct references.</li><li>In this phase, all static variables are assigned with their values defined in the code and static block(if any). This is executed from top to bottom in a class and from parent to child in the class hierarchy.</li></ul><blockquote>There are 3 Class Loaders: <br>1. <strong>Bootstrap class loader</strong>: Loads core Java API classes from the JAVA_HOME/lib directory. It is implemented in native code and is not a Java object.<br>2. <strong>Extension class loader</strong>: It is a child of the bootstrap class loader. It loads the classes present in the extensions directories JAVA_HOME/jre/lib/ext<br>3. <strong>System/Application class loader</strong>: It is a child of the extension class loader. It is responsible to load classes from the application classpath. Loads classes from the application classpath, which is specified by the java.class.path environment variable.</blockquote><h4>JVM Memory Areas</h4><ul><li><strong>Method area</strong>: The method area in the Java Virtual Machine (JVM) stores class-level information such as the class name, immediate parent class name, methods, and variable data, including static variables. There is only one method area shared by the entire JVM, meaning it’s a common resource for all classes loaded into the JVM. The method area is analogous to the storage area for compiled code of a conventional language. It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods.</li><li><strong>Heap area:</strong> Information of all objects is stored in the heap area. There is also one Heap Area per JVM. It is also a shared resource. The heap is the run-time data area from which memory for all class instances and arrays is allocated. The heap is created on virtual machine start-up. Heap storage for objects is reclaimed automatically by a garbage collector; objects are never explicitly deallocated. The memory for the heap does not need to be contiguous.</li><li><strong>Stack area:</strong> The JVM creates a separate runtime stack for each thread, which is stored in this area. Each block within the stack is known as an activation record or stack frame, and it contains information about method calls. All local variables of a method are stored in their respective stack frames. Once a thread finishes executing, its runtime stack is discarded by the JVM. This stack is not a shared resource. The run-time constant pool for a class or interface is constructed when the class or interface is created by the Java Virtual Machine.</li><li><strong>PC Registers:</strong> Store address of current execution instruction of a thread. Each thread has separate PC Registers.</li><li><em>Run-Time Constant Pool</em>: A run-time constant pool is a per-class or per-interface run-time representation of the constant_pool table in a class file. It contains several kinds of constants, ranging from numeric literals known at compile-time to method and field references that must be resolved at run-time.</li></ul><h4>JVM Execution Engine</h4><p>Execution engine executes the “<em>.class”</em> (bytecode). It reads the byte-code line by line, uses data and information present in various memory area and executes instructions.</p><ul><li><strong>Interpreter</strong>: It interprets the bytecode line by line and then executes. Multiple iterations are required if method is called multiple times.</li><li><strong>Just-In-Time Compiler(JIT)</strong> : It enhances the performance of an interpreter by converting the entire bytecode into native code. This allows the interpreter to bypass re-interpretation for repeated method calls, as it can directly use the precompiled native code, thereby improving efficiency.</li><li><strong>Garbage Collector</strong>: It destroys un-referenced objects.</li></ul><h4>Java Native Interface</h4><p>It is an interface that interacts with the Native Method Libraries and provides the native libraries(C, C++) required for the execution. It enables JVM to call C/C++ libraries and to be called by C/C++ libraries which may be specific to hardware.</p><h4>Frames</h4><ul><li>A <strong>frame</strong> is used to store data and partial results, as well as to perform dynamic linking, return values for methods, and dispatch exceptions.</li><li>A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes, whether that completion is normal or abrupt (it throws an uncaught exception).</li><li>Frames are allocated from the Java Virtual Machine stack. Each frame has its own array of local variables , its own operand stack, and a reference to the run-time constant pool of the class of the current method.</li></ul><blockquote>Each frame in the JVM has a last-in-first-out (LIFO) <strong>operand stack</strong> with a predetermined maximum depth set at compile-time. The JVM uses this stack for loading constants or values from local variables or fields. Instructions then operate on the operands from the stack, placing the results back onto it. The operand stack also facilitates passing parameters to methods and receiving their results.</blockquote><h4>Dynamic Linking</h4><ul><li>Each frame contains a reference to the run-time constant pool for the type of the current method to support dynamic linking of the method code. The class file code for a method refers to methods to be invoked and variables to be accessed via symbolic references.</li></ul><blockquote><strong>Static Linking:</strong> It occurs when the code (library or object files) is directly embedded into the executable at compile time. When a program is compiled, all the necessary libraries and functions are combined into a single executable file, thus the program doesn’t require external dependencies during runtime</blockquote><ul><li>Dynamic linking translates these symbolic method references into concrete method references, loading classes as necessary to resolve as-yet-undefined symbols. It copy the names of the external libraries into our final executable as unresolved symbols. The actual linking of these unresolved symbols happens only at runtime. Only one copy of a shared library in the memory.</li></ul><blockquote>Know more about <a href="https://www.partech.nl/en/publications/2022/03/what-is-dll-hell-problem-and-how-to-solve-it">DLL Hell Problem</a>: Key problem about dynamic linking</blockquote><h3>Synchronization in Java</h3><p>In Java, <strong>synchronization</strong> is a technique used to manage how multiple threads access shared resources. It is crucial in a multi-threaded environment to avoid thread conflicts and data inconsistencies. By ensuring that only one thread can access a resource at any given moment, synchronization helps maintain the integrity of the data.</p><p>The JVM supports synchronization of both methods and sequences of instructions within a method by a single synchronization construct: the monitor.</p><p>There are two types of synchronizations in Java:</p><ol><li><strong>Process Synchronization: </strong>It is<strong> </strong>a technique used to coordinate the execution of multiple processes. It ensures that the shared resources are safe and in order</li><li><strong>Thread Synchronization</strong>: It is used to coordinate and ordering of the execution of the threads in a multi-threaded program. It is of two types: Mutual Exclusive &amp; Cooperation (Inter-thread communication)</li></ol><p>Java provides various mechanisms to handle synchronization:</p><h4><strong>1. Synchronized Method</strong></h4><ul><li>A method can be synchronized by adding the synchronized keyword to its declaration. This ensures that only one thread can execute this method on an instance of the class at a time.</li><li><strong>Method-level synchronization</strong> is performed implicitly, as part of method invocation and return . A synchronized method is distinguished in the run-time constant pool’s method_info structure.</li><li>During the time the executing thread owns the <strong>monitor</strong>, no other thread may enter it. If an exception is thrown during invocation of the synchronized method and the synchronized method does not handle the exception, the monitor for the method is automatically exited before the exception is rethrown out of the synchronized method.</li></ul><pre>public synchronized void method() {<br>    // critical section code<br>}</pre><h4><strong>2. Synchronized Blocks:</strong></h4><ul><li>To fine-tune control over the methods, synchronization can be applied to specific blocks of code within a method. This allows synchronization only around a critical section rather than the whole method.</li></ul><pre>public void method() {<br>    synchronized(this) {<br>        // critical section code<br>        // this refers to the current object, <br>        // and only one thread can execute the synchronized block <br>        // on that object at a time.<br>    }<br>}</pre><h4><strong>3. </strong><a href="https://www.baeldung.com/java-concurrent-locks"><strong>Locks</strong></a><strong> (java.util.concurrent.locks):</strong></h4><ul><li>Java provides explicit lock management via the Lock interface, which provides more flexibility than the synchronized keyword. The ReentrantLock is the most commonly used implementation.</li></ul><pre>import java.util.concurrent.locks.Lock;<br>import java.util.concurrent.locks.ReentrantLock;<br><br>Lock lock = new ReentrantLock();<br><br>public void Method() {<br>    lock.lock();  // Acquire the lock<br>    try {<br>        // critical section code<br>    } finally {<br>        lock.unlock();  // Ensure that the lock is always released<br>    }<br>}</pre><h4><strong>4. </strong><a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html"><strong>Atomic Variables</strong></a><strong> (java.util.concurrent.atomic):</strong></h4><ul><li>Atomic algorithms exploit low-level atomic machine instructions such as compare-and-swap (CAS), to ensure data integrity.</li></ul><blockquote>A typical CAS operation works on three operands:<br>1. The memory location on which to operate (M)<br>2. The existing expected value (A) of the variable<br>3. The new value (B) which needs to be set</blockquote><ul><li>The CAS operation updates atomically the value in M to B, but only if the existing value in M matches A, otherwise no action is taken.<br>java.util.concurrent.atomic package provides atomic classes such as AtomicInteger, AtomicLong, AtomicReference, etc., which provide thread-safe operations on variables.</li></ul><h4>5. <a href="https://www.baeldung.com/thread-pool-java-and-guava">Executor Services for Thread Pool Management</a></h4><ul><li>When dealing with concurrency, using an executor service (like ExecutorService) helps in managing a pool of threads rather than creating new threads manually.</li><li>The Executor framework provides an ExecutorService interface that defines a set of methods for managing the lifecycle of threads in a thread pool. By using an ExecutorService, you can create, execute, and manage threads without having to manage their lifecycle manually.</li></ul><h4>6. <a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Semaphore.html">Semaphores</a></h4><ul><li>A Semaphore is used to control access to a resource by multiple threads by maintaining a set of permits. It is used to limit the number of concurrent threads accessing a specific resource.</li><li>A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, then access is denied.</li></ul><p>This concludes the first part of this Core Java series. We have gone over the basic architecture of JVM and understood how synchronization works in Java. In the following blogs we will go over more core concepts like JRE, JDK, multi-threading, deadlocks etc.</p><p>Do check out my other blogs and keep an eye out for new blogs in this series. <br>Medium: <a href="https://medium.com/u/b1e3116982ce">Bhaumik Maan</a> || Github: <a href="https://github.com/bhaumikmaan">bhaumikmaan</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3900a76657f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating Postman Shortcut Icon in Ubuntu]]></title>
            <link>https://medium.com/@bhaumikmaan/creating-postman-shortcut-icon-in-ubuntu-fec864754997?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/fec864754997</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[shortcuts]]></category>
            <category><![CDATA[ubuntu-desktop]]></category>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[postman]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Sun, 21 Jul 2024 03:32:22 GMT</pubDate>
            <atom:updated>2024-07-21T03:32:22.271Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TTLus-XLYJCpdva55Eu17w.jpeg" /></figure><p>Creating a desktop icon for Postman in Ubuntu can significantly streamline your workflow and enhance your productivity. As one of the most popular API development tools, Postman is indispensable for developers and testers who require a reliable, efficient way to manage their API requests and responses.</p><p>However, the convenience of accessing this powerful tool with a single click is something many users seek. In this article, we will create a desktop icon for Postman on Ubuntu, allowing you to easily launch the application and improve your overall user experience. Whether you are a seasoned developer or a newcomer to the world of APIs, having quick access to Postman is a small tweak that everyone wants.</p><p>To create an icon and launch your Postman instance via the application launcher, follow these steps:</p><ul><li>Download Postman and extract into any folder. You should see the following in your folder.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OEGLKLpQkqCm5MOtJs7F3w.png" /></figure><ul><li>Open terminal and enter the following command to go to the .local/share/applicationsdirectory:</li></ul><pre>cd ~/.local/share/applications</pre><ul><li>Create a Postman.desktop file in this directory using gedit/nano</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CJHHAkSsP_KkvwpgKgwbnQ.png" /></figure><ul><li>Paste the following contents in the Postman.desktop file:</li></ul><pre>[Desktop Entry]<br>Encoding=UTF-8<br>Name=Postman<br>Exec=/home/{username}/Documents/Postman/Postman %U<br>Icon=/home/{username}/Documents/Postman/app/icons/icon_128x128.png<br>Terminal=false<br>Type=Application<br>Categories=Development;<br>MimeType=text/html;text/xml;application/xhtml+xml;application/xml;application/rss+xml;application/rdf+xml;image/gif;image/jpeg;image/png;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;x-scheme-handler/chrome;video/webm;application/x-xpinstall;<br>StartupNotify=true<br>Actions=new-window;new-private-window;</pre><p>Replace {username} with your own username that you see in the terminal.</p><ul><li>Save the file and close the terminal!</li></ul><p>Now upon searching in applications you should see the Postman App, clicking which should open the app. You can also add it to your favourites.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/906/1*qJAePlRwCv6fgod5NU-uDQ.png" /></figure><p>And that’s how you do it. You’ve successfully created a Postman Desktop Icon to quickly access the application.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fec864754997" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning React: Beginners Haven Pt. 2]]></title>
            <link>https://medium.com/@bhaumikmaan/learning-react-beginners-haven-pt-2-5e8c970c6775?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e8c970c6775</guid>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Thu, 15 Jun 2023 14:08:41 GMT</pubDate>
            <atom:updated>2023-06-15T14:08:41.890Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1ky-RCcz-QOWQWgz" /></figure><p>Welcome to Part 2 of the blog series on ReactJS! In this blog, we dive deeper into the heart of React — States, explore CRUD operations and learn using States in React development. We will also learn how to use hooks — useState, useEffect &amp; useContext. And finally we will cover React Router DOM.</p><h3>Recap</h3><ul><li><a href="https://medium.com/@bhaumikmaan/learning-react-beginners-haven-pt-1-1fe0eca46676">Part 1</a> — Setting up the environment, JSX, Components, props and using CSS. Importantly, we learnt about Ternary Operators and Conditional Rendering</li></ul><h3>States in React</h3><p><strong>State </strong>is a structure that is updatable and is used to store data or details about a component. It enables <em>dynamic</em> and <em>interactive user interfaces</em> by allowing components to maintain and update their own data. Component re-rendering is <em>triggered by state changes</em>, in turn updating the information displayed on the screen.</p><p>Let’s create a function that increments the count of a number by 1 when we press a button using the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/1*KpCr_EEwlBo5UDIeCibZlg.png" /><figcaption>Making a function to implement to incrementing the count variable</figcaption></figure><p>On pressing the button, the console see’s the updates in the cnt but the change is <strong>not visible</strong> on the element displayed on screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SrJi6cAwdWsN_x7MaQZbTg.png" /><figcaption>Seeing the output in Console Log &amp; on app</figcaption></figure><p>This is because React doesn’t detect the change automatically, which is why using States comes into play.</p><blockquote><strong>Why is the change not seen?</strong></blockquote><blockquote>React uses a reconciliation process and its Virtual DOM (a compact replica of the real Document object model) to quickly refresh the user interface. It determines what has to be changed on the actual DOM by comparing the old and new Virtual DOM representations.<br>Since the direct modification of a variable does not initiate the reconciliation process, React is unable to recognise the change. As a result, the user interface is not updated to display the variable’s new value.</blockquote><p>To use a state and render the change in variable, we will use the useState Hook.</p><blockquote><strong>What is a Hook?</strong></blockquote><blockquote>A hook is a special function that allows us to use state and other React features in functional components. Hooks provide functional components a mechanism to manage and encapsulate stateful behaviour, enabling them to have local state, lifecycle methods, and other capabilities that were previously only available to class components..</blockquote><h3>useState Hook</h3><p>The useState hook is a built-in function in React that allows functional components to have a state. By using this, we can declare a <em>state variable</em> inside a <strong>functional component</strong> and access its <em>current value</em>. It also provides a function to update the state variable, <em>triggering a re-render</em> of the component.</p><p>To use a state, we first import the <strong>useState Hook</strong> using the following command: import { useState } from “react”.</p><p>We can now use this hook to make a state using the following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*NijefMHP73EvdTme7D9KbQ.png" /><figcaption>Implementing useState Hook</figcaption></figure><p>On clicking the button, the cnt variable gets updated and the updated value is re-rendered on the screen!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ER68k-LcfItNz_VFaTAcFg.png" /><figcaption>Output with re-rendered component &amp; new variable value</figcaption></figure><p>In the above case, useState(0) initializes the cnt state variable with an initial value of 0. The setCount function updates the cnt variable. When the button is clicked, setCount(cnt+ 1) increments the value of cnt by 1, triggering a re-render and updating the displayed count on the screen.</p><p>More examples of using a useState hook are at the end of this blog!</p><h3>CRUD Operations</h3><p>CRUD (Create, Read, Update, Delete) operations involves interacting with an API or manipulating data with the component’s state.</p><ul><li>CreateC: To create new data, we would typically gather user input through forms or other input mechanisms.</li><li>ReadR: Reading data involves fetching existing data from an API or using data stored within the component’s state.</li><li>UpdateU: To update existing data, we need to retrieve the data first (using the Read operation) and present it in an editable form to the user.</li><li>DeleteD: Deleting data involves sending an HTTP request to the API with the unique identifier of the data you want to delete.</li></ul><p>CRUD operations in React often involve <em>asynchronous operations</em> like API requests, so handling loading states and error handling is crucial. To practice this, we can make a <em>ToDo List.</em></p><h3>Component Lifecycle</h3><p><em>Component lifecycle</em> refers to the different stages a component goes through, from its creation to its removal from the DOM. It can be divided into 3 main phases:</p><ul><li><em>Mounting</em>: Occurs when a component is being initialized and inserted into the DOM.</li><li><em>Updating</em>: Occurs when a component’s state or props are updated.</li><li><em>Unmounting</em>: Occurs when a component is being removed from the DOM.</li></ul><h3>useEffect Hook</h3><p>The useEffect hook is a built-in hook in React that allows you to perform side effects in functional components. It is used to handle actions that need to be executed after rendering or in response to certain <em>dependencies</em> changing.</p><p>A <strong>dependency</strong> refers to a value or a list of values that are passed as the <em>second</em> <em>argument</em> to useEffect. The purpose of specifying dependencies is to tell React when the effect should be re-run (React compares the previous values of the dependencies with their current values, If any of the dependencies have changed).</p><p>Using the following code, we can see how a component is mounted and unmounted using the useEffect hook when the state is changed by clicking on the button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H3wqTMsAgnWVJwA3i_dvJw.png" /><figcaption>Code for Mount and Unmount using useEffect Hook</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UkN5Uz-GzwglWvjVxvRBBw.png" /><figcaption>Output after clicking the button</figcaption></figure><p>Like in the above code, the syntax of using useEffect hook is:<br>useEffect(() =&gt; { console.log(‘Effect ran’); }, [dependency]);.</p><p>The effect function is only re-run when the dependency state variable changes. If other state variables or props are updated but dependency remains the same, the effect won&#39;t be re-executed.</p><p>If we omit the dependency array altogether, the effect will run after every render. If we pass an empty array ([]), like in the example above, as the dependency, the effect will <strong>run only once.</strong></p><h3>React Router DOM</h3><p>The most thought of question when working with React is “How to make multiple pages in our application?”. Since React is a <em>Single Page Application (SPA), </em>it dynamically rewrites an existing web page with new information from the web server, rather than having multiple HTML pages.</p><p><strong>React Router DOM</strong> is a popular library that <em>provides routing capabilities</em> for React applications. It allows us to <em>handle navigation</em> and routing within our React application by synchronizing the UI with the URL.</p><p>To use React Router DOM, we go to the terminal and enter the following command to install it as a dependency: npm install react-router-dom.</p><p>Once installed, we can import the necessary components using: <br>import { BrowserRouter as Router, Routes, Route } from &quot;react-router-dom&quot;.</p><p>The key components provided by React Router DOM are:</p><ul><li>&lt;BrowserRouter&gt;: A router implementation that uses the HTML5 history API (pushstate, replacestate, and popstate events) to keep your UI in sync with the URL. It defines where in our app do we want to have different routes.</li><li>&lt;Route&gt;: A component that renders a specific component when the URL matches a given path. It can be used to define different routes and associate them with corresponding components. It is a self-closing tag.</li><li>&lt;Switch&gt;: A component that renders the first &lt;Route&gt; that matches the current URL. It helps to ensure that only one route is rendered at a time.</li><li>&lt;Link&gt;: A component that renders an anchor tag (&lt;a&gt;) to navigate to a specific URL within the application. It provides a declarative way to create links without triggering a full page reload.</li><li>&lt;Routes&gt;: A component that tells React, that this is where the user will define different routes for the application.</li></ul><p>We can implement all of the above by making a navbar with multiple components being rendered in the page when the URL changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*5gNTX4w0hXTzLiQ79Nmxug.png" /><figcaption>Code for App.js with multiple routes</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zMmSN9N6j4h5C5CmiO2JTw.jpeg" /><figcaption>Code for various components that routes link to</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YOSEcTn-pxEsJHAsqh4Kug.png" /><figcaption>Output</figcaption></figure><h3>State Management</h3><p><em>State management</em> in React refers to the process of managing and controlling the application’s state, which includes data and its related behavior, throughout the <strong>component hierarchy</strong>. State management is usually used to avoid <em>Props drilling</em>.</p><p><strong>Props drilling</strong>, also known as “component nesting hell,” refers to the process of <em>passing down props</em> through multiple intermediate components to reach a deeply nested component that needs access to those props.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*nuhXIxqJ93xQwmXK4RqEhw.png" /><figcaption>Prop Drilling</figcaption></figure><p>In the above example, props passed in the top function is passed down to middle and then to bottom where it is finally used. Another way to handle prop drilling is by using the useContext hook.</p><h3>useContext Hook</h3><p>useContext is a built-in hook in React that allows us to consume values from React&#39;s <em>Context API</em> within functional components. It provides a way to access context values without the need for intermediate components or prop drilling.</p><p>The <strong>Context API</strong> in React is a mechanism for sharing data between components without explicitly passing props through every level of the component tree.</p><p>We can do this by using createContext &amp; useContext. MyContext is created using React.createContext(). The App component acts as the <strong>provider</strong> and wraps the ChildComponent. Inside the ChildComponent, the useContext hook is used to access the context value directly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*Ee_4e8bHw9ITMkPx6vRgHg.png" /><figcaption>Using the useContext Hook</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QT_u12qT9yhVu03LcBulfg.png" /><figcaption>Output</figcaption></figure><p>By using useContext, we can avoid prop drilling and easily access context values within any functional component required. It simplifies the consumption of context and makes the code more readable and maintainable.</p><h3>More examples of useState Hook</h3><ul><li><strong>Hiding/Showing a Component<br></strong>Toggling the visibility of text by clicking of a button. We make a flag (by default true), which on click of the button, gets toggled in turn changing the visibility of the component.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*8CM9IOlo1zlFPLG40qLN1Q.png" /><figcaption>Component can be hidden or shown on click of the button</figcaption></figure><ul><li><strong>Reflecting text being Input in text box<br></strong>Reflecting whatever text is being Input into the text box under the box itself. React re-renders the component each time the input value changes and the change is reflected</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*yN72K2O56YHc80QDH5pMHQ.png" /><figcaption>Text added to the box is being reflected under it</figcaption></figure><ul><li><strong>Enforcing CSS<br></strong>Similar to the above example, we can change the CSS of components by pressing a button. We check whether the condition is true or not, and thus implementing the necessary changes using Ternary operators.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*m3GEL1kkQDP6XD0KnDMohw.png" /><figcaption>Switching colors by pressing the button</figcaption></figure><p>This concludes the second part of this React series. We have now understood the core concept of React — States. We have also learned about Router DOM, State management and component lifecycle. And finally we learnt about various hooks in React with their examples.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e8c970c6775" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning React: Beginners Haven Pt. 1]]></title>
            <link>https://medium.com/@bhaumikmaan/learning-react-beginners-haven-pt-1-1fe0eca46676?source=rss-b1e3116982ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/1fe0eca46676</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[beginners-guide]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Bhaumik Maan]]></dc:creator>
            <pubDate>Sun, 14 May 2023 07:25:44 GMT</pubDate>
            <atom:updated>2023-05-14T07:31:34.627Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Jr4g1tQdxk23Sx01" /></figure><p>Have you ever wanted to build dynamic user interfaces but didn’t know where to start? Of the ton of client side-frameworks like Angular, Bootstrap &amp; Vue, <strong>React</strong> is the most popular JavaScript library used by developers worldwide.</p><p>This blog series will cover all the core understandings you must have when working on your very own React Project! Some Pre-Requisites for learning React are:</p><ol><li>Basic knowledge of HTML, CSS, and JavaScript.</li><li>Basic understanding of ES6 features such as:<br>1. Let &amp; const<br>2. Arrow Functions<br>3. Imports &amp; Exports</li></ol><h3>Getting Started!</h3><p><strong>React</strong> is a popular <em>JavaScript library</em> for building user interfaces. Developers design the interface of their applications using React’s <em>declarative approach</em> to programming, and React takes care of updating the interface as the data changes. Complex user interfaces can be created more easily using this method, and the codebase can be kept <em>well-structured</em><strong><em> </em></strong>and<strong><em> </em></strong><em>manageable</em>.</p><p>Some of the things that you will need when working on a react project are:</p><ul><li><strong>Code Editor —</strong> (I will be using <a href="https://code.visualstudio.com/">VS Code</a>)</li><li><a href="https://nodejs.org/en/download"><strong>Node.js</strong></a><strong> — </strong>It is an open-source, cross-platform, server-side JavaScript runtime environment. It allows developers to run JavaScript code on the server-side, rather than just in the browser on the client-side. It is used in the React development workflow for managing dependencies, running build tools, and creating server-side rendered applications</li><li><a href="https://www.npmjs.com/package/download"><strong>npm</strong></a> — stands for <em>Node Package Manager</em>. <br>It is a command-line tool used to manage and share packages (also known as libraries or modules) of JavaScript code. npm is primarily used for installing, sharing, and managing packages &amp; their dependencies that are needed for building a Node.js application.</li></ul><p>Once we have the above software&#39;s ready, we can make our first react app.</p><ul><li>We put in the command npx create-react-app in the code editor terminal to begin our setup for our react app.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5V9tTaWXi__wkuBt4s9TQg.png" /></figure><blockquote>Add a . at the end of the command in the terminal if you are already in the desired folder for your app. Else, write a name so a folder of that name is created.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wVIf8hTzMjcY_Zgn6mhBIg.png" /></figure><p>On completion this is how the file looks now! This is the pre-built template that we get on creating a react app with various files. Some of these files are not necessary so we will remove them.</p><p>There are 2 main folders public &amp; src.</p><ul><li><strong>public Folder</strong> — This folder has a bunch of static files such as boilerplates, logos &amp; JSON files. <br>More importantly, it has the index.html file. We won’t be using HTML in React to make changes in the UI because it gives us a way to build <em>single-page applications</em>, which means the entire programme will be contained inside a single HTML file.<br>In the index.html, we see a single div element with ID root. This is because React will push all the code into this div using Javascript. This is quite advantageous because it eliminates the need to download and render numerous HTML files for each individual page in our application.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*PAe-F6QM3eb6-468VSY6rQ.png" /></figure><ul><li><strong>src Folder</strong> — This folder is the main source directory where we will find most of our code. The main file on which we will be working will be App.js. <br>Inside index.js file, we create React Elements and <em>route</em> all of our code to the root of the single HTML file. All the code, for the most part will reside in the App function in the App.js file.</li><li><strong>package.json</strong> — This file, present in the root directory of a React app, contains information about the project and lists all the dependencies installed in the application. <br><strong>Dependencies</strong> refer to external software components or libraries that your application needs to function properly. <br>When we clone a project, package.json is where all the information would be to access mainly for the documentation.</li></ul><p>Now we have the basic files we need &amp; an understanding of what each file does. To run our application on a live server in our browser, we use npm start command to start deploying our react app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*VrCV0lz_49e_BuIi-I4wtQ.png" /></figure><p>This deployment is hosted on your browser at <a href="http://localhost:3000/">http://localhost:3000/</a>.</p><h3>Basic Syntax (JSX, Components &amp; Props)</h3><p><a href="https://legacy.reactjs.org/docs/introducing-jsx.html"><strong>JSX</strong></a> <em>(JavaScript XML)</em> is a syntax extension for JavaScript that allows developers to write HTML-like code directly in their JavaScript code.</p><p>It is a <em>blend of HTML &amp; JS</em> which makes it easier to mix rendering and UI Logic. The advantage of this is that we may incorporate JS functionalities into our HTML code, such as utilizing variables that have been declared in a function, in our HTML page. In React, any <strong>variable</strong> can also be visualized as an HTML or JSX element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uSGYdorPyoBrM0xy05CPwg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fnQ3V_4n9mgMCIH6gYMLtA.png" /></figure><p><a href="https://legacy.reactjs.org/docs/components-and-props.html"><strong>Components</strong></a> are <em>self-contained</em> piece of code that encapsulates specific <em>functionality</em> and can be <strong>reused</strong> throughout an application. They serve the same purpose as JavaScript functions, but work in isolation and return HTML.</p><p>Components are the building blocks of a React application, and by breaking our application into smaller, reusable components, we can create a more modular and maintainable codebase.</p><p>Components are called using specific syntax i.e. &lt;componentName /&gt;, as a self-closing HTML tag that can be used repeatedly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qe8raGOjXZn2kY17_4-Wnw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cZSJQj2eVZdg_54_uK28YA.png" /></figure><p>In regular Javascript we can pass arguments in a function directly func(arg), but we can not do that in React. Thus comes the concept of <strong>Props.</strong></p><p><a href="https://legacy.reactjs.org/docs/components-and-props.html"><strong>Props</strong></a><strong> </strong>are a way to <em>pass data</em> from a parent component to a child component. Props allow components to be more flexible and reusable by allowing them to receive data from outside sources rather than hardcoding it within the component<strong> </strong><br><em>Note — Use curly braces when passing numbers in props.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*L4WhB6-pfJJC4i6131StNQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*tEY-K8Uu1RuxSnkrZgeaYg.png" /></figure><h3>CSS In React</h3><p>By default, on creating an app in React, App.css is present in the directory, with preset styles. We can add to this, or make a completely new <a href="https://legacy.reactjs.org/docs/faq-styling.html">CSS for our website</a>.</p><ul><li>We can import this CSS to our file using import ./nameOfFile.css.</li><li>Another way to do so, instead of naming each class for every HTML element, we can directly pass styles to these elements using modules. Here the styles defined in a CSS Module will only apply to the specific component that it is imported into.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*YLawdN9pfKjREwZiFyyIUA.png" /></figure><h3>Conditional Rendering &amp; Ternary Operators</h3><p>This is one of the most crucial topics of understanding in React!</p><p><strong>Ternary operators</strong> are a concise way of writing <em>conditional</em> <em>statements</em> in JavaScript, and they can be used in React as well. In React, you can use ternary operators to <em>conditionally render</em> content based on a certain condition. Ternary operators are a useful tool for writing concise and readable code in React.</p><p><strong>Syntax:</strong> { condition ? true-case : false-case }</p><p><a href="https://react.dev/learn/conditional-rendering"><strong>Conditional rendering</strong></a> refers to the practice of <em>rendering different content</em> based on a certain condition. This means that you can use a condition to decide whether to render one set of content or another, depending on whether the condition is true or false.</p><p>It is generally used when we want to show or hide certain content based on user interactions or other events.</p><p>By combining conditional rendering and ternary operators, we can write clean and concise codes for the same functionality. This includes rendering separate HTML, applying different styles to components etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8PpzALguzsjAw1eZomPyHQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H5JSNLqtnhwOdsOY-efgLA.jpeg" /></figure><h3>Lists In React</h3><p>In React, we can perform all the operations on a list, like we do in Javascript. We can <a href="https://react.dev/learn/rendering-lists">render lists</a> of data using the map() method. The map() method allows you to transform an array of data into a list of React elements that can be rendered to the DOM. We can use this to return a new UI as it traverses through the list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6cByGGHblKnQaJ9N2LIDow.jpeg" /></figure><p><em>We can use the key for various functions such as accessing, updating or deleting data from the list, we will use this later.</em></p><p>This concludes the first part of this React series. We now know how to set up and launch a React project. We have also covered the fundamentals of a React app, including JSX, components, lists, and CSS. We also addressed some essential concepts that are crucial to our progress, like Ternary Operators and Conditional Rendering.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1fe0eca46676" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>