<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DOREEN ATIENO</title>
    <description>The latest articles on DEV Community by DOREEN ATIENO (@doreen_atieno_onyango).</description>
    <link>https://dev.to/doreen_atieno_onyango</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2475051%2Fa0a76fce-54f6-42fd-967e-7ea623ba0af9.png</url>
      <title>DEV Community: DOREEN ATIENO</title>
      <link>https://dev.to/doreen_atieno_onyango</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/doreen_atieno_onyango"/>
    <language>en</language>
    <item>
      <title>Best Practices for Writing Commit Messages and Code Comments</title>
      <dc:creator>DOREEN ATIENO</dc:creator>
      <pubDate>Mon, 07 Jul 2025 13:54:54 +0000</pubDate>
      <link>https://dev.to/doreen_atieno_onyango/best-practices-for-writing-commit-messages-and-code-comments-4adl</link>
      <guid>https://dev.to/doreen_atieno_onyango/best-practices-for-writing-commit-messages-and-code-comments-4adl</guid>
      <description>&lt;p&gt;Every line of code you write solves a problem.But what if someone else has to understand, fix, or build on that code tomorrow, or even months from now? That’s where your commit messages and code comments become silent allies. They don’t just document your work, they tell the story behind it. They capture the why, not just the what.They transform a code-base from a pile of logic into a living, collaborative space.&lt;/p&gt;

&lt;p&gt;In this guide, we’ll walk through simple, practical tips to help you write commit messages that matter and comments that make a difference, so your future self (and your team) will thank you.&lt;/p&gt;

&lt;p&gt;Let’s make your code not just functional, but unforgettable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing Good Commit Messages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why Commit Messages Matter
&lt;/h3&gt;

&lt;p&gt;Commit messages are documentation. They:&lt;br&gt;
    a. Explain the intention behind code changes.&lt;br&gt;
    b. Help future developers (including yourself) understand the history&lt;br&gt;
    c. Make debugging and code reviews easier&lt;br&gt;
    d. Improve collaboration and traceability&lt;/p&gt;

&lt;h3&gt;
  
  
  Format
&lt;/h3&gt;

&lt;p&gt;Use this structure:&lt;br&gt;
&lt;code&gt;&amp;lt;type&amp;gt;: &amp;lt;short summary&amp;gt;&lt;br&gt;
[optional body for context]&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Commit Types
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;feat&lt;/strong&gt; Used when Adding a new feature&lt;br&gt;
&lt;strong&gt;fix&lt;/strong&gt; Used when Fixing a bug&lt;br&gt;
&lt;strong&gt;refactor&lt;/strong&gt; Used when Restructuring code without changing behavior&lt;br&gt;
&lt;strong&gt;docs&lt;/strong&gt; Used when Updating documentation&lt;br&gt;
&lt;strong&gt;style&lt;/strong&gt; Used when Code formatting (indentation, spacing)&lt;br&gt;
&lt;strong&gt;test&lt;/strong&gt; Used when Adding or fixing tests&lt;br&gt;
&lt;strong&gt;chore&lt;/strong&gt; Used for Misc tasks like build, CI, config&lt;/p&gt;

&lt;h3&gt;
  
  
  Writing the Summary
&lt;/h3&gt;

&lt;p&gt;a. Be concise and start with a verb:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    fix: handle null user on login
    feat: add dark mode toggle

Limit to 50 characters if possible
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;b. Use present tense (as if describing what this commit does)&lt;br&gt;
Example Messages&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    feat: implement user login endpoint
    Added JWT-based authentication with login and token generation.

    fix: correct typo in signup form validation error
    refactor: extract database logic into separate package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Writing Clear Code Comments
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Why Comment?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a. Explain why, not just what&lt;/p&gt;

&lt;p&gt;b. Clarify complex logic, not the obvious&lt;/p&gt;

&lt;p&gt;c. Help future readers maintain or extend the code&lt;/p&gt;
&lt;h3&gt;
  
  
  Commenting Best Practices
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Avoid obvious comments&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     ❌ i++ // increment i
     ✅ i++ // move to the next user in the list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Use comments to explain “why”, not “what”&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     ❌ // fetch user
     ✅ // Fetch user so we can prefill the edit form
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Document assumptions, edge cases, and decisions&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     // We retry 3 times to handle transient network failures
     for i := 0; i &amp;lt; 3; i++ {
     ...
     }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Prefer self-explanatory code over excessive comments&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Rename variables or functions if a comment feels necessary&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; // Bad
 let x = 10; // maximum retries
 // Better
 let maxRetries = 10;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use TODO/FIXME tags sparingly and consistently&lt;/strong&gt;&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     # TODO: Refactor this to async in next release&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  What to Avoid&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;a. Vague commit messages like changes, update, or fix bug&lt;br&gt;
 b. Over-commenting every line&lt;br&gt;
 c. Outdated comments - update or remove them as code changes&lt;br&gt;
 d. Jokes or sarcasm in comments (yes, it happens)&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Tips
&lt;/h3&gt;

&lt;p&gt;a. Use version control tools (git log, git blame) as communication history&lt;br&gt;
 b. Treat your commit history like documentation&lt;br&gt;
 c. Imagine someone else reading your code months later, what would they need?&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Great code is more than just working code, it's communicative, thoughtful, and human-friendly. Every commit message you write is a window into your decision-making. Every comment you leave is a helping hand to your future self or another developer.&lt;/p&gt;

&lt;p&gt;So don’t just write code, tell the story behind it. Be the kind of developer others thank when digging through version history or deciphering a complex logic flow.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Let your commits document your journey.&lt;/li&gt;
&lt;li&gt; Let your comments guide the reader through your thinking.&lt;/li&gt;
&lt;li&gt; And let your code-base be one that’s not only powerful, but
understandable, maintainable, and admired.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now go write like someone will read it, because &lt;strong&gt;they will&lt;/strong&gt;. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Getting Started with Django: A Beginner's Guide to Web Development.</title>
      <dc:creator>DOREEN ATIENO</dc:creator>
      <pubDate>Mon, 30 Jun 2025 20:19:39 +0000</pubDate>
      <link>https://dev.to/doreen_atieno_onyango/getting-started-with-django-a-beginners-guide-to-web-development-5dgo</link>
      <guid>https://dev.to/doreen_atieno_onyango/getting-started-with-django-a-beginners-guide-to-web-development-5dgo</guid>
      <description>&lt;h2&gt;
  
  
  Django: The Web Developer's Blueprint
&lt;/h2&gt;

&lt;p&gt;Imagine you're building a house. You could start from scratch, cutting every piece of wood and mixing every batch of concrete. Or you could use a proven blueprint with pre-made components that fit together perfectly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Django&lt;/strong&gt; is that blueprint for web development.&lt;/p&gt;

&lt;p&gt;Django is a &lt;strong&gt;high-level Python web framework&lt;/strong&gt; that follows the &lt;em&gt;"batteries-included"&lt;/em&gt; philosophy. This means it comes with everything you need to build a production-ready web application — from &lt;strong&gt;user authentication&lt;/strong&gt; to &lt;strong&gt;database management&lt;/strong&gt;, all built-in and ready to use.&lt;/p&gt;

&lt;p&gt;Whether you want to build a &lt;strong&gt;blog&lt;/strong&gt;, an &lt;strong&gt;e-commerce site&lt;/strong&gt;, a &lt;strong&gt;social network&lt;/strong&gt;, or a &lt;strong&gt;business application&lt;/strong&gt;, Django provides the tools and structure to make it happen efficiently and securely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Django's Architecture: The MVT Pattern
&lt;/h2&gt;

&lt;p&gt;Django follows the &lt;strong&gt;Model-View-Template (MVT)&lt;/strong&gt; pattern, which is Django's interpretation of the popular &lt;strong&gt;MVC (Model-View-Controller)&lt;/strong&gt; pattern. Understanding this architecture is crucial to becoming proficient with Django.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is MVT?
&lt;/h3&gt;

&lt;p&gt;MVT separates your application into &lt;strong&gt;three distinct layers&lt;/strong&gt;, each with specific responsibilities:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Models&lt;/strong&gt; – Your Data Layer
&lt;/h3&gt;

&lt;p&gt;Models represent your &lt;strong&gt;data structure&lt;/strong&gt; and &lt;strong&gt;business logic&lt;/strong&gt;. They define:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database tables and their relationships
&lt;/li&gt;
&lt;li&gt;Data validation rules and constraints
&lt;/li&gt;
&lt;li&gt;Business logic methods that operate on your data
&lt;/li&gt;
&lt;li&gt;How data is stored, retrieved, and manipulated
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of models as the &lt;strong&gt;blueprint for your database&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;If you're building a blog, your models might include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;User&lt;/code&gt; (for authors and readers)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Post&lt;/code&gt; (for blog articles)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Comment&lt;/code&gt; (for reader feedback)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Category&lt;/code&gt; (for organizing posts)
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Views&lt;/strong&gt; – Your Logic Layer
&lt;/h3&gt;

&lt;p&gt;Views handle the &lt;strong&gt;business logic&lt;/strong&gt; of your application. They:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Process user requests (&lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, etc.)
&lt;/li&gt;
&lt;li&gt;Interact with models to retrieve or modify data
&lt;/li&gt;
&lt;li&gt;Apply business rules and validation
&lt;/li&gt;
&lt;li&gt;Prepare data for presentation
&lt;/li&gt;
&lt;li&gt;Return responses to users
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Views are where the &lt;strong&gt;"thinking" happens&lt;/strong&gt; in your application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;A view might:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Receive a request to display a blog post
&lt;/li&gt;
&lt;li&gt;Fetch the post data from the database
&lt;/li&gt;
&lt;li&gt;Check if the user has permission to view it
&lt;/li&gt;
&lt;li&gt;Prepare the data for the template
&lt;/li&gt;
&lt;li&gt;Return the rendered page
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Templates&lt;/strong&gt; – Your Presentation Layer
&lt;/h3&gt;

&lt;p&gt;Templates define how your data is &lt;strong&gt;presented to users&lt;/strong&gt;. They:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Display data in a user-friendly format
&lt;/li&gt;
&lt;li&gt;Handle the visual presentation of your application
&lt;/li&gt;
&lt;li&gt;Include conditional logic for dynamic content
&lt;/li&gt;
&lt;li&gt;Manage the HTML structure and styling
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Templates are responsible for the &lt;strong&gt;"look and feel"&lt;/strong&gt; of your application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;A template might:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Display a blog post with proper formatting
&lt;/li&gt;
&lt;li&gt;Show different content for logged-in vs. anonymous users
&lt;/li&gt;
&lt;li&gt;Include navigation menus and footers
&lt;/li&gt;
&lt;li&gt;Handle responsive design for different screen sizes &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How MVT Works Together
&lt;/h3&gt;

&lt;p&gt;Here's the typical flow of a Django application:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User makes a request&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
(e.g., visits a page or submits a form)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;URL routing&lt;/strong&gt; directs the request to the appropriate &lt;strong&gt;view&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;View processes the request&lt;/strong&gt; and interacts with &lt;strong&gt;models&lt;/strong&gt; as needed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Models handle data operations&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
(e.g., database queries, validation, etc.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;View prepares data&lt;/strong&gt; and selects a &lt;strong&gt;template&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Template renders the response&lt;/strong&gt; using the provided data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Response is sent back to the user&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Benefits of This Separation of Concerns
&lt;/h2&gt;

&lt;p&gt;This structure makes your Django code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Maintainable&lt;/strong&gt;: Changes to one layer don't necessarily affect others
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testable&lt;/strong&gt;: You can test each layer independently
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusable&lt;/strong&gt;: Components can be reused across different parts of your application
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalable&lt;/strong&gt;: You can optimize each layer separately as your application grows
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up Your Django Development Environment
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Install Python
&lt;/h3&gt;

&lt;p&gt;Django requires Python &lt;strong&gt;3.8 or higher&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To check your Python version, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;span class="c"&gt;# or&lt;/span&gt;
python3 &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't have Python installed, download it from python.org. Make sure to check "Add Python to PATH" during installation on Windows.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Create a Virtual Environment
&lt;/h3&gt;

&lt;p&gt;Virtual environments are essential for Python development.&lt;br&gt;
They keep your project dependencies isolated from other projects and your system Python installation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Create a new directory for your project&lt;/span&gt;
&lt;span class="nb"&gt;mkdir &lt;/span&gt;my_first_django_project
&lt;span class="nb"&gt;cd &lt;/span&gt;my_first_django_project
&lt;span class="c"&gt;# Create a virtual environment&lt;/span&gt;
python &lt;span class="nt"&gt;-m&lt;/span&gt; venv venv
&lt;span class="c"&gt;# Activate the virtual environment&lt;/span&gt;
&lt;span class="c"&gt;# On Windows:&lt;/span&gt;
venv&lt;span class="se"&gt;\S&lt;/span&gt;cripts&lt;span class="se"&gt;\a&lt;/span&gt;ctivate
&lt;span class="c"&gt;# On macOS/Linux:&lt;/span&gt;
&lt;span class="nb"&gt;source &lt;/span&gt;venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll know the virtual environment is active when you see (venv) at the beginning of your command prompt.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Install Django
&lt;/h3&gt;

&lt;p&gt;With your virtual environment activated, install Django:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;django

&lt;span class="c"&gt;# Verify the installation&lt;/span&gt;
python &lt;span class="nt"&gt;-m&lt;/span&gt; django &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Create Your First Django Project
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Create a new Django project&lt;/span&gt;
django-admin startproject myproject
&lt;span class="nb"&gt;cd &lt;/span&gt;myproject

&lt;span class="c"&gt;# Start the development server&lt;/span&gt;
python manage.py runserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visit &lt;a href="http://localhost:8000/" rel="noopener noreferrer"&gt;http://localhost:8000/&lt;/a&gt; in your browser. You should see the Django welcome page - congratulations! You've successfully set up Django.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Your First Django App
&lt;/h2&gt;

&lt;p&gt;Django projects are composed of multiple "apps" — each handling a specific feature or functionality. This modular approach makes your code organized and reusable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Create an App
&lt;/h3&gt;

&lt;p&gt;Run the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py startapp blog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a new directory structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;blog/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── urls.py
└── views.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Register Your App
&lt;/h3&gt;

&lt;p&gt;Open myproject/settings.py and add your app to the INSTALLED_APPS list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;INSTALLED_APPS &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'django.contrib.admin'&lt;/span&gt;,
    &lt;span class="s1"&gt;'django.contrib.auth'&lt;/span&gt;,
    &lt;span class="s1"&gt;'django.contrib.contenttypes'&lt;/span&gt;,
    &lt;span class="s1"&gt;'django.contrib.sessions'&lt;/span&gt;,
    &lt;span class="s1"&gt;'django.contrib.messages'&lt;/span&gt;,
    &lt;span class="s1"&gt;'django.contrib.staticfiles'&lt;/span&gt;,
    &lt;span class="s1"&gt;'blog'&lt;/span&gt;,  &lt;span class="c"&gt;# Add your app here&lt;/span&gt;
&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices for Django Development
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Code Organization
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Keep apps focused on specific functionality
&lt;/li&gt;
&lt;li&gt;Use meaningful names for models, views, and templates
&lt;/li&gt;
&lt;li&gt;Follow Django's naming conventions
&lt;/li&gt;
&lt;li&gt;Organize your project structure logically
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use database indexes for frequently queried fields
&lt;/li&gt;
&lt;li&gt;Optimize database queries using &lt;code&gt;select_related&lt;/code&gt; and &lt;code&gt;prefetch_related&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Cache expensive operations
&lt;/li&gt;
&lt;li&gt;Use pagination for large datasets
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Security
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Always validate user input
&lt;/li&gt;
&lt;li&gt;Use Django's built-in security features
&lt;/li&gt;
&lt;li&gt;Keep dependencies updated
&lt;/li&gt;
&lt;li&gt;Follow the principle of least privilege
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Testing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Write tests for all your models, views, and forms
&lt;/li&gt;
&lt;li&gt;Use test-driven development (TDD) when possible
&lt;/li&gt;
&lt;li&gt;Test both happy paths and edge cases
&lt;/li&gt;
&lt;li&gt;Maintain good test coverage
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion: Your Django Journey Begins
&lt;/h2&gt;

&lt;p&gt;Django is a powerful, flexible framework that can handle projects of any size. From simple blogs to complex enterprise applications, Django provides the tools and structure you need to build robust web applications.&lt;/p&gt;

&lt;p&gt;The key to mastering Django is understanding the MVT pattern and how the different components work together. Start with simple projects, experiment with different features, and gradually build up to more complex applications.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Structs and Methods in Go: A Beginner-Friendly Guide</title>
      <dc:creator>DOREEN ATIENO</dc:creator>
      <pubDate>Wed, 04 Dec 2024 05:30:44 +0000</pubDate>
      <link>https://dev.to/doreen_atieno_onyango/structs-and-methods-in-go-a-beginner-friendly-guide-3n24</link>
      <guid>https://dev.to/doreen_atieno_onyango/structs-and-methods-in-go-a-beginner-friendly-guide-3n24</guid>
      <description>&lt;p&gt;Go (or Golang) is known for its simplicity and efficiency. Structs and methods are foundational concepts in Go that help you organize and manage data effectively. This guide will take you through the basics of structs, methods, and how to use them systematically. Ensure you have go installed in your machine, use the right package  and correct imports for you to test the functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Structs in Go
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is a Struct?&lt;/strong&gt;&lt;br&gt;
I understand struct in Go as a composite data type that groups together variables (fields) under a single name. It is particularly useful for representing more complex data structures. While it serves a role similar to classes in object-oriented programming languages, it does so in a lightweight, straightforward manner without methods like inheritance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Defining Struct&lt;/strong&gt;&lt;br&gt;
A struct is defined using the &lt;code&gt;type&lt;/code&gt; keyword followed by the &lt;code&gt;struct name&lt;/code&gt;(User) and the fields enclosed in &lt;code&gt;curly braces&lt;/code&gt; {}.&lt;br&gt;
Each field has a name and a type. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;       &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;     &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ID&lt;/code&gt; is of type &lt;code&gt;int&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;Name&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt; are of type &lt;code&gt;string&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;IsActive&lt;/code&gt; is of type &lt;code&gt;bool&lt;/code&gt;.&lt;br&gt;
Fields like &lt;code&gt;ID&lt;/code&gt;, &lt;code&gt;Name&lt;/code&gt;, &lt;code&gt;Email&lt;/code&gt;, and &lt;code&gt;IsActive&lt;/code&gt; are capitalized to make them &lt;strong&gt;exportable&lt;/strong&gt;. This means they can be accessed from outside the package.&lt;br&gt;
If a field starts with a lowercase letter, it is &lt;strong&gt;unexported&lt;/strong&gt; and cannot be accessed outside the package where it is defined.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Struct Initialization in Go&lt;/strong&gt;&lt;br&gt;
Structs in Go can be initialized using named fields or unnamed fields. Let’s explore the differences, advantages, and potential pitfalls of both approaches.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Named Fields&lt;/strong&gt;&lt;br&gt;
When initializing a struct using named fields, you explicitly specify the name of each field along with its value.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; 
&lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages of Named Fields:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. Readability:&lt;/strong&gt;&lt;br&gt;
Each field is clearly labeled with its name, making the code easier to read and understand.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; ID: 1 directly shows that the field ID is being assigned the value 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Order Independence:&lt;/strong&gt;&lt;br&gt;
The fields can be listed in any order, regardless of how they are defined in the struct.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; 
&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Safety:&lt;/strong&gt;&lt;br&gt;
Reduces the chance of assigning values to the wrong fields.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; &lt;br&gt;
If you switch the order of &lt;code&gt;ID&lt;/code&gt; and &lt;code&gt;Name&lt;/code&gt;, the program will still work as intended because the fields are explicitly named.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Best Use Cases:&lt;/strong&gt;&lt;br&gt;
When a struct has many fields, especially of the same type (e.g., multiple strings or integers).&lt;br&gt;
When you want to make your code self-documenting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Unnamed Fields&lt;/strong&gt;&lt;br&gt;
When initializing a struct using unnamed fields, you omit the field names and provide values in the exact order the fields are defined in the struct.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advantages of Unnamed Fields:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Conciseness:&lt;/strong&gt;&lt;br&gt;
It requires less typing and results in shorter code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages of Unnamed Fields:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. Order Dependency:&lt;/strong&gt;&lt;br&gt;
The values must appear in the exact order in which the fields are defined in the struct. If the order changes, it can lead to subtle bugs or incorrect data assignment.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c"&gt;// This will cause a type mismatch error.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Reduced Readability:&lt;/strong&gt;&lt;br&gt;
Without field names, it’s harder to immediately understand what each value represents, especially if the struct has multiple fields of the same type.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At a glance, it might not be clear which value corresponds to &lt;code&gt;ID&lt;/code&gt;, &lt;code&gt;Name&lt;/code&gt;, &lt;code&gt;Email&lt;/code&gt;, or &lt;code&gt;IsActive&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Which One Should You Use?&lt;/strong&gt;&lt;br&gt;
Whenever I decide to use a struct to tackle a problem, I lean toward using &lt;strong&gt;named fields&lt;/strong&gt; for clarity and maintainability. This is especially important in production code or collaborative projects, where readability and reducing errors are key.&lt;/p&gt;

&lt;p&gt;I might consider using &lt;strong&gt;unnamed fields&lt;/strong&gt; only if the struct is very small, the order of fields is obvious, and the context is extremely clear. Otherwise, named fields provide a safer and more reliable approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Default Values in Go Structs&lt;/strong&gt;&lt;br&gt;
In Go, when you create a struct instance without specifying values for all the fields, the default values (or "zero values") of the respective types are automatically assigned to the omitted fields. This behavior ensures that structs are always initialized, even if you don’t explicitly set all the fields.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Are Default (Zero) Values?&lt;/strong&gt;&lt;br&gt;
Zero values represent the "default state" of each data type in Go. I have several examples that will help you understand zero values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1: Omitted Fields in Struct Initialization&lt;/strong&gt;&lt;br&gt;
Consider the following struct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
&lt;span class="n"&gt;ID&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; 
&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; 
&lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; 
&lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you initialize an instance and omit some fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="c"&gt;// ID is initialized&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c"&gt;// Name is initialized&lt;/span&gt;
    &lt;span class="c"&gt;// Email and IsActive are omitted&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The omitted fields (&lt;code&gt;Email&lt;/code&gt; and &lt;code&gt;IsActive&lt;/code&gt;) take their zero values:&lt;br&gt;
Email (string): "" (empty string)&lt;br&gt;
IsActive (bool): false&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// {10 Doreen  false}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2: Empty Struct Initialization&lt;/strong&gt;&lt;br&gt;
If you don’t initialize any fields at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All fields will have their zero values:&lt;br&gt;
ID → 0 (default for int)&lt;br&gt;
Name → "" (default for string)&lt;br&gt;
Email → "" (default for string)&lt;br&gt;
IsActive → false (default for bool)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{0   false}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 3: Partial Initialization with Positional Values&lt;/strong&gt;&lt;br&gt;
If you use unnamed fields but don’t provide values for all fields, the omitted fields will still be set to their zero values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Johns"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="n"&gt;Johns&lt;/span&gt;  &lt;span class="no"&gt;false&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here:&lt;/strong&gt;&lt;br&gt;
Email (string) defaults to "".&lt;br&gt;
IsActive (bool) defaults to false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Are Zero Values Useful?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. Safety:&lt;/strong&gt;&lt;br&gt;
Go ensures that all fields are initialized, so there are no uninitialized fields causing undefined behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Simplicity:&lt;/strong&gt;&lt;br&gt;
You don’t have to explicitly initialize every field if you’re okay with some fields having default values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Convenience in Prototyping:&lt;/strong&gt;&lt;br&gt;
When quickly testing or prototyping, you can focus only on the fields you care about.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Avoid Reliance on Default Values for Logic&lt;/strong&gt;&lt;br&gt;
While Go's zero values (default values) are convenient, relying on them for important application logic can lead to subtle bugs and unintended behavior. This happens because the zero value for a type might not adequately represent your intended state or meaning.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is it that, Relying on Default Values Can Be Problematic?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. Lack of Clarity:&lt;/strong&gt;&lt;br&gt;
If a field is left uninitialized and automatically takes its zero value, it can be unclear whether the zero value was explicitly set by the programmer or simply defaulted. This ambiguity can make debugging difficult.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Unintended State:&lt;/strong&gt;&lt;br&gt;
If your code assumes a field is initialized with a meaningful value and processes the zero value instead, it could cause incorrect logic or results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Logic Errors:&lt;/strong&gt;&lt;br&gt;
Using default values might pass unnoticed in validation checks or business logic. For example, if 0 is a valid input for an int field, you won’t know if it was explicitly set or is the default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of the Problem&lt;/strong&gt;&lt;br&gt;
Imagine a struct for a user profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;      &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;   &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take it this way,suppose you write a logic that relies on &lt;code&gt;IsActive&lt;/code&gt; to determine if a user is active:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;CheckActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"User is active!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"User is inactive."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you accidentally leave &lt;code&gt;IsActive&lt;/code&gt; uninitialized when creating a user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c"&gt;// IsActive is omitted&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;CheckActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;inactive&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logic assumes that &lt;code&gt;IsActive&lt;/code&gt; = &lt;code&gt;false&lt;/code&gt; meaning the user is inactive, but the field wasn’t explicitly initialized—it defaulted to false. This could lead to misclassification of users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Practices to Avoid Issues&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. Explicit Initialization:&lt;/strong&gt;&lt;br&gt;
Always initialize fields explicitly if their value is meaningful to your logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;     &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c"&gt;// Explicitly set&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Use Pointers for Optional Fields:&lt;/strong&gt;&lt;br&gt;
If a field is optional or its absence is meaningful, use a pointer (*type) instead of relying on its zero value. This makes it clear whether a value was intentionally set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;      &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;   &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;isActive&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;span class="n"&gt;newUser&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;     &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;isActive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c"&gt;// Explicitly set&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, if &lt;code&gt;IsActive&lt;/code&gt; is nil, you know it was never set since we are taking its dereferenced value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Custom Default Values:&lt;/strong&gt;&lt;br&gt;
Define custom "default" values during struct initialization by using constructor functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;NewUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;     &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c"&gt;// Explicit default value&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;NewUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Johns"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"johns@example.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Outputs: false&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case &lt;code&gt;IsActive&lt;/code&gt; is false because it is omitted so it automatically takes the default value which is false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Validation Logic:&lt;/strong&gt;&lt;br&gt;
Include validation checks to ensure fields are initialized with appropriate values before use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;ValidateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name cannot be empty"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Email cannot be empty"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, empty &lt;code&gt;Name&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt; is not accepted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Use Structs in Go?&lt;/strong&gt;&lt;br&gt;
There are a number of features and advantages that structs come with that has, is and will always make me choose structs. Here's why structs are so valuable:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Organizing Related Data&lt;/strong&gt;&lt;br&gt;
Structs group related fields (variables) into a single logical unit. This makes your code more readable and manageable.&lt;br&gt;
Instead of managing multiple variables, you can handle them as a single Struct (User).&lt;br&gt;
Instead of initializing all these variables,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can always group them in one struct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;      &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;   &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Reusability&lt;/strong&gt;&lt;br&gt;
Structs enable you to define reusable types that can be used across different parts of your program.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;      &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Price&lt;/span&gt;   &lt;span class="kt"&gt;float64&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the above struct you can create multiple product instances without redefining fields each time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;product1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Laptop"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1200.50&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;product2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Smartphone"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;800.00&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Customization with Methods&lt;/strong&gt;&lt;br&gt;
Structs can have methods attached to them, allowing you to define custom behaviors for struct instances.&lt;br&gt;
Take a chill pill we are about to explore on methods.&lt;br&gt;
Here is an example to make you eager.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;     &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello, "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c"&gt;// Output: Hello, Doreen&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This enhances the struct’s functionality and encapsulates related behaviors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Encapsulation&lt;/strong&gt;&lt;br&gt;
Structs allow you to control access to their fields using export rules:&lt;br&gt;
&lt;strong&gt;Exported Fields:&lt;/strong&gt; Start with an uppercase letter and are accessible outside their package.&lt;br&gt;
&lt;strong&gt;Unexported Fields:&lt;/strong&gt; Start with a lowercase letter and are private to the package.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;      &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="c"&gt;// private field&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt;   &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="c"&gt;// public field&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Flexible and Extensible&lt;/strong&gt;&lt;br&gt;
Structs can be extended with composition, a common pattern in Go where you embed one struct inside another.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;City&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;State&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="c"&gt;// Embedded struct&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Address&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;City&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"Nairobi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Kenya"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;City&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Output: Nairobi&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. Efficient Memory Representation&lt;/strong&gt;&lt;br&gt;
Structs provide a compact and efficient way to group fields in memory. They use less overhead compared to alternatives like maps or slices for organizing data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Foundation for Object-Oriented Design&lt;/strong&gt;&lt;br&gt;
Although Go is not an object-oriented language, structs form the backbone of its type system. You can:&lt;br&gt;
Simulate classes by attaching methods to structs.&lt;br&gt;
Use interfaces to define shared behaviors between structs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Essential for JSON, XML, and Other Formats&lt;/strong&gt;&lt;br&gt;
Structs are often used to work with data serialization formats like JSON, XML, or databases. Go’s encoding packages (like encoding/json) can easily marshal and unmarshal structs.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; JSON Serialization&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"encoding/json"&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`json:"name"`&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`json:"email"`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;jsonData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonData&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// Output: {"name":"Doreen","email":"doreen@example.com"}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;9. Better Type Safety&lt;/strong&gt;&lt;br&gt;
Structs provide a more type-safe way to handle related data compared to alternatives like maps, where keys and values can have any type. With structs, field types are fixed, and the compiler can catch errors.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With a map:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="k"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;{}{&lt;/span&gt;&lt;span class="s"&gt;"ID"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Name"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With struct&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ID&lt;/span&gt;   &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler ensures that &lt;code&gt;ID&lt;/code&gt; is always an int and &lt;code&gt;Name&lt;/code&gt; is always a string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Customization with Methods in Go Structs
&lt;/h2&gt;

&lt;p&gt;In Go, you can attach methods to structs to define behaviors or actions related to the struct. This makes your code more organized, readable, and modular. It allows structs to encapsulate both data (fields) and related functionality (methods), mimicking the behavior of classes in object-oriented programming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Method?&lt;/strong&gt;&lt;br&gt;
A method in Go is simply a function that has a receiver. The receiver specifies the struct (or other type) the method is associated with. The method can then operate on the struct's fields.&lt;br&gt;
&lt;strong&gt;Syntax of a Method&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt; &lt;span class="n"&gt;ReceiverType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;MethodName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;returnType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// Method body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Receiver:&lt;/strong&gt; The variable name and type (e.g., (u User)) that the method is attached to.&lt;br&gt;
&lt;strong&gt;MethodName:&lt;/strong&gt; The name of the method.&lt;br&gt;
&lt;strong&gt;Parameters:&lt;/strong&gt; The inputs to the method (optional).&lt;br&gt;
&lt;strong&gt;ReturnType:&lt;/strong&gt; The type of value the method returns (optional).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Methods on a Struct&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Method attached to User struct&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello, "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Receiver:&lt;/strong&gt; (u User) associates the method Greet with the User struct.&lt;br&gt;
&lt;strong&gt;Purpose:&lt;/strong&gt; The method generates a greeting message using the &lt;code&gt;Name&lt;/code&gt; field of the struct.&lt;br&gt;
Using the Method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Greet&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c"&gt;// Output: Hello, Doreen&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Receiver Types: Value vs. Pointer&lt;/strong&gt;&lt;br&gt;
Methods can have either a value receiver or a pointer receiver, depending on how you want the method to interact with the struct's data.&lt;br&gt;
&lt;strong&gt;1. Value Receiver&lt;/strong&gt;&lt;br&gt;
A copy of the struct is passed to the method.&lt;br&gt;
Changes made to the struct within the method do not affect the original instance.&lt;br&gt;
You can use this when the method does not modify the struct or when the struct is small.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;DisplayStatus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt; &lt;span class="c"&gt;// This modifies a copy&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Inside method:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DisplayStatus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Outside method:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Pointer Receiver&lt;/strong&gt;&lt;br&gt;
Here a pointer to the struct is passed to the method.&lt;br&gt;
Changes made within the method affect the original struct.&lt;br&gt;
You can use this when the method modifies the struct or the struct is large.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Deactivate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsActive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt; &lt;span class="c"&gt;// Modifies the original instance&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Deactivate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"User status:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsActive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Methods with Parameters and Return Values&lt;/strong&gt;&lt;br&gt;
Methods can take additional parameters and return values like regular functions.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; Calculating Discount&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Method to calculate discounted price&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Discount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;product&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Laptop"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Discounted Price:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Discount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// 900&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Encapsulation and Abstraction&lt;/strong&gt;&lt;br&gt;
Methods allow you to encapsulate complex logic inside structs, exposing only necessary details to the user.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; Encapsulating Validation Logic&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;IsValidEmail&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"@"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Johns"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Email&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"johns@example.com"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Valid email:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsValidEmail&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c"&gt;// true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logic for validating the email is encapsulated in the &lt;code&gt;IsValidEmail&lt;/code&gt; method, keeping it separate from other parts of the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chaining Methods&lt;/strong&gt;&lt;br&gt;
Since methods can return the struct itself (or a pointer to it), you can chain multiple methods together for more concise and readable code.&lt;br&gt;
&lt;strong&gt;Example:&lt;/strong&gt; Chaining User Updates&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SetName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SetEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Doreen"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"doreen@example.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// &amp;amp;{Doreen doreen@example.com}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Benefits of Methods in Structs&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Code Organization:&lt;/strong&gt; Groups related data (fields) and functionality (methods) together.&lt;br&gt;
&lt;strong&gt;Readability:&lt;/strong&gt; Methods clearly express the actions or behaviors of a struct.&lt;br&gt;
&lt;strong&gt;Encapsulation:&lt;/strong&gt; Keeps implementation details hidden and exposes only necessary functionality.&lt;br&gt;
&lt;strong&gt;Reusability:&lt;/strong&gt; Methods can operate on struct instances in various parts of the program.&lt;br&gt;
&lt;strong&gt;Extensibility:&lt;/strong&gt; Easily add new behaviors without altering external code.&lt;br&gt;
By attaching methods to structs, you can make your Go programs more modular, maintainable, and expressive!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Structs in Go are powerful, flexible, and integral to writing clean, efficient, and organized code. They are the go-to choice when you need to represent real-world entities, manage data, or implement object-oriented principles. By mastering structs, you can create scalable and maintainable Go applications.&lt;br&gt;
By attaching methods to structs, you can make your Go programs more modular, maintainable, and expressive!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
