<?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: Huseyn</title>
    <description>The latest articles on DEV Community by Huseyn (@jad_core).</description>
    <link>https://dev.to/jad_core</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%2F3200674%2F86636551-8b3a-4774-ab31-d7bd2e322ba0.jpg</url>
      <title>DEV Community: Huseyn</title>
      <link>https://dev.to/jad_core</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jad_core"/>
    <language>en</language>
    <item>
      <title>Online Learning Problem</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Mon, 29 Dec 2025 07:18:56 +0000</pubDate>
      <link>https://dev.to/jad_core/online-learning-problem-4k2o</link>
      <guid>https://dev.to/jad_core/online-learning-problem-4k2o</guid>
      <description>&lt;h1&gt;
  
  
  Online Learning Problem (OLP)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  What is Online Learning?
&lt;/h2&gt;

&lt;p&gt;Online learning is a learning paradigm where a model &lt;strong&gt;learns sequentially while making decisions&lt;/strong&gt;, without access to the full dataset beforehand. At each step, the learner must act first and learn from feedback afterward.&lt;/p&gt;

&lt;p&gt;Unlike offline (batch) learning, online learning operates in &lt;strong&gt;real time&lt;/strong&gt; and continuously updates its strategy.&lt;/p&gt;




&lt;h2&gt;
  
  
  Standard Online Learning Framework
&lt;/h2&gt;

&lt;p&gt;At each round (t):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The learner selects an action or prediction (a_t)&lt;/li&gt;
&lt;li&gt;The environment reveals the outcome (y_t)&lt;/li&gt;
&lt;li&gt;The learner incurs a loss (\ell(a_t, y_t)) or receives a reward&lt;/li&gt;
&lt;li&gt;The learner updates its decision rule&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Future data is never known in advance.&lt;/p&gt;




&lt;h2&gt;
  
  
  Performance Measure: Regret
&lt;/h2&gt;

&lt;p&gt;Online learning performance is measured using &lt;strong&gt;regret&lt;/strong&gt;, defined as:&lt;/p&gt;

&lt;p&gt;[&lt;br&gt;
\text{Regret}(T) = \sum_{t=1}^{T} \ell(a_t) - \min_{a} \sum_{t=1}^{T} \ell(a)&lt;br&gt;
]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interpretation:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How much worse did the learner perform compared to the best fixed decision chosen in hindsight?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Low regret implies effective learning.&lt;/p&gt;




&lt;h2&gt;
  
  
  Types of Online Learning Problems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Online Prediction with Expert Advice
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The learner combines predictions from multiple experts&lt;/li&gt;
&lt;li&gt;Goal: perform almost as well as the best expert in hindsight&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Online Convex Optimization
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Loss functions are convex&lt;/li&gt;
&lt;li&gt;Common in large-scale machine learning&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Bandit Feedback
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Only the loss or reward of the chosen action is observed&lt;/li&gt;
&lt;li&gt;Limited feedback setting&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Contextual Online Learning
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Decisions depend on observed context or features&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Bandit Models
&lt;/h1&gt;

&lt;h2&gt;
  
  
  What is a Bandit Model?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;(multi-armed) bandit model&lt;/strong&gt; is a special case of online learning where the learner repeatedly chooses among several actions (arms), each with an unknown reward distribution.&lt;/p&gt;

&lt;p&gt;The learner observes feedback &lt;strong&gt;only for the selected arm&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Exploration vs Exploitation Trade-off
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Exploration:&lt;/strong&gt; trying uncertain actions to gain information&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exploitation:&lt;/strong&gt; choosing the action that currently appears best&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Balancing these two is the core challenge of bandit problems.&lt;/p&gt;




&lt;h2&gt;
  
  
  Formal Bandit Setting
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A finite set of arms: (A = {1, 2, \dots, K})&lt;/li&gt;
&lt;li&gt;Each arm has an unknown reward distribution&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;At each round:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select one arm&lt;/li&gt;
&lt;li&gt;Observe its reward&lt;/li&gt;
&lt;li&gt;Update beliefs&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Performance is measured using &lt;strong&gt;regret&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Types of Bandit Models
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Stochastic Bandits
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Rewards drawn from fixed but unknown distributions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Contextual Bandits
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Arm choice depends on observed context&lt;/li&gt;
&lt;li&gt;Used in recommender systems and ads&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Adversarial Bandits
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Rewards may be chosen by an adversary&lt;/li&gt;
&lt;li&gt;No statistical assumptions&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Common Bandit Algorithms
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;(\varepsilon)-greedy&lt;/li&gt;
&lt;li&gt;Upper Confidence Bound (UCB)&lt;/li&gt;
&lt;li&gt;Thompson Sampling&lt;/li&gt;
&lt;li&gt;EXP3 (adversarial setting)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Relationship Between Online Learning and Bandits
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;Online Learning&lt;/th&gt;
&lt;th&gt;Bandit Learning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Feedback&lt;/td&gt;
&lt;td&gt;Full loss information&lt;/td&gt;
&lt;td&gt;Partial (chosen action only)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Information&lt;/td&gt;
&lt;td&gt;More&lt;/td&gt;
&lt;td&gt;Less&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scope&lt;/td&gt;
&lt;td&gt;General framework&lt;/td&gt;
&lt;td&gt;Special case&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;All bandit problems are online learning problems, but not all online learning problems are bandits.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Applications
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Adaptive routing and forwarding&lt;/li&gt;
&lt;li&gt;Caching and resource allocation&lt;/li&gt;
&lt;li&gt;Recommender systems&lt;/li&gt;
&lt;li&gt;Online advertising&lt;/li&gt;
&lt;li&gt;Congestion control&lt;/li&gt;
&lt;li&gt;Real-time decision systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Takeaway
&lt;/h2&gt;

&lt;p&gt;Online learning focuses on &lt;strong&gt;learning while acting&lt;/strong&gt;, and bandit models address this challenge under &lt;strong&gt;limited feedback&lt;/strong&gt; conditions.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>ai</category>
      <category>tutorial</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Understanding Domains, DNS, Servers, and Reverse Proxies</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Mon, 22 Sep 2025 02:00:23 +0000</pubDate>
      <link>https://dev.to/jad_core/understanding-domains-dns-servers-and-reverse-proxies-17bm</link>
      <guid>https://dev.to/jad_core/understanding-domains-dns-servers-and-reverse-proxies-17bm</guid>
      <description>&lt;p&gt;A structured guide to demystify domain names, DNS records, servers, and how everything works together with Nginx/Apache.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Domain Basics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;domain name&lt;/strong&gt; (e.g., &lt;code&gt;myapp.com&lt;/code&gt;) is just a human-readable label.
&lt;/li&gt;
&lt;li&gt;When you &lt;strong&gt;buy a domain&lt;/strong&gt;, you’re only buying the &lt;strong&gt;name&lt;/strong&gt;, not the hosting or server.
&lt;/li&gt;
&lt;li&gt;To make it work, you must &lt;strong&gt;link the domain to a server IP&lt;/strong&gt; (where your app is hosted).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. DNS Records (Mapping Domain to Server)
&lt;/h2&gt;

&lt;p&gt;DNS records define how your domain/subdomain points to IPs or other domains.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;A Record&lt;/strong&gt; → Maps domain → IPv4 address&lt;br&gt;&lt;br&gt;
Example:  myapp.com A 123.45.67.89&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;AAAA Record&lt;/strong&gt; → Maps domain → IPv6 address  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CNAME Record&lt;/strong&gt; → Maps domain → another domain&lt;br&gt;&lt;br&gt;
Example:  &lt;a href="http://www.myapp.com" rel="noopener noreferrer"&gt;www.myapp.com&lt;/a&gt; CNAME myapp.com&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Subdomains&lt;/strong&gt;:  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;api.myapp.com A 123.45.67.89&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;blog.myapp.com A 123.45.67.89&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;→ All point to the same server IP.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. How Subdomains Work on a Single Server
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Your server usually has &lt;strong&gt;one public IP&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;All subdomains (&lt;code&gt;api.myapp.com&lt;/code&gt;, &lt;code&gt;blog.myapp.com&lt;/code&gt;) resolve to this same IP.
&lt;/li&gt;
&lt;li&gt;When the request reaches the server, the &lt;strong&gt;web server (Nginx/Apache)&lt;/strong&gt; checks the &lt;strong&gt;Host header&lt;/strong&gt; to know which site/subdomain was requested.
&lt;/li&gt;
&lt;li&gt;Based on that, it serves different apps (via &lt;em&gt;Virtual Hosts&lt;/em&gt; or &lt;em&gt;server blocks&lt;/em&gt;).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. Role of Web Servers (Nginx / Apache)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Nginx or Apache does:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Listens on &lt;strong&gt;ports 80 (HTTP)&lt;/strong&gt; and &lt;strong&gt;443 (HTTPS)&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;Reads the &lt;strong&gt;Host header&lt;/strong&gt; (&lt;code&gt;api.myapp.com&lt;/code&gt;, &lt;code&gt;blog.myapp.com&lt;/code&gt;).
&lt;/li&gt;
&lt;li&gt;Matches it with configuration (&lt;code&gt;sites-available&lt;/code&gt; in Nginx).
&lt;/li&gt;
&lt;li&gt;Serves the correct project folder or &lt;strong&gt;proxies&lt;/strong&gt; the request to backend apps.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Project Files
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Static apps (React, Angular, etc.)&lt;/strong&gt;: Deploy the &lt;strong&gt;build folder&lt;/strong&gt; (HTML, JS, CSS).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic apps (Node.js, Django, etc.)&lt;/strong&gt;: Run app on internal port (e.g., &lt;code&gt;5000&lt;/code&gt;), then &lt;strong&gt;reverse proxy&lt;/strong&gt; with Nginx.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Reverse Proxy Explained
&lt;/h2&gt;

&lt;p&gt;Why do we need it?  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Backend apps don’t (and shouldn’t) run directly on port 80/443.
&lt;/li&gt;
&lt;li&gt;They usually run on &lt;strong&gt;local ports&lt;/strong&gt; like &lt;code&gt;5000&lt;/code&gt;, &lt;code&gt;8000&lt;/code&gt;, etc.
&lt;/li&gt;
&lt;li&gt;Outsiders don’t see or access those ports.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Reverse Proxy = Gateway&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User → &lt;code&gt;https://api.myapp.com&lt;/code&gt; (port 80/443).
&lt;/li&gt;
&lt;li&gt;Nginx forwards it → &lt;code&gt;http://localhost:5000&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;App responds → Nginx sends back to user.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Why Not Direct Backend Usage?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Security: Don’t expose raw app ports.
&lt;/li&gt;
&lt;li&gt;Clean URLs: Users see only &lt;code&gt;myapp.com&lt;/code&gt;, no &lt;code&gt;:5000&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;SSL: Nginx handles HTTPS certificates.
&lt;/li&gt;
&lt;li&gt;Load Balancing: Can distribute across multiple backend servers.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  7. Example Setup with Nginx
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Backend
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run a Node.js/Django app on: &lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Nginx Config
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kn"&gt;server_name&lt;/span&gt; &lt;span class="s"&gt;api.myapp.com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kn"&gt;proxy_pass&lt;/span&gt; &lt;span class="s"&gt;http://localhost:5000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;Host&lt;/span&gt; &lt;span class="nv"&gt;$host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;X-Real-IP&lt;/span&gt; &lt;span class="nv"&gt;$remote_addr&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;h3&gt;
  
  
  Flow
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Browser → &lt;a href="https://api.myapp.com/login" rel="noopener noreferrer"&gt;https://api.myapp.com/login&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;Request hits Nginx (port 80/443)&lt;/li&gt;
&lt;li&gt;Nginx forwards to backend (localhost:5000)&lt;/li&gt;
&lt;li&gt;Backend responds → Nginx passes it back → Browser shows result&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Summary Workflow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Buy domain (get the name).&lt;/li&gt;
&lt;li&gt;Set DNS records → Domain → Server IP.&lt;/li&gt;
&lt;li&gt;Server receives requests → Nginx/Apache listens.&lt;/li&gt;
&lt;li&gt;Nginx decides → Based on domain/Host header.&lt;/li&gt;
&lt;li&gt;Serve files (static build) OR proxy to backend (dynamic apps).&lt;/li&gt;
&lt;li&gt;User only sees clean domain (no internal ports).&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>networking</category>
      <category>architecture</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>GitHub Actions Cheatsheet and Workflow Analysis</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Wed, 16 Jul 2025 17:26:38 +0000</pubDate>
      <link>https://dev.to/jad_core/github-actions-cheatsheet-and-workflow-analysis-4ae4</link>
      <guid>https://dev.to/jad_core/github-actions-cheatsheet-and-workflow-analysis-4ae4</guid>
      <description>&lt;h2&gt;
  
  
  GitHub Actions Cheatsheet
&lt;/h2&gt;

&lt;p&gt;GitHub Actions is a CI/CD platform for automating tasks in GitHub repositories. Workflows are defined in YAML files stored in the &lt;code&gt;.github/workflows/&lt;/code&gt; directory. Below is a cheatsheet of all major components and their roles.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Workflow&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: A workflow is an automated process defined in a YAML file, containing one or more jobs triggered by events.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key Attributes&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: Workflow name displayed in the GitHub Actions UI.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;on&lt;/code&gt;: Specifies events that trigger the workflow (e.g., &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pull_request&lt;/code&gt;, &lt;code&gt;schedule&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;jobs&lt;/code&gt;: Defines one or more jobs to execute.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;env&lt;/code&gt;: Workflow-level environment variables.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;concurrency&lt;/code&gt;: Controls concurrent workflow runs to prevent conflicts.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;CI Pipeline&lt;/span&gt;
  &lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;main&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;GLOBAL_VAR&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;strong&gt;Events (&lt;code&gt;on&lt;/code&gt;)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Events trigger workflows. Defined under the &lt;code&gt;on&lt;/code&gt; key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Common Triggers&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;push&lt;/code&gt;: On code push to specified branches.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pull_request&lt;/code&gt;: On pull request actions (e.g., opened, synchronized).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;schedule&lt;/code&gt;: Cron-based scheduling (e.g., &lt;code&gt;0 0 * * *&lt;/code&gt; for daily runs).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;workflow_dispatch&lt;/code&gt;: Manual trigger via GitHub UI or API.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;issue_comment&lt;/code&gt;, &lt;code&gt;release&lt;/code&gt;, &lt;code&gt;repository_dispatch&lt;/code&gt;, etc.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;main&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;dev/*&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;main&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;schedule&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;cron&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;strong&gt;Jobs&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: A job is a set of tasks (steps) running on a single runner (virtual machine or container).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key Attributes&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;runs-on&lt;/code&gt;: Specifies the runner (e.g., &lt;code&gt;ubuntu-latest&lt;/code&gt;, &lt;code&gt;windows-latest&lt;/code&gt;, &lt;code&gt;macos-latest&lt;/code&gt;, or self-hosted).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;steps&lt;/code&gt;: List of tasks to execute.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;needs&lt;/code&gt;: Defines job dependencies for sequential execution.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;env&lt;/code&gt;: Job-level environment variables.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;if&lt;/code&gt;: Conditional execution (e.g., &lt;code&gt;if: github.event_name == 'push'&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;services&lt;/code&gt;: Defines Docker containers for external dependencies (e.g., databases).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;strategy&lt;/code&gt;: For matrix builds (run job with multiple configurations, e.g., different Go versions).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
      &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. &lt;strong&gt;Steps&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Individual tasks within a job, executed sequentially on the same runner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key Attributes&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: Step name for UI display.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;uses&lt;/code&gt;: References an action (e.g., &lt;code&gt;actions/checkout@v4&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;run&lt;/code&gt;: Executes a shell command (e.g., &lt;code&gt;npm install&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;env&lt;/code&gt;: Step-level environment variables.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;if&lt;/code&gt;: Conditional execution.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;with&lt;/code&gt;: Inputs for actions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt;: Unique identifier for step outputs.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;continue-on-error&lt;/code&gt;: Allows subsequent steps to run even if the step fails.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run script&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;echo "Hello, World!"&lt;/span&gt;
      &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;MY_VAR&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Actions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Reusable units of code that perform specific tasks (e.g., checking out code, setting up environments).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key Attributes&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Hosted in GitHub Marketplace or custom repositories.&lt;/li&gt;
&lt;li&gt;Used via &lt;code&gt;uses&lt;/code&gt; (e.g., &lt;code&gt;uses: actions/setup-go@v5&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Supports inputs (&lt;code&gt;with&lt;/code&gt;) and outputs.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-go@v5&lt;/span&gt;
    &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;go-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1.21'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. &lt;strong&gt;Services&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Docker containers running alongside a job to provide dependencies (e.g., databases, caches).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key Attributes&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Defined under &lt;code&gt;jobs.&amp;lt;job_id&amp;gt;.services&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Specifies &lt;code&gt;image&lt;/code&gt;, &lt;code&gt;ports&lt;/code&gt;, &lt;code&gt;env&lt;/code&gt;, and &lt;code&gt;options&lt;/code&gt; (e.g., health checks).&lt;/li&gt;
&lt;li&gt;Accessible via &lt;code&gt;localhost&lt;/code&gt; or service name.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis:latest&lt;/span&gt;
      &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;6379:6379&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkn810ore4s4p0xx0clvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkn810ore4s4p0xx0clvz.png" alt=" " width="800" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.playbook.com/s/sajjad-courses-collections/fCBc8K5qWAbtCkDnnWi97foa?assetToken=rjSbi7LVQyC5vGJHLtqKdEc7" rel="noopener noreferrer"&gt;Checkout the PPT&lt;/a&gt; . Credit goes to: &lt;a href="https://www.udemy.com/course/backend-master-class-golang-postgresql-kubernetes/?couponCode=MT150725C#instructor-1" rel="noopener noreferrer"&gt;TechSchool&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;Runners&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Virtual machines or containers that execute jobs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Types&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;GitHub-hosted: &lt;code&gt;ubuntu-latest&lt;/code&gt;, &lt;code&gt;windows-latest&lt;/code&gt;, &lt;code&gt;macos-latest&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Self-hosted: Custom runners on your infrastructure.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. &lt;strong&gt;Environment Variables&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Variables available to steps, jobs, or workflows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Levels&lt;/strong&gt;: Workflow (&lt;code&gt;env&lt;/code&gt;), job (&lt;code&gt;env&lt;/code&gt;), step (&lt;code&gt;env&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secrets&lt;/strong&gt;: Securely stored variables accessed via &lt;code&gt;${{ secrets.NAME }}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;API_KEY&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.API_KEY }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  9. &lt;strong&gt;Matrix Strategy&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Runs a job multiple times with different configurations (e.g., multiple versions of a language).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Key Attributes&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;strategy.matrix&lt;/code&gt;: Defines variables (e.g., &lt;code&gt;go-version: ['1.21', '1.22']&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;strategy.fail-fast&lt;/code&gt;: Stops matrix on first failure (default: &lt;code&gt;true&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;strategy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;matrix&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;go-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1.21'&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1.22'&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  10. &lt;strong&gt;Artifacts and Outputs&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Artifacts&lt;/strong&gt;: Files stored after a job (e.g., build outputs).

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;actions/upload-artifact@v4&lt;/code&gt; and &lt;code&gt;actions/download-artifact@v4&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Outputs&lt;/strong&gt;: Data passed between steps or jobs via &lt;code&gt;::set-output&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Upload artifact&lt;/span&gt;
    &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/upload-artifact@v4&lt;/span&gt;
    &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-artifact&lt;/span&gt;
      &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./build/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  11. &lt;strong&gt;Concurrency and Permissions&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency&lt;/strong&gt;: Prevents multiple workflow runs from conflicting.

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;concurrency.group&lt;/code&gt; and &lt;code&gt;concurrency.cancel-in-progress&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Permissions&lt;/strong&gt;: Control workflow access to repository resources.

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;permissions&lt;/code&gt; (e.g., &lt;code&gt;contents: read&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;concurrency&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;group&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ github.workflow }}-${{ github.ref }}&lt;/span&gt;
    &lt;span class="na"&gt;cancel-in-progress&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;contents&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  12. &lt;strong&gt;Common Actions&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;actions/checkout@v4&lt;/code&gt;: Clones the repository.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;actions/setup-&amp;lt;language&amp;gt;@vX&lt;/code&gt;: Sets up languages (e.g., &lt;code&gt;setup-go&lt;/code&gt;, &lt;code&gt;setup-node&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;actions/upload-artifact@v4&lt;/code&gt;: Uploads files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;actions/download-artifact@v4&lt;/code&gt;: Downloads files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;actions/cache@v4&lt;/code&gt;: Caches dependencies for faster builds.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  13. &lt;strong&gt;Expressions and Contexts&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Expressions&lt;/strong&gt;: Use &lt;code&gt;${{ &amp;lt;expression&amp;gt; }}&lt;/code&gt; for dynamic values (e.g., &lt;code&gt;${{ github.sha }}&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contexts&lt;/strong&gt;: Predefined objects like &lt;code&gt;github&lt;/code&gt;, &lt;code&gt;env&lt;/code&gt;, &lt;code&gt;secrets&lt;/code&gt;, &lt;code&gt;matrix&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ github.event_name == 'push' }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  14. &lt;strong&gt;Best Practices&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use specific action versions (e.g., &lt;code&gt;@v4&lt;/code&gt; instead of &lt;code&gt;@main&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Store sensitive data in GitHub Secrets.&lt;/li&gt;
&lt;li&gt;Break workflows into modular jobs for clarity.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;continue-on-error&lt;/code&gt; for non-critical steps.&lt;/li&gt;
&lt;li&gt;Leverage caching to speed up workflows (&lt;code&gt;actions/cache@v4&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more details, see the &lt;a href="https://docs.github.com/en/actions" rel="noopener noreferrer"&gt;GitHub Actions Documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis of the Provided Go Workflow YAML
&lt;/h2&gt;

&lt;p&gt;The provided YAML file defines a GitHub Actions workflow named &lt;code&gt;unit-test&lt;/code&gt; for a Go project with a PostgreSQL database. Below is a detailed breakdown of the workflow, referencing the cheatsheet components.&lt;/p&gt;

&lt;h3&gt;
  
  
  YAML File
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This workflow will build a golang project&lt;/span&gt;
&lt;span class="c1"&gt;# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go&lt;/span&gt;

&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unit-test&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;main"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;dev/sajjad/users"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;main"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Test&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;postgres&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres:latest&lt;/span&gt;
        &lt;span class="c1"&gt;# Provide the password for postgres&lt;/span&gt;
        &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;root&lt;/span&gt;
          &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;root&lt;/span&gt;
          &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;simple_bank&lt;/span&gt;
        &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;5432:5432&lt;/span&gt;
        &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;-&lt;/span&gt;
          &lt;span class="s"&gt;--health-cmd pg_isready&lt;/span&gt;
          &lt;span class="s"&gt;--health-interval 10s&lt;/span&gt;
          &lt;span class="s"&gt;--health-timeout 5s&lt;/span&gt;
          &lt;span class="s"&gt;--health-retries 5&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up Go&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-go@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;go-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1.24.3"&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Check out code to runner&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Install go-migrate&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
          &lt;span class="s"&gt;$ curl -L https://github.com/golang-migrate/migrate/releases/download/v4.18.3/migrate.linux-amd64.tar.gz | tar xvz&lt;/span&gt;
          &lt;span class="s"&gt;sudo mv migrate.linux-amd64 /usr/bin/migrate&lt;/span&gt;
          &lt;span class="s"&gt;which Migrate&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run migrations&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;make migrateup&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Test&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;make test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Component Breakdown
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Workflow&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Name&lt;/strong&gt;: &lt;code&gt;unit-test&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Purpose&lt;/strong&gt;: Automates unit testing for a Go project with a PostgreSQL database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;File Location&lt;/strong&gt;: Stored in &lt;code&gt;.github/workflows/&lt;/code&gt; (not specified in the YAML but implied).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Events (&lt;code&gt;on&lt;/code&gt;)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Triggers&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;push: branches: ["main", "dev/sajjad/users"]&lt;/code&gt;: Runs on pushes to the &lt;code&gt;main&lt;/code&gt; branch and the &lt;code&gt;dev/sajjad/users&lt;/code&gt; branch.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pull_request: branches: ["main"]&lt;/code&gt;: Runs on pull requests targeting the &lt;code&gt;main&lt;/code&gt; branch.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: This ensures the workflow runs for code changes to specific branches and pull requests, supporting both development and production workflows.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Jobs&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Job Name&lt;/strong&gt;: &lt;code&gt;test&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runner&lt;/strong&gt;: &lt;code&gt;runs-on: ubuntu-latest&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Uses a GitHub-hosted Ubuntu virtual machine (latest version).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Purpose&lt;/strong&gt;: Executes the test-related tasks for the Go project.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Services&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Service Name&lt;/strong&gt;: &lt;code&gt;postgres&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;image: postgres:latest&lt;/code&gt;: Uses the latest PostgreSQL Docker image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;env&lt;/code&gt;:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POSTGRES_USER: root&lt;/code&gt;: Database user.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POSTGRES_PASSWORD: root&lt;/code&gt;: Database password.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POSTGRES_DB: simple_bank&lt;/code&gt;: Database name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ports: - 5432:5432&lt;/code&gt;: Maps port 5432 for access via &lt;code&gt;localhost:5432&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;options&lt;/code&gt;: Health checks ensure the database is ready:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--health-cmd pg_isready&lt;/code&gt;: Checks if PostgreSQL is accepting connections.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--health-interval 10s&lt;/code&gt;, &lt;code&gt;--health-timeout 5s&lt;/code&gt;, &lt;code&gt;--health-retries 5&lt;/code&gt;: Retries 5 times every 10 seconds with a 5-second timeout.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: The PostgreSQL service provides a database for testing and migrations, accessible by the job’s steps.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Steps&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1: Set up Go&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;uses: actions/setup-go@v4&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;with: go-version: "1.24.3"&lt;/code&gt;: Installs Go version 1.24.3.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: Sets up the Go environment required for building and testing.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2: Check out code to runner&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;uses: actions/checkout@v4&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: Clones the repository to the runner, making the code available for subsequent steps.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3: Install go-migrate&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;run&lt;/code&gt;: Downloads and installs &lt;code&gt;go-migrate&lt;/code&gt;分散
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   curl &lt;span class="nt"&gt;-L&lt;/span&gt; https://github.com/golang-migrate/migrate/releases/download/v4.18.3/migrate.linux-amd64.tar.gz | &lt;span class="nb"&gt;tar &lt;/span&gt;xvz
   &lt;span class="nb"&gt;sudo mv &lt;/span&gt;migrate.linux-amd64 /usr/bin/migrate
   which Migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; - **Explanation**: Installs the `go-migrate` CLI (version 4.18.3) to manage database migrations.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 4: Run migrations&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;run: make migrateup&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: Runs the &lt;code&gt;migrateup&lt;/code&gt; Makefile target to apply database migrations to the PostgreSQL database.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Step 5: Test&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;run: make test&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation&lt;/strong&gt;: Runs the &lt;code&gt;test&lt;/code&gt; Makefile target to execute the Go tests, likely using the PostgreSQL database.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  How the Workflow Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Trigger&lt;/strong&gt;: The workflow runs on:

&lt;ul&gt;
&lt;li&gt;Pushes to the &lt;code&gt;main&lt;/code&gt; or &lt;code&gt;dev/sajjad/users&lt;/code&gt; branches.&lt;/li&gt;
&lt;li&gt;Pull requests targeting the &lt;code&gt;main&lt;/code&gt; branch.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runner Setup&lt;/strong&gt;: An &lt;code&gt;ubuntu-latest&lt;/code&gt; virtual machine is provisioned.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Service Startup&lt;/strong&gt;: The &lt;code&gt;postgres&lt;/code&gt; service starts a PostgreSQL container with the &lt;code&gt;simple_bank&lt;/code&gt; database, accessible at &lt;code&gt;localhost:5432&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step Execution&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Installs Go 1.24.3.&lt;/li&gt;
&lt;li&gt;Clones the repository.&lt;/li&gt;
&lt;li&gt;Installs the &lt;code&gt;go-migrate&lt;/code&gt; CLI.&lt;/li&gt;
&lt;li&gt;Runs database migrations using &lt;code&gt;make migrateup&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Executes tests using &lt;code&gt;make test&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Completion&lt;/strong&gt;: The workflow completes, marking success or failure in the GitHub Actions UI. The PostgreSQL container is terminated.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Key Notes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Makefile Integration&lt;/strong&gt;: The workflow uses &lt;code&gt;make&lt;/code&gt; commands (&lt;code&gt;migrateup&lt;/code&gt;, &lt;code&gt;test&lt;/code&gt;), indicating the project has a &lt;code&gt;Makefile&lt;/code&gt; to simplify task execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt;: Hardcoded &lt;code&gt;POSTGRES_USER&lt;/code&gt; and &lt;code&gt;POSTGRES_PASSWORD&lt;/code&gt; (&lt;code&gt;root&lt;/code&gt;) are used for simplicity. In production, use GitHub Secrets for sensitive data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Health Checks&lt;/strong&gt;: The PostgreSQL service uses health checks to ensure readiness, preventing steps from running before the database is available.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Potential Improvement&lt;/strong&gt;: The &lt;code&gt;go-migrate&lt;/code&gt; installation command has an extra &lt;code&gt;$&lt;/code&gt; before &lt;code&gt;curl&lt;/code&gt;, which may cause errors. It should be:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  curl &lt;span class="nt"&gt;-L&lt;/span&gt; https://github.com/golang-migrate/migrate/releases/download/v4.18.3/migrate.linux-amd64.tar.gz | &lt;span class="nb"&gt;tar &lt;/span&gt;xvz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Additional Tips for This Workflow
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Add Environment Variables&lt;/strong&gt;: Explicitly define &lt;code&gt;DATABASE_URL&lt;/code&gt; for clarity:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres://root:root@localhost:5432/simple_bank?sslmode=disable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in the &lt;code&gt;Run migrations&lt;/code&gt; and &lt;code&gt;Test&lt;/code&gt; steps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Caching&lt;/strong&gt;: Use &lt;code&gt;actions/cache@v4&lt;/code&gt; to cache Go modules for faster builds:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Cache Go modules&lt;/span&gt;
    &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/cache@v4&lt;/span&gt;
    &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;~/go/pkg/mod&lt;/span&gt;
      &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ runner.os }}-go-${{ hashFiles('**/go.mod') }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Matrix Testing&lt;/strong&gt;: Test multiple Go versions:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;strategy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;matrix&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;go-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1.21'&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1.22'&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1.24.3'&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For further customization, refer to the &lt;a href="https://docs.github.com/en/actions" rel="noopener noreferrer"&gt;GitHub Actions Documentation&lt;/a&gt; and &lt;a href="https://github.com/golang-migrate/migrate" rel="noopener noreferrer"&gt;go-migrate Documentation&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>github</category>
      <category>cicd</category>
      <category>go</category>
      <category>backend</category>
    </item>
    <item>
      <title>Database Concurrency Phenomena &amp; ISOLATION Label: Read Phenomena and Serialization Anomaly</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Tue, 15 Jul 2025 10:16:00 +0000</pubDate>
      <link>https://dev.to/jad_core/database-concurrency-phenomena-isolation-label-read-phenomena-and-serialization-anomaly-3485</link>
      <guid>https://dev.to/jad_core/database-concurrency-phenomena-isolation-label-read-phenomena-and-serialization-anomaly-3485</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In database systems, concurrent transactions can lead to issues that affect data consistency. These issues, known as &lt;strong&gt;read phenomena&lt;/strong&gt; (dirty reads, non-repeatable reads, phantom reads) and &lt;strong&gt;serialization anomaly&lt;/strong&gt;, occur when multiple transactions read and write data simultaneously. This document defines and explains these phenomena with examples to illustrate their impact in a generic database context.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concurrency Phenomena
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Dirty Reads
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: A transaction reads data that has been modified by another transaction but not yet committed. If the modifying transaction rolls back, the read data becomes invalid.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Transaction 1 updates a customer’s balance to 90 but hasn’t committed.&lt;/li&gt;
&lt;li&gt;Transaction 2 reads the balance as 90.&lt;/li&gt;
&lt;li&gt;Transaction 1 rolls back, restoring the balance to 100.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Issue&lt;/strong&gt;: Transaction 2 operates on incorrect data (90 instead of 100).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Impact&lt;/strong&gt;: Can lead to decisions based on temporary, uncommitted data, causing inconsistencies in applications like financial systems or inventory management.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Non-Repeatable Reads
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: A transaction reads the same data twice and gets different results because another transaction modified and committed the data between the reads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Transaction 1 reads a product’s stock quantity as 50.&lt;/li&gt;
&lt;li&gt;Transaction 2 updates the stock to 40 and commits.&lt;/li&gt;
&lt;li&gt;Transaction 1 reads the stock again and sees 40.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Issue&lt;/strong&gt;: The same query within Transaction 1 yields inconsistent results.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Impact&lt;/strong&gt;: Inconsistent reads can cause errors in applications requiring stable data, such as order processing or reporting.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Phantom Reads
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: A transaction re-executes a query to retrieve a set of rows and finds different rows (new or missing) because another transaction inserted or deleted rows and committed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Transaction 1 queries all orders for a customer, finding 3 orders.&lt;/li&gt;
&lt;li&gt;Transaction 2 inserts a new order for the same customer and commits.&lt;/li&gt;
&lt;li&gt;Transaction 1 re-runs the query and finds 4 orders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Issue&lt;/strong&gt;: The result set changes due to “phantom” rows appearing.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Impact&lt;/strong&gt;: Can affect operations that rely on a consistent set of records, such as generating reports or auditing transactions.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Serialization Anomaly
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: A serialization anomaly occurs when the outcome of concurrently executing transactions cannot be explained by any serial (one-at-a-time) execution order of those transactions, even though each transaction is individually consistent. This is specific to the &lt;code&gt;SERIALIZABLE&lt;/code&gt; isolation level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Initial State: Three records (A: 100, B: 100, C: 100).&lt;/li&gt;
&lt;li&gt;Transaction 1 updates A to 90 and B to 110.&lt;/li&gt;
&lt;li&gt;Transaction 2 updates B to 90 and C to 110.&lt;/li&gt;
&lt;li&gt;Transaction 3 updates C to 90 and A to 110.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expected Outcome&lt;/strong&gt; (serial execution): Balances remain A: 100, B: 100, C: 100 (net zero change).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anomaly&lt;/strong&gt;: Concurrent execution might result in inconsistent balances (e.g., A: 90, B: 100, C: 110) because transactions interleave in a way that doesn’t match any serial order.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Issue&lt;/strong&gt;: The final state is inconsistent with expected business logic, despite each transaction being valid.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Impact&lt;/strong&gt;: Can lead to incorrect data states in applications where the combined effect of transactions must align with a specific logical outcome, such as financial ledgers.&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  SQL Transaction Isolation Levels
&lt;/h2&gt;

&lt;p&gt;Transaction isolation levels, defined by the SQL standard, control how transactions interact with each other and manage concurrency issues like read phenomena and serialization anomalies. The four standard isolation levels, from least to most strict, are described below, including their impact on the phenomena discussed above.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. READ UNCOMMITTED
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: The least strict isolation level, allowing transactions to read uncommitted changes made by other transactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Behavior&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Transactions can see data modified by other transactions before they commit.&lt;/li&gt;
&lt;li&gt;Offers the highest concurrency but the lowest consistency.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Impact on Phenomena&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dirty Reads&lt;/strong&gt;: Possible, as uncommitted data is visible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Repeatable Reads&lt;/strong&gt;: Possible, as committed changes from other transactions can alter data between reads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phantom Reads&lt;/strong&gt;: Possible, as new rows inserted by other transactions can appear.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serialization Anomaly&lt;/strong&gt;: Possible, as there’s no guarantee of serial execution.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

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

&lt;ul&gt;
&lt;li&gt;Transaction 1 updates a record but doesn’t commit.&lt;/li&gt;
&lt;li&gt;Transaction 2 reads the uncommitted data, which may be rolled back, leading to a dirty read.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Use Case&lt;/strong&gt;: Rarely used in practice due to its high risk of inconsistencies; suitable only for applications where data accuracy is not critical (e.g., approximate analytics).&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. READ COMMITTED
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Ensures that transactions only read data that has been committed, preventing dirty reads but allowing other concurrency issues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Behavior&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Each query within a transaction sees only committed data at the time of the query.&lt;/li&gt;
&lt;li&gt;Common default isolation level in many databases, including PostgreSQL.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Impact on Phenomena&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dirty Reads&lt;/strong&gt;: Prevented, as only committed data is visible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Repeatable Reads&lt;/strong&gt;: Possible, as data can change between queries within the same transaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phantom Reads&lt;/strong&gt;: Possible, as new rows can be inserted by other transactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serialization Anomaly&lt;/strong&gt;: Possible, as there’s no guarantee of serial execution.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

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

&lt;ul&gt;
&lt;li&gt;Transaction 1 reads a record’s value as 50.&lt;/li&gt;
&lt;li&gt;Transaction 2 updates the value to 40 and commits.&lt;/li&gt;
&lt;li&gt;Transaction 1 reads the same record again and sees 40 (non-repeatable read).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Use Case&lt;/strong&gt;: Suitable for applications where dirty reads are unacceptable but some inconsistency (e.g., non-repeatable reads) is tolerable, such as simple reporting systems.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. REPEATABLE READ
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Ensures that data read by a transaction remains consistent throughout the transaction, preventing non-repeatable reads but not phantom reads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Behavior&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Uses a snapshot of committed data at the start of the transaction for all reads, or locks read rows to prevent modifications.&lt;/li&gt;
&lt;li&gt;In PostgreSQL, this is implemented using multiversion concurrency control (MVCC).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Impact on Phenomena&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dirty Reads&lt;/strong&gt;: Prevented, as only committed data is visible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Repeatable Reads&lt;/strong&gt;: Prevented, as the transaction sees a consistent snapshot of data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phantom Reads&lt;/strong&gt;: Possible, as new rows inserted by other transactions may appear in subsequent queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serialization Anomaly&lt;/strong&gt;: Possible, as concurrent transactions can still produce non-serializable outcomes.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

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

&lt;ul&gt;
&lt;li&gt;Transaction 1 reads all records matching a condition (e.g., 3 orders).&lt;/li&gt;
&lt;li&gt;Transaction 2 inserts a new record matching the condition and commits.&lt;/li&gt;
&lt;li&gt;Transaction 1 re-queries and sees the new record (phantom read).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Use Case&lt;/strong&gt;: Suitable for applications requiring consistent reads within a transaction, such as financial calculations or inventory checks, but where phantom reads are acceptable.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. SERIALIZABLE
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: The strictest isolation level, ensuring that transactions execute as if they were run sequentially, preventing all concurrency anomalies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Behavior&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Guarantees that the outcome of concurrent transactions matches some serial execution order.&lt;/li&gt;
&lt;li&gt;In PostgreSQL, uses predicate locking and MVCC to detect conflicts, aborting transactions (error code &lt;code&gt;40001&lt;/code&gt;) if a serialization anomaly is detected.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Impact on Phenomena&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dirty Reads&lt;/strong&gt;: Prevented, as only committed data is visible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Repeatable Reads&lt;/strong&gt;: Prevented, as the transaction sees a consistent snapshot.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phantom Reads&lt;/strong&gt;: Prevented, as conflicting inserts/deletes trigger a serialization failure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serialization Anomaly&lt;/strong&gt;: Prevented, as the database ensures a serializable outcome.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

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

&lt;ul&gt;
&lt;li&gt;Transaction 1 and Transaction 2 attempt conflicting updates that would lead to a serialization anomaly (e.g., cyclic updates to records).&lt;/li&gt;
&lt;li&gt;The database detects the conflict and aborts one transaction, requiring a retry.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Use Case&lt;/strong&gt;: Ideal for applications requiring strict data consistency, such as financial systems or critical business logic, though it may reduce concurrency due to potential transaction aborts.&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Isolation Levels and Concurrency Phenomena Table
&lt;/h2&gt;

&lt;p&gt;This table illustrates the relationship between SQL transaction isolation levels and the concurrency phenomena they allow or prevent.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Phenomenon&lt;/th&gt;
&lt;th&gt;READ UNCOMMITTED&lt;/th&gt;
&lt;th&gt;READ COMMITTED&lt;/th&gt;
&lt;th&gt;REPEATABLE READ&lt;/th&gt;
&lt;th&gt;SERIALIZABLE&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Dirty Read&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Non-Repeatable Read&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Phantom Read&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Serialization Anomaly&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Yes&lt;/strong&gt;: The phenomenon can occur under the specified isolation level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No&lt;/strong&gt;: The phenomenon is prevented under the specified isolation level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolation Levels&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;READ UNCOMMITTED&lt;/strong&gt;: Allows all phenomena due to minimal restrictions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;READ COMMITTED&lt;/strong&gt;: Prevents dirty reads but allows others.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;REPEATABLE READ&lt;/strong&gt;: Prevents dirty and non-repeatable reads but allows phantom reads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SERIALIZABLE&lt;/strong&gt;: Prevents all phenomena, ensuring full consistency.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dirty Reads&lt;/strong&gt;: Reading uncommitted data (possible in READ UNCOMMITTED).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-Repeatable Reads&lt;/strong&gt;: Inconsistent results from repeated reads (possible in READ UNCOMMITTED and READ COMMITTED).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phantom Reads&lt;/strong&gt;: Changes in query result sets due to inserts/deletes (possible in READ UNCOMMITTED, READ COMMITTED, and REPEATABLE READ).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serialization Anomaly&lt;/strong&gt;: Non-serializable outcomes from concurrent transactions (possible in all but SERIALIZABLE).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolation Levels&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;READ UNCOMMITTED&lt;/strong&gt;: Allows all phenomena; least consistent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;READ COMMITTED&lt;/strong&gt;: Prevents dirty reads; moderate consistency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;REPEATABLE READ&lt;/strong&gt;: Prevents dirty and non-repeatable reads; higher consistency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SERIALIZABLE&lt;/strong&gt;: Prevents all phenomena; maximum consistency but may impact performance.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Understanding these phenomena and isolation levels is crucial for designing robust database applications, as they guide the choice of isolation level to balance consistency and performance based on application requirements.&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
    </item>
    <item>
      <title>Database Transactions: A Comprehensive Guide with Go</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Sat, 12 Jul 2025 17:32:30 +0000</pubDate>
      <link>https://dev.to/jad_core/database-transactions-a-comprehensive-guide-with-go-2e52</link>
      <guid>https://dev.to/jad_core/database-transactions-a-comprehensive-guide-with-go-2e52</guid>
      <description>&lt;h2&gt;
  
  
  What is a Database Transaction?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;database transaction&lt;/strong&gt; (often called a "TX") is a sequence of one or more database operations (e.g., INSERT, UPDATE, DELETE) executed as a single logical unit. Transactions ensure data integrity and consistency by guaranteeing that either &lt;em&gt;all&lt;/em&gt; operations succeed or &lt;em&gt;none&lt;/em&gt; are applied, maintaining the database in a consistent state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Characteristics - &lt;strong&gt;ACID&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Atomicity&lt;/strong&gt;: All operations in a transaction are completed successfully, or none are applied (all-or-nothing).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt;: The database transitions from one valid state to another, adhering to all constraints (e.g., foreign keys, unique constraints).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolation&lt;/strong&gt;: Transactions are executed independently, preventing interference from concurrent transactions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Durability&lt;/strong&gt;: Committed transactions are permanently saved, even in the event of a system failure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: Transferring $100 from Account A to Account B involves two operations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Deduct $100 from Account A.&lt;/li&gt;
&lt;li&gt;Add $100 to Account B.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These must be part of a single transaction to avoid inconsistencies (e.g., money deducted but not credited).&lt;/p&gt;

&lt;h2&gt;
  
  
  Ensuring Transactions
&lt;/h2&gt;

&lt;p&gt;To ensure transactions are reliable, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Begin a Transaction&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with &lt;code&gt;BEGIN&lt;/code&gt; or &lt;code&gt;START TRANSACTION&lt;/code&gt; in SQL or use the Go &lt;code&gt;database/sql&lt;/code&gt; package’s &lt;code&gt;Begin()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;This marks the transaction’s start, tracking all operations as a single unit.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Execute Operations&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Perform SQL queries within the transaction.&lt;/li&gt;
&lt;li&gt;Changes are held in a temporary state until committed or rolled back.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Commit the Transaction&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;COMMIT&lt;/code&gt; in SQL or &lt;code&gt;Tx.Commit()&lt;/code&gt; in Go to make changes permanent.&lt;/li&gt;
&lt;li&gt;Ensures durability, saving changes to disk.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Rollback on Failure&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;ROLLBACK&lt;/code&gt; in SQL or &lt;code&gt;Tx.Rollback()&lt;/code&gt; in Go to undo changes if an error occurs.&lt;/li&gt;
&lt;li&gt;Ensures atomicity, restoring the database to its pre-transaction state.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Set Isolation Levels&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use isolation levels (e.g., READ COMMITTED, SERIALIZABLE) to control how transactions interact.&lt;/li&gt;
&lt;li&gt;In Go, set the isolation level when beginning a transaction (e.g., &lt;code&gt;db.BeginTx(context.Background(), &amp;amp;sql.TxOptions{Isolation: sql.LevelSerializable})&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Error Handling&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement robust error handling in Go to catch failures (e.g., constraint violations) and trigger rollbacks.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Database State in Transactions
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;database state&lt;/strong&gt; refers to the data at any given moment. Transactions ensure valid state transitions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initial State&lt;/strong&gt;: The database is consistent, satisfying all constraints.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Temporary State&lt;/strong&gt;: During a transaction, changes are held in memory or logs (e.g., using write-ahead logging or MVCC) and are not visible to other transactions until committed (depending on isolation level).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Committed State&lt;/strong&gt;: On &lt;code&gt;COMMIT&lt;/code&gt;, changes are written to permanent storage, creating a new consistent state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rolled-Back State&lt;/strong&gt;: On &lt;code&gt;ROLLBACK&lt;/code&gt;, changes are discarded, and the database reverts to its initial state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency&lt;/strong&gt;: Isolation levels and mechanisms like MVCC (in PostgreSQL) ensure concurrent transactions see consistent snapshots.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Happens if a Transaction Fails?
&lt;/h2&gt;

&lt;p&gt;Failures can occur due to errors, crashes, or concurrency issues. The DBMS and application handle these as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Query Errors&lt;/strong&gt;: If a query fails (e.g., due to a constraint violation), the transaction is rolled back, undoing all changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System Crashes&lt;/strong&gt;: Write-ahead logging ensures committed changes are recovered, and uncommitted changes are discarded on restart.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deadlocks&lt;/strong&gt;: The DBMS detects deadlocks and aborts one transaction, requiring the application to retry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application Handling&lt;/strong&gt;: In Go, use error handling to detect failures and call &lt;code&gt;Tx.Rollback()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementing Transactions in Go
&lt;/h2&gt;

&lt;p&gt;Go’s &lt;code&gt;database/sql&lt;/code&gt; package provides robust support for transactions, compatible with databases like PostgreSQL, MySQL (InnoDB), and SQLite. Below is an example of a transaction in Go using PostgreSQL to transfer money between accounts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Go Example: Bank Transfer Transaction
&lt;/h3&gt;

&lt;p&gt;This example demonstrates a transaction to transfer $100 from Account A to Account B, with error handling and rollback.&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;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"context"&lt;/span&gt;
    &lt;span class="s"&gt;"database/sql"&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
    &lt;span class="s"&gt;"log"&lt;/span&gt;

    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="s"&gt;"github.com/lib/pq"&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="c"&gt;// Connect to PostgreSQL database&lt;/span&gt;
    &lt;span class="n"&gt;connStr&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"user=youruser password=yourpassword dbname=yourdb sslmode=disable"&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;connStr&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;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to connect to database:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c"&gt;// Begin a transaction&lt;/span&gt;
    &lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Background&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BeginTx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TxOptions&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Isolation&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LevelReadCommitted&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;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to begin transaction:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Execute transaction operations&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExecContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"UPDATE accounts SET balance = balance - 100 WHERE account_id = $1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"A"&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;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rollback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to deduct from Account A:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExecContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"UPDATE accounts SET balance = balance + 100 WHERE account_id = $1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"B"&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;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rollback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to credit Account B:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Check for sufficient balance (example constraint)&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;
    &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;QueryRowContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"SELECT balance FROM accounts WHERE account_id = $1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;balance&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;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rollback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to check balance:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&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;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rollback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Insufficient balance in Account A"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;// Commit the transaction&lt;/span&gt;
    &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Commit&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;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rollback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to commit transaction:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&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="s"&gt;"Transaction completed successfully!"&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;h3&gt;
  
  
  Explanation of Go Code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Database Connection&lt;/strong&gt;: Uses &lt;code&gt;sql.Open&lt;/code&gt; to connect to a PostgreSQL database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Begin Transaction&lt;/strong&gt;: &lt;code&gt;db.BeginTx&lt;/code&gt; starts a transaction with a specified isolation level (&lt;code&gt;sql.LevelReadCommitted&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execute Queries&lt;/strong&gt;: &lt;code&gt;tx.ExecContext&lt;/code&gt; runs SQL queries within the transaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: Checks for errors after each query and calls &lt;code&gt;tx.Rollback()&lt;/code&gt; if any occur.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Balance Check&lt;/strong&gt;: Queries the balance to enforce a business rule (e.g., no negative balance).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commit or Rollback&lt;/strong&gt;: Calls &lt;code&gt;tx.Commit()&lt;/code&gt; to save changes or &lt;code&gt;tx.Rollback()&lt;/code&gt; to undo them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Install the PostgreSQL driver: &lt;code&gt;go get github.com/lib/pq&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Ensure a PostgreSQL database with an &lt;code&gt;accounts&lt;/code&gt; table:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;account_id&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'B'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common Issues and Mitigations
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Deadlocks&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Issue&lt;/strong&gt;: Multiple transactions lock resources in conflicting orders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mitigation&lt;/strong&gt;: Access tables in a consistent order, use shorter transactions, and catch deadlock errors (&lt;code&gt;sql.ErrTxDone&lt;/code&gt; or database-specific errors) to retry.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Issue&lt;/strong&gt;: Long transactions hold locks, reducing concurrency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mitigation&lt;/strong&gt;: Optimize queries, use appropriate indexes, and break large transactions into smaller ones.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lost Updates&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Issue&lt;/strong&gt;: Concurrent transactions overwrite each other’s changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mitigation&lt;/strong&gt;: Use stricter isolation levels (e.g., &lt;code&gt;sql.LevelSerializable&lt;/code&gt;) or optimistic locking.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Connection Management&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Issue&lt;/strong&gt;: Improper connection handling can lead to leaks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mitigation&lt;/strong&gt;: Always defer &lt;code&gt;db.Close()&lt;/code&gt; and ensure &lt;code&gt;tx.Rollback()&lt;/code&gt; or &lt;code&gt;tx.Commit()&lt;/code&gt; is called.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Best Practices in Go
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use Context&lt;/strong&gt;: Pass a &lt;code&gt;context.Context&lt;/code&gt; to &lt;code&gt;BeginTx&lt;/code&gt; and query methods to support cancellation and timeouts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explicit Rollbacks&lt;/strong&gt;: Always call &lt;code&gt;tx.Rollback()&lt;/code&gt; in error paths, even if &lt;code&gt;tx.Commit()&lt;/code&gt; fails, as some DBMSs require it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connection Pooling&lt;/strong&gt;: Leverage &lt;code&gt;database/sql&lt;/code&gt;’s built-in connection pooling by reusing the &lt;code&gt;sql.DB&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolation Levels&lt;/strong&gt;: Choose the appropriate isolation level for your use case to balance consistency and performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing&lt;/strong&gt;: Test transaction failure scenarios (e.g., constraint violations, network issues) to ensure robust error handling.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Database transactions ensure data integrity by adhering to ACID properties. In Go, the &lt;code&gt;database/sql&lt;/code&gt; package provides a clean and efficient way to manage transactions, with robust error handling and rollback support. By following best practices and understanding database state transitions, you can build reliable applications that maintain consistency even in the face of failures.&lt;/p&gt;

&lt;p&gt;For further details, explore the Go &lt;code&gt;database/sql&lt;/code&gt; documentation and your DBMS’s transaction management features (e.g., PostgreSQL’s MV assumptions.&lt;/p&gt;

</description>
      <category>database</category>
      <category>go</category>
      <category>backend</category>
    </item>
    <item>
      <title>Database Schema Migration Cheatsheet with golang-migrate/migrate</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Sat, 12 Jul 2025 10:10:17 +0000</pubDate>
      <link>https://dev.to/jad_core/database-schema-migration-cheatsheet-with-golang-migratemigrate-35b3</link>
      <guid>https://dev.to/jad_core/database-schema-migration-cheatsheet-with-golang-migratemigrate-35b3</guid>
      <description>&lt;p&gt;This cheatsheet provides a concise guide to using the &lt;code&gt;golang-migrate/migrate&lt;/code&gt; library for database schema migrations. It covers installation, file structure, migration commands (up, down, force down), and best practices.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Overview
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;What is golang-migrate/migrate?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A CLI tool and Go library for managing database schema migrations.&lt;/li&gt;
&lt;li&gt;Supports incremental, version-controlled, and reversible changes to database schemas.&lt;/li&gt;
&lt;li&gt;Compatible with databases like PostgreSQL, MySQL, SQLite, and more.&lt;/li&gt;
&lt;li&gt;Migrations are written as SQL files (or Go code for advanced cases).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Key Concepts&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Up Migration&lt;/strong&gt;: Applies changes to evolve the schema (e.g., create tables, add columns).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Down Migration&lt;/strong&gt;: Reverts changes to roll back the schema (e.g., drop tables, remove columns).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Force Down&lt;/strong&gt;: Resolves issues with a "dirty" database by forcing a specific version.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Installation
&lt;/h2&gt;

&lt;p&gt;Install the &lt;code&gt;migrate&lt;/code&gt; CLI tool for your operating system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Go installed (for Go-based CLI compilation, optional).&lt;/li&gt;
&lt;li&gt;A supported database (e.g., PostgreSQL, MySQL) with connection details.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Install via Package Managers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MacOS (Homebrew)&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  brew &lt;span class="nb"&gt;install &lt;/span&gt;golang-migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Linux (via curl)&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  curl &lt;span class="nt"&gt;-L&lt;/span&gt; https://github.com/golang-migrate/migrate/releases/download/v4.17.1/migrate.linux-amd64.tar.gz | &lt;span class="nb"&gt;tar &lt;/span&gt;xvz
  &lt;span class="nb"&gt;sudo mv &lt;/span&gt;migrate /usr/local/bin/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Windows (Scoop)&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Verify Installation&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  migrate &lt;span class="nt"&gt;-version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Install via Go (Optional)
&lt;/h3&gt;

&lt;p&gt;If you prefer to build from source or use as a library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go get &lt;span class="nt"&gt;-u&lt;/span&gt; github.com/golang-migrate/migrate/v4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. File Structure
&lt;/h2&gt;

&lt;p&gt;Organize migration files in a dedicated directory for version control and clarity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recommended Structure
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/
├── database/
│   ├── migrations/
│   │   ├── 202507120001_create_users_table.up.sql
│   │   ├── 202507120001_create_users_table.down.sql
│   │   ├── 202507120002_add_email_index.up.sql
│   │   ├── 202507120002_add_email_index.down.sql
│   └── schema.sql  (optional: schema dump)
├── main.go
└── Makefile  (optional: for migration shortcuts)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Naming Convention&lt;/strong&gt;: Files are named &lt;code&gt;&amp;lt;timestamp&amp;gt;_&amp;lt;description&amp;gt;.&amp;lt;up/down&amp;gt;.sql&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;Example: &lt;code&gt;202507120001_create_users_table.up.sql&lt;/code&gt; for creating a table.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;202507120001_create_users_table.down.sql&lt;/code&gt; for dropping it.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Directory Path&lt;/strong&gt;: Typically &lt;code&gt;database/migrations/&lt;/code&gt; (relative to project root).&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example Migration Files
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;202507120001_create_users_table.up.sql&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="c1"&gt;-- Create users table&lt;/span&gt;
  &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;CURRENT_TIMESTAMP&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;202507120001_create_users_table.down.sql&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="c1"&gt;-- Drop users table&lt;/span&gt;
  &lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;202507120002_add_email_index.up.sql&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="c1"&gt;-- Add index on email column&lt;/span&gt;
  &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_email&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;202507120002_add_email_index.down.sql&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="c1"&gt;-- Drop index on email column&lt;/span&gt;
  &lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;idx_email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Usage
&lt;/h2&gt;

&lt;p&gt;Run migrations using the &lt;code&gt;migrate&lt;/code&gt; CLI with a database connection string and migration path.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Commands
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Create a New Migration&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  migrate create &lt;span class="nt"&gt;-ext&lt;/span&gt; sql &lt;span class="nt"&gt;-dir&lt;/span&gt; database/migrations &lt;span class="nt"&gt;-seq&lt;/span&gt; create_table_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-ext sql&lt;/code&gt;: Specifies SQL file extension.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-dir&lt;/code&gt;: Path to migration directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-seq&lt;/code&gt;: Generates a sequential timestamp.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example output: Creates &lt;code&gt;database/migrations/202507120003_create_table_name.up.sql&lt;/code&gt; and &lt;code&gt;.down.sql&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Apply Migrations (Up)&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://username:password@localhost:5432/dbname?sslmode=disable"&lt;/span&gt; &lt;span class="nt"&gt;-verbose&lt;/span&gt; up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Applies all pending migrations in order.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-path&lt;/code&gt;: Directory containing migration files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-database&lt;/code&gt;: Database connection string (replace with your DB details).&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;-verbose&lt;/code&gt;: Shows detailed output.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Revert Migrations (Down)&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://username:password@localhost:5432/dbname?sslmode=disable"&lt;/span&gt; &lt;span class="nt"&gt;-verbose&lt;/span&gt; down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Reverts the last applied migration.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;down N&lt;/code&gt; to revert the last &lt;code&gt;N&lt;/code&gt; migrations (e.g., &lt;code&gt;down 2&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Force Down (Fix Dirty Database)&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://username:password@localhost:5432/dbname?sslmode=disable"&lt;/span&gt; force VERSION
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use when a migration fails and marks the database as "dirty," preventing further migrations.&lt;/li&gt;
&lt;li&gt;Replace &lt;code&gt;VERSION&lt;/code&gt; with the desired migration version (e.g., &lt;code&gt;202507120001&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caution&lt;/strong&gt;: Ensure the database state matches the forced version to avoid inconsistencies.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example: If migration &lt;code&gt;202507120002&lt;/code&gt; fails, fix the error in the &lt;code&gt;.up.sql&lt;/code&gt; file, then run:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://username:password@localhost:5432/dbname?sslmode=disable"&lt;/span&gt; force 202507120001
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This sets the database to version &lt;code&gt;202507120001&lt;/code&gt;, clearing the dirty state.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Check Migration Status&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://username:password@localhost:5432/dbname?sslmode=disable"&lt;/span&gt; version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Displays the current schema version.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using with a Makefile (Optional)
&lt;/h3&gt;

&lt;p&gt;Simplify commands with a &lt;code&gt;Makefile&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="c"&gt;# Makefile
&lt;/span&gt;&lt;span class="nv"&gt;DB_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgresql://username:password@localhost:5432/dbname?sslmode&lt;span class="o"&gt;=&lt;/span&gt;disable

&lt;span class="nl"&gt;migration_up&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;$(&lt;/span&gt;&lt;span class="s2"&gt;DB_URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-verbose&lt;/span&gt; up

&lt;span class="nl"&gt;migration_down&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;$(&lt;/span&gt;&lt;span class="s2"&gt;DB_URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-verbose&lt;/span&gt; down

&lt;span class="nl"&gt;migration_force&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;$(&lt;/span&gt;&lt;span class="s2"&gt;DB_URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; force &lt;span class="p"&gt;$(&lt;/span&gt;VERSION&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run: &lt;code&gt;make migration_up&lt;/code&gt;, &lt;code&gt;make migration_down&lt;/code&gt;, or &lt;code&gt;make migration_force VERSION=202507120001&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Version Control&lt;/strong&gt;: Commit migration files to your repository (e.g., Git) for team collaboration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test Migrations&lt;/strong&gt;: Test migrations in a staging environment before applying to production.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reversible Migrations&lt;/strong&gt;: Always define &lt;code&gt;down&lt;/code&gt; migrations to allow rollbacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoid Destructive Changes&lt;/strong&gt;: Use non-destructive changes (e.g., add nullable columns) to minimize data loss risks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backup Database&lt;/strong&gt;: Always back up your database before running migrations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handle Dirty Databases&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;If a migration fails, check the error, fix the &lt;code&gt;.up.sql&lt;/code&gt; or &lt;code&gt;.down.sql&lt;/code&gt; file, and use &lt;code&gt;force VERSION&lt;/code&gt; cautiously.&lt;/li&gt;
&lt;li&gt;Verify the database state matches the forced version.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Common Issues and Solutions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Error: Dirty database version X. Fix and force version&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Cause: A migration failed, leaving the database in a "dirty" state.&lt;/li&gt;
&lt;li&gt;Solution: Fix the problematic migration file, then run &lt;code&gt;migrate force X&lt;/code&gt; to set the correct version.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Migration order issues&lt;/strong&gt;: Ensure migration files are applied sequentially based on timestamps.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Connection errors&lt;/strong&gt;: Verify the database connection string and ensure the database is running.&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Example Workflow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a migration:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   migrate create &lt;span class="nt"&gt;-ext&lt;/span&gt; sql &lt;span class="nt"&gt;-dir&lt;/span&gt; database/migrations &lt;span class="nt"&gt;-seq&lt;/span&gt; add_posts_table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Edit &lt;code&gt;database/migrations/202507120003_add_posts_table.up.sql&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="c1"&gt;-- Create posts table&lt;/span&gt;
   &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
       &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&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;title&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
   &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Edit &lt;code&gt;database/migrations/202507120003_add_posts_table.down.sql&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="c1"&gt;-- Drop posts table&lt;/span&gt;
   &lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Apply the migration:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://username:password@localhost:5432/dbname?sslmode=disable"&lt;/span&gt; up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Revert if needed:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://username:password@localhost:5432/dbname?sslmode=disable"&lt;/span&gt; down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;If migration fails and database is dirty:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fix the error in the migration file.&lt;/li&gt;
&lt;li&gt;Force to the last successful version:
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; migrate &lt;span class="nt"&gt;-path&lt;/span&gt; database/migrations/ &lt;span class="nt"&gt;-database&lt;/span&gt; &lt;span class="s2"&gt;"postgresql://username:password@localhost:5432/dbname?sslmode=disable"&lt;/span&gt; force 202507120002
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  8. Additional Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Official GitHub: &lt;a href="https://github.com/golang-migrate/migrate" rel="noopener noreferrer"&gt;golang-migrate/migrate&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Supported Databases: PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, and more.&lt;/li&gt;
&lt;li&gt;CLI Documentation: Run &lt;code&gt;migrate -help&lt;/code&gt; for full command details.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>go</category>
      <category>backend</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>Complete Guide: Using `sqlc` with Your Go Project</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Mon, 07 Jul 2025 09:29:55 +0000</pubDate>
      <link>https://dev.to/jad_core/complete-guide-using-sqlc-with-your-go-project-e2b</link>
      <guid>https://dev.to/jad_core/complete-guide-using-sqlc-with-your-go-project-e2b</guid>
      <description>&lt;h2&gt;
  
  
  🚀 What is &lt;code&gt;sqlc&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;sqlc&lt;/code&gt; is a code generation tool that converts your SQL queries into type-safe Go functions. It eliminates manual boilerplate code and avoids ORMs.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅ Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Type-safe queries&lt;/li&gt;
&lt;li&gt;No ORM overhead&lt;/li&gt;
&lt;li&gt;Native SQL with full control&lt;/li&gt;
&lt;li&gt;Auto-generated Go models &amp;amp; methods&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  📦 Installation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  CLI
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;sqlc  &lt;span class="c"&gt;# macOS&lt;/span&gt;
&lt;span class="c"&gt;# or&lt;/span&gt;
go &lt;span class="nb"&gt;install &lt;/span&gt;github.com/sqlc-dev/sqlc/cmd/sqlc@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  📁 Recommended Project Structure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myapp/
├── db/
│   ├── migrations/
│   ├── queries/
│   │   ├── users.sql
│   └── schema.sql
├── sqlc.yaml
├── go.mod
└── main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⚙️ &lt;code&gt;sqlc.yaml&lt;/code&gt; Configuration
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2"&lt;/span&gt;
&lt;span class="na"&gt;sql&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;engine&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;postgresql"&lt;/span&gt;
    &lt;span class="na"&gt;queries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;db/queries/"&lt;/span&gt;
    &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;db/migrations/"&lt;/span&gt;
    &lt;span class="na"&gt;gen&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;go&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;package&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;db"&lt;/span&gt;
        &lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;db/sqlc"&lt;/span&gt;
        &lt;span class="na"&gt;sql_package&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pgx/v5"&lt;/span&gt;
        &lt;span class="na"&gt;emit_json_tags&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="na"&gt;emit_interface&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ✍️ Writing SQL Queries
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;db/queries/users.sql&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- name: CreateUser :one&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;RETURNING&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;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- name: GetUser :one&lt;/span&gt;
&lt;span class="k"&gt;SELECT&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;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- name: ListUsers :many&lt;/span&gt;
&lt;span class="k"&gt;SELECT&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;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔖 Query Types in &lt;code&gt;sqlc&lt;/code&gt;
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tag&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;th&gt;Go Return Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:one&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns a single row. Fails if no row or more than one is returned. Use it for &lt;code&gt;SELECT&lt;/code&gt; with unique constraint or primary key filters.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(Model, error)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:many&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns multiple rows as a slice. Use for general &lt;code&gt;SELECT&lt;/code&gt; queries that return 0 to many rows.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;([]Model, error)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:exec&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Executes query with no result rows. Use for &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;, or &lt;code&gt;DELETE&lt;/code&gt; that don't return rows.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(error)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:execrows&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Executes and also returns the number of rows affected. Useful when you care how many rows were impacted by &lt;code&gt;UPDATE&lt;/code&gt; or &lt;code&gt;DELETE&lt;/code&gt;.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(int64, error)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:copyfrom&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Generates a method using PostgreSQL's COPY FROM. Use for fast bulk insert operations.&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(int64, error)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  📄 Example Migration (&lt;code&gt;db/migrations/init.up.sql&lt;/code&gt;)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🛠️ Generating Go Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sqlc generate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creates Go code in &lt;code&gt;db/sqlc&lt;/code&gt; with models and query methods.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧑‍💻 Using in Go Code
&lt;/h2&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="p"&gt;(&lt;/span&gt;
  &lt;span class="s"&gt;"context"&lt;/span&gt;
  &lt;span class="s"&gt;"database/sql"&lt;/span&gt;
  &lt;span class="s"&gt;"fmt"&lt;/span&gt;
  &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="s"&gt;"github.com/lib/pq"&lt;/span&gt;
  &lt;span class="s"&gt;"myapp/db/sqlc"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;dbConn&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;sql&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"postgres"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"postgresql://root:root@localhost:5432/simple_bank?sslmode=disable"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sqlc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dbConn&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;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Background&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="s"&gt;"esha"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"esha@email.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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧹 Handling Errors
&lt;/h2&gt;

&lt;p&gt;If you see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;error: Dirty database version &lt;span class="nt"&gt;-1&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; Fix and force version.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;migrate ... force 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;migrate ... up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ✅ Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;pgx/v5&lt;/code&gt; for performance&lt;/li&gt;
&lt;li&gt;Never edit applied migrations&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;.PHONY&lt;/code&gt; Makefile targets to run &lt;code&gt;sqlc generate&lt;/code&gt;, &lt;code&gt;migrate&lt;/code&gt;, etc.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;More&lt;/strong&gt;&lt;br&gt;
These annotations like &lt;code&gt;:one&lt;/code&gt;, &lt;code&gt;:many&lt;/code&gt;, &lt;code&gt;:exec&lt;/code&gt;, etc., are special &lt;strong&gt;&lt;code&gt;sqlc&lt;/code&gt; query tags&lt;/strong&gt; that tell &lt;code&gt;sqlc&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What kind of Go function to generate&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What kind of return value to expect&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They appear in SQL comments just before each query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- name: MyFunctionName :&amp;lt;type&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;SQL&lt;/span&gt; &lt;span class="k"&gt;statement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s break them down:&lt;/p&gt;




&lt;h2&gt;
  
  
  🔖 &lt;code&gt;sqlc&lt;/code&gt; Query Types Explained
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tag&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;th&gt;Return Type (Go)&lt;/th&gt;
&lt;th&gt;Use When...&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:one&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Return exactly &lt;strong&gt;one row&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(Type, error)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;SELECT that should return a single record&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:many&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Return &lt;strong&gt;multiple rows&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;([]Type, error)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;SELECT with multiple rows expected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:exec&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Execute&lt;/strong&gt; query, no result rows&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(error)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;INSERT/UPDATE/DELETE without returning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:execrows&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Like &lt;code&gt;:exec&lt;/code&gt; but also returns rows affected&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(int64, error)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;UPDATE/DELETE where you want affected row count&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;:copyfrom&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Use &lt;strong&gt;COPY FROM&lt;/strong&gt; PostgreSQL bulk import&lt;/td&gt;
&lt;td&gt;&lt;code&gt;(int64, error)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;For high-speed bulk insert&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  🧠 Examples
&lt;/h3&gt;




&lt;h3&gt;
  
  
  ✅ &lt;code&gt;:one&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- name: GetUserByID :one&lt;/span&gt;
&lt;span class="k"&gt;SELECT&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;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generates:&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;q&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Queries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;GetUserByID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&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;int32&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="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ &lt;code&gt;:many&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- name: ListUsers :many&lt;/span&gt;
&lt;span class="k"&gt;SELECT&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;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generates:&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;q&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Queries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;ListUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&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="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ &lt;code&gt;:exec&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- name: DeleteUser :exec&lt;/span&gt;
&lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generates:&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;q&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Queries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;DeleteUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&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;int32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ &lt;code&gt;:execrows&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- name: UpdateEmail :execrows&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generates:&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;q&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Queries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;UpdateEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&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;int32&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="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int64&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ &lt;code&gt;:copyfrom&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- name: CopyUsers :copyfrom&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;STDIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generates:&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;q&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Queries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;CopyUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Reader&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int64&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Used for &lt;strong&gt;efficient bulk data import&lt;/strong&gt; with a CSV reader or similar.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧠 Summary Cheat Sheet
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You want to...&lt;/th&gt;
&lt;th&gt;Use this&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SELECT 1 row&lt;/td&gt;
&lt;td&gt;&lt;code&gt;:one&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SELECT many rows&lt;/td&gt;
&lt;td&gt;&lt;code&gt;:many&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INSERT/UPDATE/DELETE (no rows)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;:exec&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UPDATE and get row count&lt;/td&gt;
&lt;td&gt;&lt;code&gt;:execrows&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;COPY FROM for bulk insert&lt;/td&gt;
&lt;td&gt;&lt;code&gt;:copyfrom&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




</description>
    </item>
    <item>
      <title>Docker Cheatsheet</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Sun, 06 Jul 2025 17:15:28 +0000</pubDate>
      <link>https://dev.to/jad_core/docker-cheatsheet-13bk</link>
      <guid>https://dev.to/jad_core/docker-cheatsheet-13bk</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Docker?&lt;/strong&gt;&lt;br&gt;
Docker is a platform for developing, shipping, and running applications inside containers. It allows you to package an application with its dependencies, ensuring consistency across different environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Concepts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Container&lt;/strong&gt; : A lightweight, standalone, executable package that includes everything needed to run an application (code, runtime, libraries, and dependencies).&lt;br&gt;
&lt;strong&gt;Image&lt;/strong&gt; : A read-only template used to create containers. Images are built from a series of layers, each representing an instruction in the image’s Dockerfile.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic Docker Commands&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Managing Containers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker ps&lt;/code&gt;: List all running containers.&lt;br&gt;
Example: docker ps&lt;br&gt;
Use: Check currently active containers.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker ps -a&lt;/code&gt;: List all containers (including stopped ones).&lt;br&gt;
Example: docker ps -a&lt;br&gt;
Use: View all containers, running or stopped.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run&lt;/code&gt;: Create and start a container from an image.&lt;br&gt;
Example: docker run -d -p 8080:80 nginx&lt;br&gt;
Options:&lt;br&gt;
&lt;code&gt;-d&lt;/code&gt;: Run container in detached mode (background).&lt;br&gt;
&lt;code&gt;-p&lt;/code&gt; &lt;strong&gt;:&lt;/strong&gt;: Map host port to container port.&lt;br&gt;
&lt;code&gt;--name &amp;lt;name&amp;gt;&lt;/code&gt;: Assign a name to the container.&lt;br&gt;
&lt;code&gt;-e &amp;lt;environemnt_variable&amp;gt;&lt;/code&gt;: set env variables like db user, pass etc&lt;/p&gt;

&lt;p&gt;Use: Start a new container from an image.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker exec:&lt;/code&gt; Run a command inside a running container.&lt;br&gt;
Example: &lt;code&gt;docker exec -it &amp;lt;container_id&amp;gt; /bin/bash&lt;/code&gt;&lt;br&gt;
Options:&lt;br&gt;
&lt;code&gt;-i:&lt;/code&gt; Interactive mode (keep STDIN open).&lt;br&gt;
&lt;code&gt;-t:&lt;/code&gt; Allocate a pseudo-TTY (terminal).&lt;/p&gt;

&lt;p&gt;Use: Access a container’s shell or run commands inside it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker stop&lt;/code&gt;: Stop a running container.&lt;br&gt;
Example: &lt;code&gt;docker stop &amp;lt;container_id&amp;gt;&lt;/code&gt;&lt;br&gt;
Use: Gracefully stop a container.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker rm&lt;/code&gt;: Remove a stopped container.&lt;br&gt;
Example: &lt;code&gt;docker rm &amp;lt;container_id&amp;gt;&lt;/code&gt;&lt;br&gt;
Use: Delete a container to free up resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Managing Images&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker images&lt;/code&gt;: List all images on the system.&lt;br&gt;
Example: &lt;code&gt;docker images&lt;/code&gt;&lt;br&gt;
Use: View available images locally.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker pull&lt;/code&gt;: Download an image from a registry (e.g., Docker Hub).&lt;br&gt;
Example: &lt;code&gt;docker pull ubuntu:latest&lt;/code&gt;&lt;br&gt;
Use: Fetch an image to use locally.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker rmi&lt;/code&gt;: Remove an image.&lt;br&gt;
Example: &lt;code&gt;docker rmi &amp;lt;image_id&amp;gt;&lt;/code&gt;&lt;br&gt;
Use: Delete an unused image to free up space.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker build&lt;/code&gt;: Build an image from a Dockerfile.&lt;br&gt;
Example: &lt;code&gt;docker build -t my-app:latest&lt;/code&gt; .&lt;br&gt;
Options: &lt;code&gt;-t &amp;lt;name:tag&amp;gt;&lt;/code&gt;: Name and tag the image.&lt;/p&gt;

&lt;p&gt;Use: Create a custom image from a Dockerfile.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Logs and Monitoring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker logs&lt;/code&gt;: View logs of a container.&lt;br&gt;
Example: &lt;code&gt;docker logs &amp;lt;container_id&amp;gt;&lt;/code&gt;&lt;br&gt;
Options: &lt;code&gt;-f&lt;/code&gt;: Follow log output (like tail -f).&lt;/p&gt;

&lt;p&gt;Use: Inspect container output for debugging or monitoring.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker inspect&lt;/code&gt; : Display detailed information about a container or image.&lt;br&gt;
Example: &lt;code&gt;docker inspect &amp;lt;container_id&amp;gt;&lt;/code&gt;&lt;br&gt;
Use: Get metadata, network settings, or configuration details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;System Management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker info&lt;/code&gt;: Display system-wide information about Docker.&lt;br&gt;
Example: docker info&lt;br&gt;
Use: Check Docker version, storage, and running containers/images.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker system prune&lt;/code&gt;: Remove all unused containers, networks, and images.&lt;br&gt;
Example: &lt;code&gt;docker system prune -a&lt;/code&gt;&lt;br&gt;
Options:&lt;br&gt;
&lt;code&gt;-a:&lt;/code&gt; &lt;em&gt;Remove all unused images, not just dangling ones.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Use: Clean up unused Docker resources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Networking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker network ls&lt;/code&gt;: List all networks.&lt;br&gt;
Example: docker network ls&lt;br&gt;
Use: View available Docker networks.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker network create&lt;/code&gt;: Create a new network.&lt;br&gt;
Example: &lt;code&gt;docker network create my-network&lt;/code&gt;&lt;br&gt;
Use: Set up a custom network for containers to communicate.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker build -t my-app .&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Build an image from Dockerfile in current directory.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker images&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;List all images on local machine.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker rmi &amp;lt;image_id&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove an image.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker pull &amp;lt;image&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Pull an image from Docker Hub.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker push &amp;lt;image&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Push your image to a registry.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker run -it --name my-container -p 3000:3000 my-app&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Run a container from an image interactively.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker ps&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;List running containers.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker ps -a&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;List all containers, running or stopped.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker stop &amp;lt;container_id&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Stop a running container.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker rm &amp;lt;container_id&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove a stopped container.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker logs &amp;lt;container_id&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;View container logs.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker exec -it &amp;lt;container_id&amp;gt; /bin/bash&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Open shell in running container.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker container inspect container_name&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Inspect Container&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker-compose up&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Start all services defined in &lt;code&gt;docker-compose.yml&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker-compose down&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Stop and remove all services.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker-compose build&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Build/rebuild images for services.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker-compose logs&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;View logs of all services.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network ls&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;List all available Docker networks.&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network inspect &amp;lt;network_name&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Show detailed info about a specific network (subnet, containers, driver, etc.).&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network create mynet&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create a new custom network (default: bridge).&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network create --driver bridge mynet&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create a network explicitly using the &lt;code&gt;bridge&lt;/code&gt; driver.&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network create --driver host hostnet&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create a network using the host driver (shares host network).&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network create --driver overlay overnet&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create an overlay network (multi-host, Docker Swarm).&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network rm &amp;lt;network_name&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Delete a specific network (only if no container is using it).&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network prune&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Remove all unused networks.&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker run -d --name app --network=mynet myimage&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Run a container and attach it to &lt;code&gt;mynet&lt;/code&gt;.&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network connect mynet mycontainer&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Connect an existing container to &lt;code&gt;mynet&lt;/code&gt;.&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;docker network disconnect mynet mycontainer&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Disconnect a container from &lt;code&gt;mynet&lt;/code&gt;.&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;`docker inspect &amp;lt;container_id&amp;gt;&lt;/td&gt;
&lt;td&gt;grep -A 5 "Networks"`&lt;/td&gt;
&lt;td&gt;Show which networks a container is connected to.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Tips&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always &lt;strong&gt;specify a tag (e.g., nginx:latest)&lt;/strong&gt; when pulling or running images to avoid unexpected updates.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;docker ps -q&lt;/code&gt; to &lt;strong&gt;get only container IDs&lt;/strong&gt; (useful for scripting).&lt;/li&gt;
&lt;li&gt;Combine &lt;code&gt;docker exec with -it&lt;/code&gt;for interactive commands like shells.&lt;/li&gt;
&lt;li&gt;Regularly &lt;code&gt;clean up unused containers and images with docker system&lt;/code&gt;prune to save disk space.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Mind Map: From Powering On Laptop to Loading an Application</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Fri, 04 Jul 2025 11:10:54 +0000</pubDate>
      <link>https://dev.to/jad_core/mind-map-from-powering-on-laptop-to-loading-an-application-1i5g</link>
      <guid>https://dev.to/jad_core/mind-map-from-powering-on-laptop-to-loading-an-application-1i5g</guid>
      <description>&lt;h4&gt;
  
  
  1. Powering On the Laptop
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HDD (Hard Disk Drive or SSD)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The HDD stores the operating system (OS), applications, and data in a non-volatile state, meaning they persist even when the laptop is powered off.&lt;/li&gt;
&lt;li&gt;When you power on the laptop, the &lt;strong&gt;BIOS/UEFI&lt;/strong&gt; (firmware on the motherboard) initializes hardware and loads the OS bootloader from the HDD into &lt;strong&gt;RAM&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;RAM (Random Access Memory)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;RAM is volatile memory, meaning it’s empty when the laptop is off. Upon boot, the bootloader and essential OS files are copied from the HDD to RAM for faster access.&lt;/li&gt;
&lt;li&gt;The CPU can only execute instructions stored in RAM, not directly from the HDD, due to RAM’s much faster access speed.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Flow&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;HDD → Bootloader code is copied to RAM → CPU begins executing the bootloader to load the OS.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Operating System Loads
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CPU (Central Processing Unit)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The CPU is the "brain" of the computer, responsible for executing instructions. It consists of several key components: &lt;strong&gt;Program Counter (PC)&lt;/strong&gt;, &lt;strong&gt;Instruction Register (IR)&lt;/strong&gt;, &lt;strong&gt;Register Set&lt;/strong&gt;, &lt;strong&gt;Control Unit (CU)&lt;/strong&gt;, and &lt;strong&gt;Arithmetic Logic Unit (ALU)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Program Counter (PC)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The PC holds the memory address of the &lt;strong&gt;next instruction&lt;/strong&gt; to be executed.&lt;/li&gt;
&lt;li&gt;At boot, the PC is set to the starting address of the bootloader code in RAM, directing the CPU to begin OS loading.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Control Unit (CU)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The CU acts as the "conductor," coordinating the fetch-decode-execute cycle.&lt;/li&gt;
&lt;li&gt;It reads the address in the PC, fetches the instruction from RAM, and directs other components to process it.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Instruction Register (IR)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The IR temporarily holds the current instruction fetched from RAM, allowing the CU to decode it.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Register Set&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Registers are small, ultra-fast storage locations inside the CPU (e.g., general-purpose registers, status registers).&lt;/li&gt;
&lt;li&gt;During OS loading, registers store temporary data like memory addresses, counters, or status flags used by the bootloader and OS initialization code.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Arithmetic Logic Unit (ALU)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The ALU performs arithmetic (e.g., addition) and logical operations (e.g., comparisons).&lt;/li&gt;
&lt;li&gt;During boot, the ALU may perform calculations for memory allocation or hardware checks as directed by the OS loading process.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;RAM&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;As the OS loads, more files (kernel, drivers, system libraries) are copied from the HDD to RAM.&lt;/li&gt;
&lt;li&gt;RAM acts as a workspace where the CPU can quickly access and manipulate data.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Flow&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;PC points to bootloader address in RAM → CU fetches instruction to IR → CU decodes instruction → ALU/registers process data as needed → OS files are loaded from HDD to RAM → OS initializes.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Launching an Application
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;User Action&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;You click on an application (e.g., a web browser or text editor).&lt;/li&gt;
&lt;li&gt;The OS receives this request and initiates a &lt;strong&gt;process&lt;/strong&gt; for the application.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Process and Thread&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;process&lt;/strong&gt; is an instance of a program in execution, including its code, data, and state (e.g., memory allocation, open files).&lt;/li&gt;
&lt;li&gt;Each process has at least one &lt;strong&gt;thread&lt;/strong&gt;, which is the smallest unit of execution. A thread contains a PC, register set, and stack, allowing it to execute instructions independently within the process.&lt;/li&gt;
&lt;li&gt;Modern applications may use multiple threads (e.g., one for the UI, another for background tasks) to improve performance.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;HDD&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The application’s executable file (e.g., &lt;code&gt;.exe&lt;/code&gt; on Windows) is stored on the HDD.&lt;/li&gt;
&lt;li&gt;When you launch the application, the OS locates the executable on the HDD.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;RAM&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The OS copies the application’s code, libraries, and initial data from the HDD to RAM.&lt;/li&gt;
&lt;li&gt;The process’s memory space (code segment, data segment, stack, heap) is allocated in RAM.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Flow&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;OS identifies application on HDD → Copies executable and dependencies to RAM → Allocates process memory in RAM → Creates initial thread for execution.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. Executing the Application
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Program Counter (PC)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The PC is set to the starting address of the application’s code in RAM (the entry point of the program).&lt;/li&gt;
&lt;li&gt;For each thread, the PC tracks the next instruction to execute.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Control Unit (CU)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The CU manages the fetch-decode-execute cycle for the application’s instructions:

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Fetch&lt;/strong&gt;: CU retrieves the instruction at the PC’s address from RAM and stores it in the IR.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decode&lt;/strong&gt;: CU interprets the instruction to determine the required actions (e.g., load data, perform a calculation).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execute&lt;/strong&gt;: CU directs the ALU, registers, or memory to carry out the instruction.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Instruction Register (IR)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The IR holds the current instruction during the fetch phase, allowing the CU to decode and execute it.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Register Set&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Registers store temporary data for the application, such as:

&lt;ul&gt;
&lt;li&gt;Variables or intermediate results (general-purpose registers).&lt;/li&gt;
&lt;li&gt;Stack pointers for function calls and thread management.&lt;/li&gt;
&lt;li&gt;Status flags (e.g., zero flag, carry flag) set by the ALU after operations.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Each thread has its own set of register values to maintain its execution state.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Arithmetic Logic Unit (ALU)&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The ALU performs calculations (e.g., adding numbers in the application) or logical operations (e.g., checking if a condition is true) as specified by the instructions.&lt;/li&gt;
&lt;li&gt;For example, if the application calculates a sum, the ALU adds the values stored in registers.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;RAM&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The application’s code and data reside in RAM during execution.&lt;/li&gt;
&lt;li&gt;The CPU accesses RAM to fetch instructions (via the PC) and read/write data as the application runs.&lt;/li&gt;
&lt;li&gt;If the application opens a file, the file’s data may be copied from the HDD to RAM for processing.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;HDD&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The HDD is accessed if the application needs additional data (e.g., loading a saved file) or if the OS swaps out memory pages to free up RAM (in case of high memory usage).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Flow&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;PC points to application code in RAM → CU fetches instruction to IR → CU decodes instruction → Registers/ALU execute operations (e.g., calculations, data movement) → RAM provides code/data → HDD accessed for additional data if needed → Thread continues executing, updating PC for next instruction.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  5. Multitasking and Thread Management
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Process and Thread Context&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;The OS manages multiple processes and threads, scheduling them on the CPU using a scheduler.&lt;/li&gt;
&lt;li&gt;Each thread’s state (PC, register values, stack) is saved when it’s paused and restored when it resumes, enabling multitasking.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;CPU Components&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PC&lt;/strong&gt;: Updated for each thread to point to its next instruction when switched.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Registers&lt;/strong&gt;: Save/restore thread-specific data during context switches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CU&lt;/strong&gt;: Coordinates context switches by saving the current thread’s state and loading the next thread’s state.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;RAM&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Stores the memory space for all running processes and threads.&lt;/li&gt;
&lt;li&gt;The OS may use virtual memory, swapping data to the HDD if RAM is full.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;HDD&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Used for virtual memory (swap space/page file) when RAM is insufficient.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Flow&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;OS schedules threads → CU saves/restores PC and registers during context switches → RAM holds process/thread data → HDD handles swap space if needed.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  6. Completing the Application’s Task
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;As the application runs, the CPU repeatedly executes the fetch-decode-execute cycle for each thread:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PC&lt;/strong&gt; increments to the next instruction or jumps to a new address (e.g., for loops or function calls).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CU&lt;/strong&gt; orchestrates the cycle, directing the ALU and registers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IR&lt;/strong&gt; holds each instruction temporarily.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ALU&lt;/strong&gt; performs computations as needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Registers&lt;/strong&gt; store intermediate results and thread state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RAM&lt;/strong&gt; provides fast access to code and data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HDD&lt;/strong&gt; is accessed for persistent storage (e.g., saving a file).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;When the application closes, the OS deallocates its RAM, terminates its process/threads, and updates any saved data on the HDD.&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Summary Mind Map (Conceptual Flow)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Power On
   └── HDD: Stores OS → Copies bootloader to RAM
2. OS Loads
   ├── RAM: Holds OS code/data
   └── CPU
       ├── PC: Points to bootloader address
       ├── CU: Fetches/decodes/executes boot instructions
       ├── IR: Holds current boot instruction
       ├── Registers: Store temporary boot data
       └── ALU: Performs boot-related calculations
3. Launch Application
   ├── HDD: Stores application executable → Copies to RAM
   ├── RAM: Allocates process memory (code, data, stack)
   └── OS: Creates process and initial thread
4. Execute Application
   ├── CPU
   │   ├── PC: Tracks next instruction address
   │   ├── CU: Manages fetch-decode-execute cycle
   │   ├── IR: Holds current instruction
   │   ├── Registers: Store thread data (variables, stack pointers)
   │   └── ALU: Performs computations
   ├── RAM: Provides code/data for execution
   └── HDD: Accessed for additional data or virtual memory
5. Multitasking
   ├── OS: Schedules threads, manages context switches
   ├── CPU: Saves/restores PC and registers
   ├── RAM: Holds all process/thread data
   └── HDD: Swap space for virtual memory
6. Application Closes
   ├── RAM: Deallocates process memory
   ├── HDD: Saves final data
   └── CPU: Terminates thread execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Key Roles Recap
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HDD&lt;/strong&gt;: Persistent storage for OS, applications, and data; slow but large capacity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RAM&lt;/strong&gt;: Fast, volatile memory for active code and data; serves as CPU’s workspace.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Program Counter (PC)&lt;/strong&gt;: Tracks the address of the next instruction, guiding the CPU’s execution flow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instruction Register (IR)&lt;/strong&gt;: Holds the current instruction for decoding and execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Register Set&lt;/strong&gt;: Ultra-fast storage for temporary data and thread state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Control Unit (CU)&lt;/strong&gt;: Orchestrates the fetch-decode-execute cycle and coordinates CPU components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ALU&lt;/strong&gt;: Performs arithmetic and logical operations for computations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Process/Thread&lt;/strong&gt;: Process is the program in execution; threads are lightweight execution units within a process, each with its own PC and registers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This flow illustrates how the components work together seamlessly, from powering on the laptop to running an application, with the CPU’s components, RAM, and HDD playing distinct but interconnected roles. If you’d like a visual diagram or further details on any part (e.g., context switching, virtual memory), let me know!&lt;/p&gt;

</description>
      <category>backend</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>Concurrency &amp; Goroutine in GO</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Wed, 18 Jun 2025 05:35:03 +0000</pubDate>
      <link>https://dev.to/jad_core/concurrency-goroutine-in-go-381f</link>
      <guid>https://dev.to/jad_core/concurrency-goroutine-in-go-381f</guid>
      <description>&lt;h2&gt;
  
  
  What Is Concurrency in Go?
&lt;/h2&gt;

&lt;p&gt;Concurrency is when multiple tasks are in progress at the same time — not necessarily running simultaneously, but progressing independently. Go makes concurrency simple and efficient with &lt;em&gt;goroutines&lt;/em&gt; and &lt;em&gt;channels&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is a Goroutine?
&lt;/h2&gt;

&lt;p&gt;A goroutine is a &lt;strong&gt;lightweight thread&lt;/strong&gt; managed by the &lt;strong&gt;Go runtime&lt;/strong&gt;. You start one with the go keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go doSomething()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Very lightweight (~2KB initial stack)&lt;/li&gt;
&lt;li&gt;Managed by Go runtime, not the OS&lt;/li&gt;
&lt;li&gt;Automatically grows/shrinks its stack&lt;/li&gt;
&lt;li&gt;Scheduled by Go's internal M:N scheduler (many goroutines on few OS threads)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Real-World Scenario: Web Server Handling Requests
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func handler(w http.ResponseWriter, r *http.Request) {
    go logRequest(r) // non-blocking logging
    data := process(r) // main request logic
    w.Write(data)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;What Happens:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;logRequest runs in a separate goroutine → doesn't block response&lt;/li&gt;
&lt;li&gt;process(r) runs on main goroutine handling the request&lt;/li&gt;
&lt;li&gt;Go's scheduler assigns both to available threads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Under the Hood: What Happens When a Goroutine Runs?&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Go's Scheduler (G-M-P Model):
&lt;/h4&gt;

&lt;p&gt;Go uses an internal M:N scheduler with three components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;G: Goroutine (your code)&lt;/li&gt;
&lt;li&gt;M: Machine (OS thread)&lt;/li&gt;
&lt;li&gt;P: Processor (context for scheduling goroutines)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Lifecycle:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;You call &lt;code&gt;go doSomething()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Go runtime:

&lt;ul&gt;
&lt;li&gt;Creates a new G (goroutine struct)&lt;/li&gt;
&lt;li&gt;Adds it to a local run queue of a P&lt;/li&gt;
&lt;li&gt;A running M picks it up from P and runs it&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;If doSomething() blocks (e.g., on IO or time.Sleep), Go parks the G, and the M picks another G&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  How Goroutines Differ from Threads
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Goroutine&lt;/th&gt;
&lt;th&gt;OS Thread&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Memory&lt;/td&gt;
&lt;td&gt;~2KB stack&lt;/td&gt;
&lt;td&gt;~1MB stack&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Managed by&lt;/td&gt;
&lt;td&gt;Go runtime&lt;/td&gt;
&lt;td&gt;OS Kernel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scheduling&lt;/td&gt;
&lt;td&gt;M:N, cooperative&lt;/td&gt;
&lt;td&gt;1:1, preemptive&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Startup cost&lt;/td&gt;
&lt;td&gt;Tiny&lt;/td&gt;
&lt;td&gt;Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Context switch&lt;/td&gt;
&lt;td&gt;Fast (no syscall)&lt;/td&gt;
&lt;td&gt;Slower (involves kernel)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What Makes Go Concurrency Powerful?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Built-in scheduler ensures fairness and efficiency&lt;/li&gt;
&lt;li&gt;Goroutines auto-grow/shrink stack&lt;/li&gt;
&lt;li&gt;Go has network poller (epoll/kqueue) to handle blocking IO without blocking the thread&lt;/li&gt;
&lt;li&gt;Garbage collector knows about goroutines and stack frames&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important Notes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blocking a goroutine ≠ blocking a thread: e.g., a goroutine waiting on a channel doesn't block an OS thread&lt;/li&gt;
&lt;li&gt;Too many goroutines? Still okay — Go can handle millions if you avoid leaks and deep blocking&lt;/li&gt;
&lt;li&gt;Use runtime.NumGoroutine() to inspect count&lt;/li&gt;
&lt;li&gt;Use pprof or go tool trace to profile concurrent programs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion &amp;amp; Best Practices&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use goroutines for anything parallel (IO, background jobs, async tasks)&lt;/li&gt;
&lt;li&gt;Don’t forget to sync: use channels, WaitGroups, or mutexes&lt;/li&gt;
&lt;li&gt;Avoid goroutine leaks (e.g., waiting forever on a channel)&lt;/li&gt;
&lt;li&gt;Monitor memory and goroutine counts in production&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Git Commit Convention</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Mon, 16 Jun 2025 04:53:53 +0000</pubDate>
      <link>https://dev.to/jad_core/git-commit-convention-4mdd</link>
      <guid>https://dev.to/jad_core/git-commit-convention-4mdd</guid>
      <description>&lt;h3&gt;
  
  
  Git Commit Convention Rules Every Developer Should Know
&lt;/h3&gt;

&lt;p&gt;Many developers write commit messages like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"fixed bug"&lt;/li&gt;
&lt;li&gt;"updated code"&lt;/li&gt;
&lt;li&gt;"changes made"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, this kind of approach doesn’t work well in professional software development. Instead, you should follow a standardized format for writing meaningful and consistent commit messages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Commit Message Format:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;type&amp;gt;(&amp;lt;scope&amp;gt;): &amp;lt;subject&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common Commit Types:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;feat&lt;/code&gt;: When you add a new feature&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fix&lt;/code&gt;: When you fix a bug&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docs&lt;/code&gt;: For documentation changes or updates&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;style&lt;/code&gt;: For formatting, white-space, missing semicolons, etc. (no code logic changes)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;refactor&lt;/code&gt;: Code refactoring (no new features or bug fixes)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;test&lt;/code&gt;: Adding or updating tests&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;chore&lt;/code&gt;: Other technical tasks (e.g., dependency updates, build scripts, configs)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;perf&lt;/code&gt;: Performance improvements&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ci&lt;/code&gt;: Changes to CI/CD configuration&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;revert&lt;/code&gt;: Reverting a previous commit&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;update&lt;/code&gt;: Updating packages or dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Following this convention makes your commit history clean, understandable, and professional.&lt;/p&gt;

</description>
      <category>git</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Make() in GO</title>
      <dc:creator>Huseyn</dc:creator>
      <pubDate>Mon, 16 Jun 2025 04:26:37 +0000</pubDate>
      <link>https://dev.to/jad_core/make-in-go-2d07</link>
      <guid>https://dev.to/jad_core/make-in-go-2d07</guid>
      <description>&lt;p&gt;&lt;strong&gt;What Does make Do in Go?&lt;/strong&gt;&lt;br&gt;
The make function is used to &lt;strong&gt;create and initialize only three types&lt;/strong&gt; of built-in data structures in Go:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Slices&lt;/li&gt;
&lt;li&gt;✅ Maps&lt;/li&gt;
&lt;li&gt;✅ Channels&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are reference types, and unlike arrays or structs, they need some internal setup to work properly. The make function does exactly that — it sets them up so you can use them safely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Not Just Declare Normally?&lt;/strong&gt;&lt;br&gt;
Because if you just declare a slice/map/channel without using make, it may be nil — and using a nil map or channel will cause a runtime error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&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;make(type, length[, capacity])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;For slices, you can give both length and capacity.&lt;/li&gt;
&lt;li&gt;For maps, you can give an optional initial capacity.&lt;/li&gt;
&lt;li&gt;For channels, you specify the buffer size (for buffered channels).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;🔍 Examples&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;🧺 Slice Example
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s := make([]int, 3, 5)
fmt.Println(s)        // [0 0 0]
fmt.Println(len(s))   // 3
fmt.Println(cap(s))   // 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;len = how many items it holds now&lt;/li&gt;
&lt;li&gt;cap = how many items it can hold before resizing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can now safely use s and append more items.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;🗺️ Map Example
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;m := make(map[string]int)
m["apple"] = 10
fmt.Println(m) // map[apple:10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You must use make for maps before writing to them, or you’ll get a panic.&lt;/p&gt;

&lt;p&gt;You can give an initial capacity: make(map[string]int, 100) — this allocates space for ~100 items.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;📬 Channel Example
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ch := make(chan int, 2)
ch &amp;lt;- 1
ch &amp;lt;- 2
fmt.Println(&amp;lt;-ch) // 1
fmt.Println(&amp;lt;-ch) // 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a buffered channel of size 2.&lt;br&gt;
make is always used to create channels.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;❓ What's the Difference Between make and new?&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;&lt;code&gt;make&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;new&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Works with&lt;/td&gt;
&lt;td&gt;Only slices, maps, channels&lt;/td&gt;
&lt;td&gt;All types&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Returns&lt;/td&gt;
&lt;td&gt;Initialized &lt;strong&gt;value&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;Pointer to &lt;strong&gt;zero value&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ready to use?&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ No — you often must set it up manually&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;🧑‍🍳 Real-World Analogy&lt;/strong&gt;&lt;br&gt;
Imagine you're setting a table:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;new&lt;/code&gt; just gives you an empty plate, but you still need to put food on it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;make&lt;/code&gt; gives you a fully served plate, ready to eat from.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;🧪 In Practice: Shopping Cart&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;func main() {
    cart := make([]string, 0) // slice
    prices := make(map[string]float64) // map
    orderQueue := make(chan string, 2) // channel

    cart = append(cart, "Book", "Pen")
    prices["Book"] = 9.99

    orderQueue &amp;lt;- "Order #1"
    orderQueue &amp;lt;- "Order #2"

    fmt.Println("Cart:", cart)
    fmt.Println("Price of Book:", prices["Book"])
    fmt.Println("Processing:", &amp;lt;-orderQueue)
}

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

&lt;/div&gt;



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