<?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: Shrouk Abozeid</title>
    <description>The latest articles on DEV Community by Shrouk Abozeid (@shroukabozeid).</description>
    <link>https://dev.to/shroukabozeid</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4008684%2Fddf07757-4921-49f6-8aa8-bd77aeda61b0.jpg</url>
      <title>DEV Community: Shrouk Abozeid</title>
      <link>https://dev.to/shroukabozeid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shroukabozeid"/>
    <language>en</language>
    <item>
      <title>Threading and Code Execution in Rails: Executor, Reloader, and Load Interlock</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Mon, 26 Oct 2026 15:42:14 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/threading-and-code-execution-in-rails-executor-reloader-and-load-interlock-igh</link>
      <guid>https://dev.to/shroukabozeid/threading-and-code-execution-in-rails-executor-reloader-and-load-interlock-igh</guid>
      <description>&lt;p&gt;Rails handles concurrency for us in many situations. But what happens when we create our own threads? How does Rails safely reload application code while other threads are running? And what does the Executor have to do with database connections and autoloading?&lt;/p&gt;

&lt;p&gt;I recently explored these questions while reading the Rails 8 guides. Here are my notes, with examples to make the concepts easier to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. How Rails Handles Concurrency
&lt;/h2&gt;

&lt;p&gt;Rails allows different operations to run concurrently.&lt;/p&gt;

&lt;p&gt;For example, when using a threaded web server such as Puma, multiple HTTP requests can be processed simultaneously. Active Job can also execute multiple jobs concurrently, depending on the queue adapter and its configuration. Action Cable handles messages using threads as well.&lt;/p&gt;

&lt;p&gt;Each thread manages its own unit of work, such as a controller instance or a job instance, but threads share the same process space.&lt;/p&gt;

&lt;p&gt;This means they can access shared objects, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby classes and modules.&lt;/li&gt;
&lt;li&gt;Application configuration.&lt;/li&gt;
&lt;li&gt;Global variables.&lt;/li&gt;
&lt;li&gt;Other shared objects in memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider two requests arriving at the same time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UsersController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;
    &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each request gets its own controller instance, so the instance variable &lt;code&gt;@user&lt;/code&gt; belongs to that particular instance.&lt;/p&gt;

&lt;p&gt;However, both requests use the same &lt;code&gt;User&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;This is usually fine, provided your application doesn't introduce unsafe shared state or modify shared objects in ways that cause race conditions.&lt;/p&gt;

&lt;p&gt;Rails provides mechanisms to coordinate application execution with its internal systems. Two important ones are the &lt;strong&gt;Executor&lt;/strong&gt; and the &lt;strong&gt;Reloader&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's understand why we need them.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The Rails Executor
&lt;/h2&gt;

&lt;p&gt;The Executor separates application code from framework code.&lt;/p&gt;

&lt;p&gt;Whenever Rails invokes application code through its framework entry points, it wraps that execution in the Executor.&lt;/p&gt;

&lt;p&gt;The Executor has two callbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;to_run&lt;/code&gt;: Called before application code executes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;to_complete&lt;/code&gt;: Called after application code finishes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of the Executor as a boundary around a unit of application work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Framework invokes application code
               |
               v
        Executor starts
               |
               v
        to_run callbacks
               |
               v
       Application code
               |
               v
       to_complete callbacks
               |
               v
        Execution ends
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What does the Executor actually do?
&lt;/h3&gt;

&lt;p&gt;In a default Rails application, its callbacks help Rails:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Track which threads are in safe positions for autoloading and reloading.&lt;/li&gt;
&lt;li&gt;Enable and disable the Active Record query cache.&lt;/li&gt;
&lt;li&gt;Return acquired Active Record connections to the connection pool.&lt;/li&gt;
&lt;li&gt;Constrain the lifetimes of internal caches.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of making every component manage these responsibilities independently, Rails provides a common execution boundary.&lt;/p&gt;

&lt;p&gt;This is particularly important when application code runs concurrently.&lt;/p&gt;

&lt;h3&gt;
  
  
  What if we create our own thread?
&lt;/h3&gt;

&lt;p&gt;Rails knows how to wrap application code at its own entry points. But Ruby also lets us create threads directly.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're executing Rails application code inside a manually created Ruby thread.&lt;/p&gt;

&lt;p&gt;That thread hasn't automatically been wrapped in the Rails Executor just because it was created inside a Rails application.&lt;/p&gt;

&lt;p&gt;Why does this matter?&lt;/p&gt;

&lt;p&gt;The code might access Active Record, reference autoloaded constants, or interact with other Rails-managed resources. Rails needs to coordinate those operations with its execution lifecycle.&lt;/p&gt;

&lt;p&gt;The solution is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the application code runs inside the Executor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The important distinction is that the Executor doesn't create the thread. It manages the Rails execution lifecycle within the thread.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This also applies when using thread pools or concurrency libraries that execute application code on threads you manage.&lt;/p&gt;

&lt;h3&gt;
  
  
  What if we can't use &lt;code&gt;wrap&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;Rails also provides &lt;code&gt;run!&lt;/code&gt; and &lt;code&gt;complete!&lt;/code&gt; for situations where wrapping a block isn't practical.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;execution_context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run!&lt;/span&gt;

  &lt;span class="k"&gt;begin&lt;/span&gt;
    &lt;span class="c1"&gt;# Execute application code here.&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;
  &lt;span class="k"&gt;ensure&lt;/span&gt;
    &lt;span class="n"&gt;execution_context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete!&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;execution_context&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;run!&lt;/code&gt; starts the Executor's execution context, while &lt;code&gt;complete!&lt;/code&gt; finishes it.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ensure&lt;/code&gt; clause ensures that completion is attempted even if the application code raises an exception.&lt;/p&gt;

&lt;p&gt;For ordinary application code, prefer &lt;code&gt;wrap&lt;/code&gt; because it handles the lifecycle for you.&lt;/p&gt;

&lt;p&gt;One more detail: the Executor is re-entrant. If it's already active on the current thread, calling &lt;code&gt;wrap&lt;/code&gt; again doesn't establish a second independent execution context.&lt;/p&gt;

&lt;p&gt;However, each new thread needs its own Executor context when it executes Rails application code.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The Rails Reloader
&lt;/h2&gt;

&lt;p&gt;The Executor manages the execution lifecycle, but it doesn't decide whether the application needs to reload its code.&lt;/p&gt;

&lt;p&gt;That's where the Reloader comes in.&lt;/p&gt;

&lt;p&gt;During development, Rails can reload application classes when their source files change. This lets us modify a model or controller without restarting the server.&lt;/p&gt;

&lt;p&gt;For example, suppose we have this model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greeting&lt;/span&gt;
    &lt;span class="s2"&gt;"Hello!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We change the method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greeting&lt;/span&gt;
    &lt;span class="s2"&gt;"Hello from the updated code!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rails needs to unload the old class and load the updated version.&lt;/p&gt;

&lt;p&gt;But what happens if other threads are executing application code while the reload is happening?&lt;/p&gt;

&lt;p&gt;Rails needs to coordinate the reload so that other threads don't continue executing against classes that have been unloaded.&lt;/p&gt;

&lt;p&gt;The Reloader helps manage this process.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do we use the Reloader?
&lt;/h3&gt;

&lt;p&gt;It provides a similar block-based interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reloader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# Execute application code here.&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before entering the block, the Reloader checks whether the application needs to be reloaded.&lt;/p&gt;

&lt;p&gt;If a reload is required, it waits until the reload can safely occur, performs it, and then continues with the application code.&lt;/p&gt;

&lt;p&gt;The Reloader also invokes the Executor when one isn't already active on the current thread.&lt;/p&gt;

&lt;p&gt;Therefore, you don't need to wrap the same code separately in both the Reloader and the Executor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Executor vs. Reloader
&lt;/h3&gt;

&lt;p&gt;Although they have similar interfaces, they serve different purposes.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Executor&lt;/th&gt;
&lt;th&gt;Reloader&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Manages the execution lifecycle.&lt;/td&gt;
&lt;td&gt;Coordinates application code reloading.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runs its execution callbacks.&lt;/td&gt;
&lt;td&gt;Uses the Executor and coordinates reloading when necessary.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Suitable for application code executed in manually created threads.&lt;/td&gt;
&lt;td&gt;Suitable for long-running, top-level processes that repeatedly invoke application code and need reload-aware execution.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For example, a long-running process might repeatedly invoke application code while running in development.&lt;/p&gt;

&lt;p&gt;In that case, the Reloader can check for code changes before each unit of work.&lt;/p&gt;

&lt;p&gt;However, &lt;strong&gt;only long-running, top-level processes should invoke the Reloader directly&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;When a reload is required, the Reloader waits until other threads have completed their active Executor executions. Calling it from a child thread while its parent is waiting inside an Executor block can cause a deadlock.&lt;/p&gt;

&lt;p&gt;For manually created child threads, use the Executor instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. What Happens During a Reload?
&lt;/h2&gt;

&lt;p&gt;To understand the Reloader more deeply, we need to look at class unloading.&lt;/p&gt;

&lt;p&gt;When Rails reloads application code, it unloads autoloaded classes and modules so they can be loaded again.&lt;/p&gt;

&lt;p&gt;The Reloader provides two additional callbacks for coordinating operations around this process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;before_class_unload&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;after_class_unload&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These callbacks are useful when other components need to perform cleanup before classes are unloaded or take action afterward.&lt;/p&gt;

&lt;p&gt;For example, Action Cable uses &lt;code&gt;before_class_unload&lt;/code&gt; to disconnect existing WebSocket connections before application classes are unloaded.&lt;/p&gt;

&lt;p&gt;The exact point at which class unloading happens relative to the Reloader's &lt;code&gt;to_run&lt;/code&gt; and &lt;code&gt;to_complete&lt;/code&gt; callbacks depends on the &lt;code&gt;config.reload_classes_only_on_change&lt;/code&gt; setting.&lt;/p&gt;

&lt;p&gt;The important point is that Rails coordinates unloading with application execution rather than allowing classes to be unloaded arbitrarily while other threads are using them.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. How Rails Uses These Mechanisms
&lt;/h2&gt;

&lt;p&gt;We don't usually need to wrap every controller action or background job ourselves.&lt;/p&gt;

&lt;p&gt;Rails already integrates the Executor and Reloader into its framework entry points.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP requests
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ActionDispatch::Executor&lt;/code&gt; and &lt;code&gt;ActionDispatch::Reloader&lt;/code&gt; are Rack middlewares responsible for wrapping requests in the appropriate execution lifecycle.&lt;/p&gt;

&lt;p&gt;In a typical development configuration, the Reloader ensures that incoming requests use the latest application code when a reload is needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Active Job
&lt;/h3&gt;

&lt;p&gt;Active Job wraps job executions with the Reloader.&lt;/p&gt;

&lt;p&gt;This means that when a job is picked up from the queue, Rails can ensure it runs against the appropriate version of the application code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Action Cable
&lt;/h3&gt;

&lt;p&gt;Action Cable works a little differently because WebSocket connections are long-lived.&lt;/p&gt;

&lt;p&gt;A connection is associated with an instance of a connection class. We can't simply reload the application independently for every incoming WebSocket message while keeping the same connection instance.&lt;/p&gt;

&lt;p&gt;Instead, Action Cable wraps individual message handlers with the Executor.&lt;/p&gt;

&lt;p&gt;When Rails needs to reload application code, Action Cable uses the Reloader's &lt;code&gt;before_class_unload&lt;/code&gt; callback to disconnect its existing connections.&lt;/p&gt;

&lt;p&gt;The clients can then reconnect automatically and communicate with the updated application code.&lt;/p&gt;

&lt;p&gt;This approach allows Rails to reload application classes without requiring every WebSocket connection to close naturally first.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about production?
&lt;/h3&gt;

&lt;p&gt;The Reloader checks for file changes only when both &lt;code&gt;config.enable_reloading&lt;/code&gt; and &lt;code&gt;config.reload_classes_only_on_change&lt;/code&gt; are enabled.&lt;/p&gt;

&lt;p&gt;These are the defaults in the development environment.&lt;/p&gt;

&lt;p&gt;In production, reloading is disabled by default. When &lt;code&gt;config.enable_reloading&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;, the Reloader acts as a pass-through to the Executor.&lt;/p&gt;

&lt;p&gt;The Executor still has important work to do, including coordinating Active Record connection management.&lt;/p&gt;

&lt;p&gt;This is why the Executor remains relevant even when application code reloading is disabled.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. The Load Interlock
&lt;/h2&gt;

&lt;p&gt;So far, we've seen that Rails needs to coordinate application execution with class loading and unloading.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Load Interlock&lt;/strong&gt; is the mechanism that helps make autoloading and reloading safe in a multithreaded environment.&lt;/p&gt;

&lt;p&gt;Let's look at the problem it solves.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why can't two threads load the same class independently?
&lt;/h3&gt;

&lt;p&gt;Suppose two threads encounter a constant that hasn't been loaded yet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;User&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rails needs to load the class definition from the appropriate file.&lt;/p&gt;

&lt;p&gt;Imagine another thread tries to access &lt;code&gt;User&lt;/code&gt; while that class definition is only partially evaluated.&lt;/p&gt;

&lt;p&gt;That thread could observe an inconsistent state.&lt;/p&gt;

&lt;p&gt;Similarly, unloading a class while another thread is executing code that uses it can cause problems.&lt;/p&gt;

&lt;p&gt;The Load Interlock coordinates these operations.&lt;/p&gt;

&lt;p&gt;It tracks threads that are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Running application code.&lt;/li&gt;
&lt;li&gt;Loading a class.&lt;/li&gt;
&lt;li&gt;Unloading autoloaded constants.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only one thread can perform a load or unload at a time, and these operations must wait until other threads have finished running application code.&lt;/p&gt;

&lt;p&gt;This ensures that class loading and unloading happen at safe points.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does the Executor have to do with it?
&lt;/h3&gt;

&lt;p&gt;The Executor automatically acquires a &lt;code&gt;running&lt;/code&gt; lock for the duration of its block.&lt;/p&gt;

&lt;p&gt;When application code references a constant that needs to be autoloaded, Rails can transition to the appropriate loading state and then return to the running state.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Executor starts
      |
      v
Acquire running lock
      |
      v
Execute application code
      |
      v
Constant needs autoloading?
      |
      v
Load the class safely
      |
      v
Return to running state
      |
      v
Executor completes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This coordination is particularly important in development, where application classes can be reloaded.&lt;/p&gt;

&lt;p&gt;With the default production configuration, reloading is disabled and eager loading is enabled, so Rails doesn't need the Load Interlock for reloading. The Executor still performs its other responsibilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. How &lt;code&gt;permit_concurrent_loads&lt;/code&gt; Prevents Deadlocks
&lt;/h2&gt;

&lt;p&gt;This is one of the more subtle parts of the guide.&lt;/p&gt;

&lt;p&gt;The Executor holds the &lt;code&gt;running&lt;/code&gt; lock while application code executes.&lt;/p&gt;

&lt;p&gt;Now imagine one thread starts another thread and waits for it to finish.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="no"&gt;User&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assume &lt;code&gt;User&lt;/code&gt; hasn't been loaded yet.&lt;/p&gt;

&lt;p&gt;Let's follow what happens.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The outer thread enters the Executor and acquires the &lt;code&gt;running&lt;/code&gt; lock.&lt;/li&gt;
&lt;li&gt;It creates a child thread.&lt;/li&gt;
&lt;li&gt;The child thread enters its own Executor block.&lt;/li&gt;
&lt;li&gt;The child tries to reference &lt;code&gt;User&lt;/code&gt;, which needs to be autoloaded.&lt;/li&gt;
&lt;li&gt;Loading &lt;code&gt;User&lt;/code&gt; requires the other thread's application execution to finish.&lt;/li&gt;
&lt;li&gt;But the outer thread is waiting for the child to finish using &lt;code&gt;thread.join&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The outer thread is still inside its Executor block, holding the &lt;code&gt;running&lt;/code&gt; lock.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Neither thread can make progress.&lt;/p&gt;

&lt;p&gt;The outer thread is waiting for the child, while the child is waiting for the outer thread to stop running application code.&lt;/p&gt;

&lt;p&gt;That's a deadlock.&lt;/p&gt;

&lt;h3&gt;
  
  
  The solution
&lt;/h3&gt;

&lt;p&gt;Rails provides &lt;code&gt;permit_concurrent_loads&lt;/code&gt; through the Load Interlock.&lt;/p&gt;

&lt;p&gt;It allows a thread to release its &lt;code&gt;running&lt;/code&gt; lock while waiting for another operation to complete, provided it won't reference any potentially autoloaded constants inside that block.&lt;/p&gt;

&lt;p&gt;Here's the revised example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;thread&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="no"&gt;User&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Dependencies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;interlock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permit_concurrent_loads&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the outer thread releases its &lt;code&gt;running&lt;/code&gt; lock while waiting for the child thread.&lt;/p&gt;

&lt;p&gt;The child can acquire the loading lock, load &lt;code&gt;User&lt;/code&gt;, and finish execution.&lt;/p&gt;

&lt;p&gt;The outer thread then resumes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why must we be careful?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;permit_concurrent_loads&lt;/code&gt; makes a specific promise: the calling thread won't dereference any potentially autoloaded constant inside the supplied block.&lt;/p&gt;

&lt;p&gt;The safest approach is to keep the block as small as possible and place it directly around the blocking operation.&lt;/p&gt;

&lt;p&gt;For example, wrapping &lt;code&gt;thread.join&lt;/code&gt; is preferable to wrapping a large section of application logic.&lt;/p&gt;

&lt;p&gt;Don't use this method as a general-purpose way to disable thread safety. It's intended for situations where a thread must wait for another operation that may need to autoload application code.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Practical Takeaways
&lt;/h2&gt;

&lt;p&gt;Here are the rules I took away from reading the Rails guide.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use the Executor when you create your own threads.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="c1"&gt;# Rails application code&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that your code participates in Rails' execution lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use the Reloader only when you need reload-aware execution in a long-running, top-level process.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reloader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wrap&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# Application code that should run with the latest code&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't call it indiscriminately from child threads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understand that Rails already handles its own entry points.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Controller actions, Active Job executions, and Action Cable message handlers are integrated with these mechanisms. You generally don't need to wrap them manually.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember that disabling reloading doesn't make the Executor unnecessary.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Executor still coordinates responsibilities such as Active Record connection management.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Be careful when combining threads, blocking operations, and autoloading.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A call to &lt;code&gt;join&lt;/code&gt;, or another blocking operation inside an Executor block, can retain the &lt;code&gt;running&lt;/code&gt; lock and cause a deadlock if another thread needs to autoload a constant.&lt;/p&gt;

&lt;p&gt;In such cases, &lt;code&gt;permit_concurrent_loads&lt;/code&gt; may help, provided you follow its requirements.&lt;/p&gt;

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

&lt;p&gt;Rails makes concurrent application execution relatively straightforward, but it still needs to coordinate shared resources and application code loading.&lt;/p&gt;

&lt;p&gt;The Executor provides a common boundary for running application code. The Reloader builds on that boundary to coordinate code reloading, while the Load Interlock helps ensure that classes are loaded and unloaded safely across threads.&lt;/p&gt;

&lt;p&gt;Understanding these mechanisms helps explain why Rails automatically wraps its own entry points, why manually created threads need special attention, and why certain combinations of blocking operations and autoloading can cause deadlocks.&lt;/p&gt;

&lt;p&gt;These are internal mechanisms that we don't usually need to think about during everyday Rails development. But understanding why they exist makes it much easier to write safe concurrent code when we need to go beyond the framework's default behavior.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;These notes are based on the official Rails 8.0 guide, “Threading and Code Execution in Rails.”&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>The Rails Initialization Process: From bin/rails to a Running Server</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Tue, 20 Oct 2026 15:28:29 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/the-rails-initialization-process-from-binrails-to-a-running-server-45mc</link>
      <guid>https://dev.to/shroukabozeid/the-rails-initialization-process-from-binrails-to-a-running-server-45mc</guid>
      <description>&lt;p&gt;When you run &lt;code&gt;bin/rails server&lt;/code&gt;, Rails doesn't immediately start accepting HTTP requests.&lt;/p&gt;

&lt;p&gt;It first loads dependencies, resolves the command, prepares the server, loads your application configuration, runs initializers, and builds the Rack application.&lt;/p&gt;

&lt;p&gt;Let's walk through it.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Launching Rails
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;bin/rails&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The journey starts with the &lt;code&gt;bin/rails&lt;/code&gt; executable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env ruby&lt;/span&gt;

&lt;span class="no"&gt;APP_PATH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;expand_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"../config/application"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__dir__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;require_relative&lt;/span&gt; &lt;span class="s2"&gt;"../config/boot"&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"rails/commands"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line tells the operating system to execute the script using Ruby.&lt;/p&gt;

&lt;p&gt;Next, Rails defines &lt;code&gt;APP_PATH&lt;/code&gt;, which points to &lt;code&gt;config/application.rb&lt;/code&gt;. This constant will be used later when Rails loads the application.&lt;/p&gt;

&lt;p&gt;Then it requires two files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;config/boot.rb&lt;/code&gt;, to prepare the Ruby environment and dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rails/commands&lt;/code&gt;, to resolve and execute the requested Rails command.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at them one by one.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;config/boot.rb&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"BUNDLE_GEMFILE"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;expand_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"../Gemfile"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__dir__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"bundler/setup"&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"bootsnap/setup"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file prepares the application's dependency environment.&lt;/p&gt;

&lt;p&gt;First, &lt;code&gt;BUNDLE_GEMFILE&lt;/code&gt; tells Bundler which Gemfile to use, unless it has already been configured.&lt;/p&gt;

&lt;p&gt;Next, &lt;code&gt;bundler/setup&lt;/code&gt; configures Ruby's load path so the application can load the gems declared in the Gemfile.&lt;/p&gt;

&lt;p&gt;Finally, &lt;code&gt;bootsnap/setup&lt;/code&gt; enables Bootsnap's caching mechanisms to help reduce startup time.&lt;/p&gt;

&lt;p&gt;At this point, the dependencies are ready to be loaded.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Resolving the Rails command
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;rails/commands.rb&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;After booting the dependency environment, Rails loads &lt;code&gt;rails/commands.rb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A simplified version looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"rails/command"&lt;/span&gt;

&lt;span class="n"&gt;aliases&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"g"&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"generate"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"d"&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"destroy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"c"&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"console"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"s"&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"server"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"db"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"dbconsole"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"r"&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"runner"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"t"&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"test"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ARGV&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shift&lt;/span&gt;
&lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;aliases&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt;

&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Command&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ARGV&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ARGV&lt;/code&gt; contains the command-line arguments passed to the script.&lt;/p&gt;

&lt;p&gt;For example, when you execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/rails server &lt;span class="nt"&gt;-p&lt;/span&gt; 3001
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rails extracts &lt;code&gt;server&lt;/code&gt; as the command and leaves &lt;code&gt;-p 3001&lt;/code&gt; as the remaining arguments.&lt;/p&gt;

&lt;p&gt;If you use the shorthand &lt;code&gt;s&lt;/code&gt;, Rails expands it to &lt;code&gt;server&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It then calls &lt;code&gt;Rails::Command.invoke&lt;/code&gt; to find and execute the appropriate command.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;rails/command.rb&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Rails::Command.invoke&lt;/code&gt; method looks up the requested command and executes it when a matching command is found.&lt;/p&gt;

&lt;p&gt;If Rails cannot resolve the command, it can fall back to invoking a Rake task with the corresponding name.&lt;/p&gt;

&lt;p&gt;For our example, Rails resolves &lt;code&gt;server&lt;/code&gt; to &lt;code&gt;Rails::Command::ServerCommand&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can explore the implementation in the &lt;a href="https://github.com/rails/rails/blob/main/railties/lib/rails/command.rb" rel="noopener noreferrer"&gt;Rails command source code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Preparing the server
&lt;/h2&gt;

&lt;p&gt;The server command is responsible for preparing the environment and starting the server.&lt;/p&gt;

&lt;p&gt;A simplified version of its &lt;code&gt;perform&lt;/code&gt; method looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;perform&lt;/span&gt;
  &lt;span class="n"&gt;extract_environment_option_from_argument&lt;/span&gt;
  &lt;span class="n"&gt;set_application_directory!&lt;/span&gt;
  &lt;span class="n"&gt;prepare_restart&lt;/span&gt;

  &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_options&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;tap&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="no"&gt;APP_PATH&lt;/span&gt;
    &lt;span class="no"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;root&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;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serveable?&lt;/span&gt;
      &lt;span class="n"&gt;print_boot_information&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;served_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact implementation can vary between Rails versions, but the important steps are the same.&lt;/p&gt;

&lt;p&gt;First, Rails processes the environment option and prepares the application directory and restart behavior.&lt;/p&gt;

&lt;p&gt;Then it creates a &lt;code&gt;Rails::Server&lt;/code&gt; instance using the configured server options. These can include the host, port, environment, PID file, and server adapter.&lt;/p&gt;

&lt;p&gt;Notice the order: the server object is created before &lt;code&gt;APP_PATH&lt;/code&gt; is required.&lt;/p&gt;

&lt;p&gt;This ordering matters because the server setup needs to establish the selected environment before the application is loaded.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is &lt;code&gt;Rails::Server&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;In modern Rails, &lt;code&gt;Rails::Server&lt;/code&gt; inherits from &lt;code&gt;Rackup::Server&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Rackup provides the interface for running Rack applications, while Rails adds application-specific behavior around it.&lt;/p&gt;

&lt;p&gt;The server object prepares the server configuration, but this doesn't mean that the Rails application has already completed initialization.&lt;/p&gt;

&lt;p&gt;That happens in the next part of the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Loading the application configuration
&lt;/h2&gt;

&lt;p&gt;When Rails executes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="no"&gt;APP_PATH&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it loads your application's &lt;code&gt;config/application.rb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A typical Rails application contains something similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require_relative&lt;/span&gt; &lt;span class="s2"&gt;"boot"&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"rails/all"&lt;/span&gt;

&lt;span class="no"&gt;Bundler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;groups&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;MyApp&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Application&lt;/span&gt;
    &lt;span class="c1"&gt;# Application configuration&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual file depends on your application and Rails version.&lt;/p&gt;

&lt;p&gt;Let's break down the important parts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loading &lt;code&gt;rails/all&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;"rails/all"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This loads the standard Rails frameworks, including Active Record, Action Controller, Action View, and other framework components.&lt;/p&gt;

&lt;p&gt;Applications can choose a more selective set of frameworks, so the exact set depends on how the application was created and configured.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining &lt;code&gt;Rails::Application&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;MyApp&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Application&lt;/span&gt;
    &lt;span class="c1"&gt;# Application configuration&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This defines your application class and its configuration.&lt;/p&gt;

&lt;p&gt;For example, this is where you might configure time zones, autoload paths, middleware, generators, or other application settings.&lt;/p&gt;

&lt;p&gt;However, defining the application class is not the same as fully initializing the application.&lt;/p&gt;

&lt;p&gt;Rails still needs to execute the initialization process.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Building the Rack application
&lt;/h2&gt;

&lt;p&gt;Now we reach &lt;code&gt;Rails::Server#start&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The server startup process prepares runtime resources, handles development caching where applicable, configures logging, and delegates to Rackup to load and run the application.&lt;/p&gt;

&lt;p&gt;For a standard Rails application, the Rack configuration file is &lt;code&gt;config.ru&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require_relative&lt;/span&gt; &lt;span class="s2"&gt;"config/environment"&lt;/span&gt;

&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file is the bridge between Rack and Rails.&lt;/p&gt;

&lt;p&gt;Rackup uses &lt;code&gt;Rack::Builder&lt;/code&gt; to evaluate the configuration file and build the Rack application.&lt;/p&gt;

&lt;p&gt;The first line loads &lt;code&gt;config/environment.rb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's follow that call.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Initializing Rails
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;config/environment.rb&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;A standard Rails environment file looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require_relative&lt;/span&gt; &lt;span class="s2"&gt;"application"&lt;/span&gt;

&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initialize!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line ensures the application configuration has been loaded.&lt;/p&gt;

&lt;p&gt;The second line triggers the actual initialization process.&lt;/p&gt;

&lt;p&gt;This is an important distinction:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;config/application.rb&lt;/code&gt; defines the application; &lt;code&gt;Rails.application.initialize!&lt;/code&gt; initializes it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The initialization process is implemented through Rails' initializer infrastructure.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;rails/all.rb&lt;/code&gt; and the Railties
&lt;/h3&gt;

&lt;p&gt;When &lt;code&gt;config/application.rb&lt;/code&gt; requires &lt;code&gt;rails/all&lt;/code&gt;, Rails loads the framework components and their Railties.&lt;/p&gt;

&lt;p&gt;A Railtie is an integration point that allows a framework or library to participate in Rails initialization and configuration.&lt;/p&gt;

&lt;p&gt;Rails uses these integrations to register initializers, configure middleware, prepare framework components, and coordinate startup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running initializers
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;initialize!&lt;/code&gt; method calls &lt;code&gt;run_initializers&lt;/code&gt;, which executes the registered initializers in dependency order.&lt;/p&gt;

&lt;p&gt;The initializers are organized into three broad groups:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Bootstrap initializers:&lt;/strong&gt; prepare the application and its environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Railtie initializers:&lt;/strong&gt; configure and integrate Rails frameworks, engines, and application-level behavior.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Finisher initializers:&lt;/strong&gt; perform later initialization work and finalize the application setup.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The ordering matters because some initializers depend on work completed by earlier ones.&lt;/p&gt;

&lt;p&gt;For example, a framework may need its configuration to be loaded before another initializer uses it.&lt;/p&gt;

&lt;p&gt;Application-specific initializers in &lt;code&gt;config/initializers&lt;/code&gt; also participate in this process.&lt;/p&gt;

&lt;p&gt;Once initialization finishes, &lt;code&gt;Rails.application&lt;/code&gt; is ready to serve as the application object passed to Rack.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. From &lt;code&gt;Rails.application&lt;/code&gt; to the HTTP server
&lt;/h2&gt;

&lt;p&gt;Remember this line in &lt;code&gt;config.ru&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;application&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It tells Rack to use the Rails application as the Rack endpoint.&lt;/p&gt;

&lt;p&gt;Rack builds the middleware application, and incoming requests pass through the configured middleware stack before reaching Rails' request-handling components.&lt;/p&gt;

&lt;p&gt;The request eventually reaches the router and the relevant controller, depending on the application and request.&lt;/p&gt;

&lt;p&gt;Meanwhile, Rackup starts the configured server adapter—typically Puma—which listens for HTTP connections and dispatches requests to the Rack application.&lt;/p&gt;

&lt;p&gt;So there are two related but different responsibilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rails&lt;/strong&gt; initializes the application and prepares request-handling behavior.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rackup and the server adapter&lt;/strong&gt; provide the interface that runs the application and accepts HTTP requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That separation is one of the reasons Rails can run on different compatible Rack server implementations.&lt;/p&gt;

&lt;h2&gt;
  
  
  The complete flow
&lt;/h2&gt;

&lt;p&gt;Here is the sequence in simplified form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bin/rails
   |
   v
config/boot.rb
   |
   v
rails/commands.rb
   |
   v
Rails::Command.invoke
   |
   v
ServerCommand
   |
   v
Rails::Server
   |
   v
config/application.rb
   |
   v
server.start
   |
   v
Rackup loads config.ru
   |
   v
config/environment.rb
   |
   v
Rails.application.initialize!
   |
   v
Initializers run
   |
   v
run Rails.application
   |
   v
Rack middleware + server adapter
   |
   v
The application is serving requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The diagram is a conceptual overview rather than a complete call stack. Some steps invoke other setup routines, and the exact implementation can vary by Rails and Rackup version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Tracing Rails initialization helped me understand that starting a Rails server is not one operation. It's a sequence of responsibilities shared between the Rails command system, application configuration, the initializer infrastructure, Rack, and the server adapter.&lt;/p&gt;

&lt;p&gt;If you want to explore the implementation further, the &lt;a href="https://guides.rubyonrails.org/v8.0.2/initialization.html" rel="noopener noreferrer"&gt;official Rails initialization guide&lt;/a&gt; is an excellent place to continue.&lt;/p&gt;

&lt;p&gt;Understanding this flow is especially useful when debugging boot errors, investigating initializer order, or figuring out where a configuration change takes effect.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>development</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>Learning Go as a Ruby Developer #7: Finally Understanding Pointers</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Tue, 06 Oct 2026 15:30:00 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-7-finally-understanding-pointers-4lkk</link>
      <guid>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-7-finally-understanding-pointers-4lkk</guid>
      <description>&lt;p&gt;Pointers were probably one of the concepts I was most curious about when I started learning Go.&lt;/p&gt;

&lt;p&gt;Coming from Ruby, I don't normally think about memory addresses or explicitly passing references around.&lt;/p&gt;

&lt;p&gt;In Go, pointers are much more visible.&lt;/p&gt;

&lt;p&gt;At first, they looked like one of those concepts I remembered from C++ and hoped I wouldn't have to think about again.&lt;/p&gt;

&lt;p&gt;But once I understood what they actually represent, they became much less scary.&lt;/p&gt;




&lt;h2&gt;
  
  
  So What Is a Pointer?
&lt;/h2&gt;

&lt;p&gt;A pointer is a value that stores the &lt;strong&gt;memory address of another value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;

&lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;amp;&lt;/code&gt; operator gives us the address of &lt;code&gt;age&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which represents the value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which represents the address where &lt;code&gt;30&lt;/code&gt; is stored.&lt;/p&gt;




&lt;h2&gt;
  
  
  Getting the Value Back
&lt;/h2&gt;

&lt;p&gt;If we have a pointer, we can use &lt;code&gt;*&lt;/code&gt; to get the value stored at that address.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;

&lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;age&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;pointer&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first &lt;code&gt;Println&lt;/code&gt; gives us the memory address.&lt;/p&gt;

&lt;p&gt;The second gives us:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because &lt;code&gt;*pointer&lt;/code&gt; means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to the address stored in &lt;code&gt;pointer&lt;/code&gt; and give me the value there.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is called &lt;strong&gt;dereferencing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So we have two operators to remember:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;amp; → get the address
* → get the value at that address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Changing a Value Through a Pointer
&lt;/h2&gt;

&lt;p&gt;This is where pointers become really useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;

&lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;

&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;31&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;31
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We didn't directly change &lt;code&gt;age&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We changed the value at the address stored in &lt;code&gt;pointer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;pointer&lt;/code&gt; points to &lt;code&gt;age&lt;/code&gt;, the original value changes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pointers with Functions
&lt;/h2&gt;

&lt;p&gt;This becomes especially useful when passing values to functions.&lt;/p&gt;

&lt;p&gt;Consider this function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;changeAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;31&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;

&lt;span class="n"&gt;changeAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the value is still:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's because the function receives a copy of the value.&lt;/p&gt;

&lt;p&gt;If we want the function to modify the original value, we can pass a pointer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;changeAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;31&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&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 go"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;

&lt;span class="n"&gt;changeAge&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;age&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the result is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;31
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We pass the address:&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="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and inside the function we dereference it:&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  This Is Where Ruby Feels Very Different
&lt;/h2&gt;

&lt;p&gt;In Ruby, I don't normally have to think about passing a memory address.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change_name&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Alice"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;user&lt;/code&gt; is an object, the method can modify that object.&lt;/p&gt;

&lt;p&gt;Ruby handles the object references for me.&lt;/p&gt;

&lt;p&gt;Go makes the relationship much more explicit.&lt;/p&gt;

&lt;p&gt;I have to decide whether I'm passing the value itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;changeAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or a pointer to the value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;changeAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That distinction matters.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pointers and Structs
&lt;/h2&gt;

&lt;p&gt;Pointers become especially common when working with structs.&lt;/p&gt;

&lt;p&gt;Let's use the &lt;code&gt;Person&lt;/code&gt; struct from the previous article:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;We can create a pointer to a &lt;code&gt;Person&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;person&lt;/code&gt; is a pointer to a &lt;code&gt;Person&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can still access its fields directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go automatically dereferences the pointer when accessing the field.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is effectively shorthand for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a nice convenience because we don't have to write &lt;code&gt;*&lt;/code&gt; every time we access a field.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pointer Receivers
&lt;/h2&gt;

&lt;p&gt;This also connects to something we saw in Part 3: &lt;strong&gt;methods&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Remember that Go methods can have a receiver:&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;p&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SayHello&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;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a &lt;strong&gt;value receiver&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The method receives a copy of the &lt;code&gt;Person&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we want the method to modify the original &lt;code&gt;Person&lt;/code&gt;, we can use a pointer receiver:&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;p&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bob"&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;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bob
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pointer receiver allows the method to modify the original value.&lt;/p&gt;




&lt;h2&gt;
  
  
  Value Receiver vs Pointer Receiver
&lt;/h2&gt;

&lt;p&gt;This distinction is important:&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;p&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means the method receives a copy.&lt;/p&gt;

&lt;p&gt;While:&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;p&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means the method receives a pointer to the original value.&lt;/p&gt;

&lt;p&gt;Coming from Ruby, this is one of those things I don't normally have to think about.&lt;/p&gt;

&lt;p&gt;In Ruby, I can simply mutate the object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Bob"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go makes the decision more explicit.&lt;/p&gt;




&lt;h2&gt;
  
  
  Go vs Ruby
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;th&gt;Ruby&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Reference to a value&lt;/td&gt;
&lt;td&gt;Pointer&lt;/td&gt;
&lt;td&gt;Object reference handled by Ruby&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Get address&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;value&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No equivalent syntax&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dereference&lt;/td&gt;
&lt;td&gt;&lt;code&gt;*pointer&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No explicit dereference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Modify through reference&lt;/td&gt;
&lt;td&gt;&lt;code&gt;*pointer = value&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Mutate the object directly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pointer receiver&lt;/td&gt;
&lt;td&gt;&lt;code&gt;func (p *Person)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No equivalent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Value receiver&lt;/td&gt;
&lt;td&gt;&lt;code&gt;func (p Person)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No direct equivalent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory address&lt;/td&gt;
&lt;td&gt;Explicitly accessible through pointers&lt;/td&gt;
&lt;td&gt;Normally hidden&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  What Surprised Me
&lt;/h2&gt;

&lt;p&gt;The syntax wasn't actually the hardest part.&lt;/p&gt;

&lt;p&gt;It was changing the way I think about values.&lt;/p&gt;

&lt;p&gt;In Ruby, I can mostly think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I have an object."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In Go, I sometimes have to ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Do I have the value, or do I have a pointer to the value?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That distinction affects how data is passed to functions and how methods can modify it.&lt;/p&gt;

&lt;p&gt;Once I started thinking about a pointer as simply:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"A value that tells me where another value lives"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;the concept became much easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Main Idea
&lt;/h2&gt;

&lt;p&gt;I initially thought pointers were going to be one of the most complicated parts of learning Go.&lt;/p&gt;

&lt;p&gt;They're definitely more visible than they are in Ruby, but the basic idea is pretty simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;amp; → give me the address
* → give me the value at that address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And when working with structs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;person&lt;/code&gt; points to a &lt;code&gt;Person&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While:&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;p&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;this method works with a pointer to the original &lt;code&gt;Person&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As a Ruby developer, I probably won't need to think about memory addresses every day.&lt;/p&gt;

&lt;p&gt;But understanding pointers gives me a better picture of what's actually happening when Go passes values around.&lt;/p&gt;

&lt;p&gt;And that's becoming one of my favorite parts of learning Go:&lt;/p&gt;

&lt;p&gt;Ruby lets me stay comfortably high-level.&lt;/p&gt;

&lt;p&gt;Go occasionally makes me look under the hood.&lt;/p&gt;

</description>
      <category>go</category>
      <category>ruby</category>
      <category>rails</category>
      <category>programming</category>
    </item>
    <item>
      <title>Learning Go as a Ruby Developer #6: Living Without Classes</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Fri, 04 Sep 2026 14:44:56 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-6-living-without-classes-503l</link>
      <guid>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-6-living-without-classes-503l</guid>
      <description>&lt;p&gt;One of the questions I had while learning Go was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If Go doesn't have classes, where do I put my data?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In Ruby, I'm used to creating a class and defining its attributes and behavior there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:age&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
    &lt;span class="vi"&gt;@age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go doesn't have classes.&lt;/p&gt;

&lt;p&gt;Instead, one of the building blocks it gives us is the &lt;strong&gt;struct&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A struct is a collection of fields that allows us to group related data together.&lt;/p&gt;




&lt;h3&gt;
  
  
  Structs in Go
&lt;/h3&gt;

&lt;p&gt;A struct is defined using the &lt;code&gt;struct&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Here we're defining a type called &lt;code&gt;Person&lt;/code&gt; with two fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Name&lt;/code&gt;, which is a &lt;code&gt;string&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Age&lt;/code&gt;, which is an &lt;code&gt;int&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can then create a value of that type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And access its fields using &lt;code&gt;.&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;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;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;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;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This feels quite similar to accessing attributes on a Ruby object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;
&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;age&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the underlying idea is different.&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating Struct Values
&lt;/h2&gt;

&lt;p&gt;There are several ways to initialize a struct.&lt;/p&gt;

&lt;p&gt;The first is to provide the values in the same order as the fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also explicitly specify the field names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This second form is easier to understand, especially when a struct has many fields.&lt;/p&gt;

&lt;p&gt;We can also initialize only some fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fields we don't specify receive their &lt;strong&gt;zero value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;Age&lt;/code&gt; would be:&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="m"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can even create an empty struct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In that case, all fields get their respective zero values.&lt;/p&gt;

&lt;p&gt;This connects nicely with what we saw in Part 2 when talking about zero values.&lt;/p&gt;




&lt;h2&gt;
  
  
  Structs and Pointers
&lt;/h2&gt;

&lt;p&gt;Structs can also be used with pointers.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;person&lt;/code&gt; is a pointer to a &lt;code&gt;Person&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can still access the fields directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go allows this as a convenient shorthand for dereferencing the pointer first.&lt;/p&gt;

&lt;p&gt;Conceptually, this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is an easier way of working with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was another place where Go reminded me of C++.&lt;/p&gt;

&lt;p&gt;As a Ruby developer, pointers aren't something I normally think about when working with objects.&lt;/p&gt;

&lt;p&gt;In Go, understanding whether you're working with a value or a pointer becomes much more important.&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating Your Own Types
&lt;/h2&gt;

&lt;p&gt;Go also allows us to create a new named type based on another type.&lt;/p&gt;

&lt;p&gt;The syntax is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;typename&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;datatype&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;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Banana&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;Banana&lt;/code&gt; is its own type based on &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can declare a variable using it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;myBanana&lt;/span&gt; &lt;span class="n"&gt;Banana&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;50&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This might look unnecessary at first.&lt;/p&gt;

&lt;p&gt;Why create &lt;code&gt;Banana&lt;/code&gt; if it's basically an &lt;code&gt;int&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;The important idea is that we're giving a type a specific meaning.&lt;/p&gt;

&lt;p&gt;Instead of having an application full of anonymous &lt;code&gt;int&lt;/code&gt; values, we can create named types that communicate what those values represent.&lt;/p&gt;




&lt;h2&gt;
  
  
  Ruby's Approach
&lt;/h2&gt;

&lt;p&gt;Ruby is dynamically typed, so we normally don't create separate primitive-based types in the same way.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable simply references an &lt;code&gt;Integer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we want to represent something like a person, we'd typically create a class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:age&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go separates these concepts.&lt;/p&gt;

&lt;p&gt;A struct gives us a way to group fields:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;And a custom type gives us a way to define a new named type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Banana&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There doesn't need to be a class involved.&lt;/p&gt;




&lt;h2&gt;
  
  
  Struct vs Ruby Class
&lt;/h2&gt;

&lt;p&gt;This is where I initially had to change my mental model.&lt;/p&gt;

&lt;p&gt;A Ruby class usually combines &lt;strong&gt;data and behavior&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;say_hello&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A Go struct, on the other hand, primarily describes the data:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;And later, behavior can be associated with the type using methods.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SayHello&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;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll look at methods more closely when we continue exploring how Go handles behavior without classes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Go vs Ruby
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;th&gt;Ruby&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Group related data&lt;/td&gt;
&lt;td&gt;&lt;code&gt;struct&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;class&lt;/code&gt;, &lt;code&gt;Struct&lt;/code&gt;, etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Define a struct&lt;/td&gt;
&lt;td&gt;&lt;code&gt;type Person struct { ... }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;class Person&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access fields&lt;/td&gt;
&lt;td&gt;&lt;code&gt;person.Name&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;person.name&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Initialize&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Person{Name: "Alice"}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Person.new("Alice")&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Missing fields&lt;/td&gt;
&lt;td&gt;Get zero value&lt;/td&gt;
&lt;td&gt;Depends on initialization&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Empty value&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Person{}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Person.new&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pointer to value&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;Person{...}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No explicit pointer syntax&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Custom type&lt;/td&gt;
&lt;td&gt;&lt;code&gt;type Banana int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No direct equivalent in the same type-system sense&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Main idea
&lt;/h2&gt;

&lt;p&gt;Go doesn't really ask you to think in terms of classes first.&lt;/p&gt;

&lt;p&gt;Instead, you can start with the data:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;And that's it.&lt;/p&gt;

&lt;p&gt;No constructor is required just to create a value.&lt;/p&gt;

&lt;p&gt;No inheritance is involved.&lt;/p&gt;

&lt;p&gt;No class hierarchy is necessary.&lt;/p&gt;

&lt;p&gt;You simply define the type and create values from it.&lt;/p&gt;




&lt;p&gt;But TBH I still like Ruby's object model.&lt;/p&gt;

&lt;p&gt;When working on a Rails application, classes provide a natural place for models, services, and domain behavior.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>go</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Learning Go as a Ruby Developer #5: Strings and formatting</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Mon, 17 Aug 2026 14:24:35 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-part-5-strings-and-formatting-57j5</link>
      <guid>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-part-5-strings-and-formatting-57j5</guid>
      <description>&lt;p&gt;A Go string is essentially a &lt;strong&gt;sequence of bytes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That sounds like a small implementation detail, but once you start dealing with UTF-8 and Unicode, it becomes important to understand what's actually happening.&lt;/p&gt;




&lt;h2&gt;
  
  
  Strings in Go
&lt;/h2&gt;

&lt;p&gt;A basic Go string looks familiar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strings can use double quotes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello &lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt; World"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or backticks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;`Hello \t World`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And these two behave differently.&lt;/p&gt;

&lt;p&gt;With double quotes, escape sequences are interpreted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello &lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt; World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;\t&lt;/code&gt; becomes a tab.&lt;/p&gt;

&lt;p&gt;With backticks, the contents are treated as a raw string literal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;`Hello \t World`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;\t&lt;/code&gt; remains part of the string.&lt;/p&gt;

&lt;p&gt;This is useful when you want the contents of a string to be interpreted literally.&lt;/p&gt;




&lt;h2&gt;
  
  
  A String Is a Sequence of Bytes
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello You!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can convert the string to a byte slice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%v&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%v&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello You!
[72 101 108 108 111 32 89 111 117 33]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those numbers represent the individual bytes that make up the string.&lt;/p&gt;

&lt;p&gt;For example, the first character:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;H
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is represented by the byte:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;72
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So when Go says a string is a sequence of bytes, this isn't just an abstract definition.&lt;/p&gt;

&lt;p&gt;We can actually see those bytes.&lt;/p&gt;




&lt;h2&gt;
  
  
  So Where Does UTF-8 Come In?
&lt;/h2&gt;

&lt;p&gt;Computers ultimately store data as binary.&lt;/p&gt;

&lt;p&gt;That means we need a way to represent text as numbers that computers can store.&lt;/p&gt;

&lt;p&gt;That's where character encodings come in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UTF-8&lt;/strong&gt; stands for:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unicode Transformation Format – 8-bit&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It provides a way to encode Unicode characters using between 1 and 4 bytes per character.&lt;/p&gt;

&lt;p&gt;This allows the same encoding to represent characters from many different writing systems.&lt;/p&gt;

&lt;p&gt;For example, simple ASCII characters such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A
B
C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can be represented using a single byte.&lt;/p&gt;

&lt;p&gt;Other Unicode characters can require multiple bytes.&lt;/p&gt;

&lt;p&gt;And this is where the distinction between &lt;strong&gt;characters&lt;/strong&gt; and &lt;strong&gt;bytes&lt;/strong&gt; becomes important.&lt;/p&gt;




&lt;h2&gt;
  
  
  Bytes Are Not Characters
&lt;/h2&gt;

&lt;p&gt;Consider this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each character in this particular string can be represented by one byte.&lt;/p&gt;

&lt;p&gt;But that doesn't mean every character is one byte.&lt;/p&gt;

&lt;p&gt;For example, Unicode contains characters that require multiple bytes when encoded as UTF-8.&lt;/p&gt;

&lt;p&gt;So if we're dealing with text outside the ASCII range, we can't simply assume:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 character = 1 byte
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of those details I rarely think about when writing normal Ruby application code.&lt;/p&gt;

&lt;p&gt;Go makes the underlying representation much more visible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Ruby Comparison
&lt;/h2&gt;

&lt;p&gt;Ruby also lets us inspect the bytes of a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello You!"&lt;/span&gt;

&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bytes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives us the byte values making up the string.&lt;/p&gt;

&lt;p&gt;So the underlying concept isn't completely foreign to Ruby.&lt;/p&gt;

&lt;p&gt;The difference is that Go's type system and standard library make this distinction much more explicit.&lt;/p&gt;

&lt;p&gt;That mental model becomes particularly useful when working with files, network protocols, encodings, and other low-level operations.&lt;/p&gt;




&lt;h2&gt;
  
  
  Formatting with &lt;code&gt;fmt&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Another part of my notes that I found useful was Go's &lt;code&gt;fmt&lt;/code&gt; package.&lt;/p&gt;

&lt;p&gt;It provides formatted output similar to Ruby's &lt;code&gt;printf&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;30&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name: %s, Age: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;%&lt;/code&gt; symbols are called &lt;strong&gt;format verbs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They tell &lt;code&gt;fmt&lt;/code&gt; how to format the value.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;%v&lt;/code&gt; — Print the Value
&lt;/h3&gt;

&lt;p&gt;When you're not particularly concerned about the exact formatting, &lt;code&gt;%v&lt;/code&gt; is useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Alice"&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%v&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It prints the value using its default format.&lt;/p&gt;

&lt;p&gt;This is one of the formatting verbs I expect to use frequently.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;code&gt;%T&lt;/code&gt; — Print the Type
&lt;/h3&gt;

&lt;p&gt;One I particularly like while learning Go is &lt;code&gt;%T&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;30&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%T&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prints the type of the value.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Coming from Ruby, this reminds me of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are useful when you're trying to understand what you're actually working with.&lt;/p&gt;




&lt;h3&gt;
  
  
  Formatting Numbers
&lt;/h3&gt;

&lt;p&gt;Go also provides formatting verbs for different number representations.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;42&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%b&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%o&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%x&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&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;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%X&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These represent the number in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;%d&lt;/code&gt; — decimal&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%b&lt;/code&gt; — binary&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%o&lt;/code&gt; — octal&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%x&lt;/code&gt; — hexadecimal using lowercase letters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;%X&lt;/code&gt; — hexadecimal using uppercase letters&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can be represented as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;101010
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in binary.&lt;/p&gt;




&lt;h2&gt;
  
  
  Characters and Unicode Formatting
&lt;/h2&gt;

&lt;p&gt;Go also provides formatting verbs for characters and Unicode values.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%c&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;65&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because &lt;code&gt;65&lt;/code&gt; corresponds to the Unicode code point for &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There's also &lt;code&gt;%q&lt;/code&gt; for quoted character/string representations.&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;%#U&lt;/code&gt; can be used to display a Unicode code point in Go syntax.&lt;/p&gt;

&lt;p&gt;These formatting options become particularly useful when you're debugging text and encoding-related issues.&lt;/p&gt;




&lt;h2&gt;
  
  
  Go vs Ruby
&lt;/h2&gt;

&lt;p&gt;Here's how some of these concepts compare.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;th&gt;Ruby&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Sequence of bytes&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;String&lt;/code&gt; object&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;String literal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"Hello"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"Hello"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Raw string&lt;/td&gt;
&lt;td&gt;&lt;code&gt;`Hello`&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;%q{Hello}&lt;/code&gt; / other forms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inspect bytes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;[]byte(str)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;str.bytes&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Print&lt;/td&gt;
&lt;td&gt;&lt;code&gt;fmt.Println&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;puts&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Formatted output&lt;/td&gt;
&lt;td&gt;&lt;code&gt;fmt.Printf&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;printf&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Print type&lt;/td&gt;
&lt;td&gt;&lt;code&gt;%T&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;.class&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Default formatting&lt;/td&gt;
&lt;td&gt;&lt;code&gt;%v&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;puts&lt;/code&gt; / interpolation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Decimal&lt;/td&gt;
&lt;td&gt;&lt;code&gt;%d&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;printf("%d", n)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Binary&lt;/td&gt;
&lt;td&gt;&lt;code&gt;%b&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;printf("%b", n)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hexadecimal&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;%x&lt;/code&gt; / &lt;code&gt;%X&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;printf("%x", n)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Character&lt;/td&gt;
&lt;td&gt;&lt;code&gt;%c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;chr&lt;/code&gt; / formatting&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;When you're working with files, network communication, binary data, or encoding problems, understanding that you're ultimately dealing with bytes becomes extremely valuable.&lt;/p&gt;

&lt;p&gt;And this is another recurring theme in my Go journey:&lt;/p&gt;

&lt;p&gt;Ruby often lets me forget what's happening underneath.&lt;/p&gt;

&lt;p&gt;Go occasionally makes me look underneath the hood.&lt;/p&gt;

&lt;p&gt;And sometimes, that's exactly what I need to become a better developer.&lt;/p&gt;

</description>
      <category>go</category>
      <category>ruby</category>
      <category>programming</category>
    </item>
    <item>
      <title>Learning Go as a Ruby Developer #4: What can we do with a single "for"</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Fri, 07 Aug 2026 16:32:53 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-part-4-what-can-we-do-with-a-single-for-3320</link>
      <guid>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-part-4-what-can-we-do-with-a-single-for-3320</guid>
      <description>&lt;p&gt;So loops in ruby has several ways to implement&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;until&lt;/span&gt; &lt;span class="n"&gt;finished&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go looks at all of this and basically says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We only need one loop.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That loop is &lt;code&gt;for&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before getting into loops, though, let's look at conditions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conditions in Go
&lt;/h2&gt;

&lt;p&gt;Go has the familiar &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else if&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;18&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;"Adult"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Minor"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax is pretty close to Ruby, but there is one important difference.&lt;/p&gt;

&lt;p&gt;Go does &lt;strong&gt;not&lt;/strong&gt; use &lt;code&gt;end&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Adult"&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Minor"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go uses curly braces to define the block:&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;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;18&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;"Adult"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Minor"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Coming from Ruby, I had to get used to seeing &lt;code&gt;{}&lt;/code&gt; everywhere.&lt;/p&gt;




&lt;h2&gt;
  
  
  Boolean Operators
&lt;/h2&gt;

&lt;p&gt;The familiar operators are there:&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;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;active&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// not active&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;18&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// exactly 18&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And for inequality:&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;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;18&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// not 18&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the basic logic isn't particularly difficult to transition to.&lt;/p&gt;

&lt;p&gt;It's mostly the syntax that's different.&lt;/p&gt;

&lt;p&gt;now going back to loops&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;code&gt;for&lt;/code&gt; Loop
&lt;/h2&gt;

&lt;p&gt;Go has only one looping construct:&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;for&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The traditional form looks like this:&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;for&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are three parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initialization&lt;/strong&gt; — runs before the first iteration&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Condition&lt;/strong&gt; — checked before every iteration&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Post statement&lt;/strong&gt; — runs after every iteration&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is very similar to the traditional C-style loop.&lt;/p&gt;

&lt;p&gt;And this is where my C++ nostalgia started showing up.&lt;/p&gt;

&lt;p&gt;Go doesn't have separate &lt;code&gt;times&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt;, or &lt;code&gt;until&lt;/code&gt; loops.&lt;/p&gt;

&lt;p&gt;Instead, you use &lt;code&gt;for&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One keyword, several forms.&lt;/p&gt;




&lt;h2&gt;
  
  
  The While-Like &lt;code&gt;for&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;What if we don't need the initialization and post statements?&lt;/p&gt;

&lt;p&gt;No problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This behaves like a &lt;code&gt;while&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;In other words:&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;for&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is Go's version of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of those examples where Go's simplicity starts making more sense.&lt;/p&gt;

&lt;p&gt;Instead of introducing another keyword for &lt;code&gt;while&lt;/code&gt;, Go reuses &lt;code&gt;for&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Infinite Loops
&lt;/h2&gt;

&lt;p&gt;You can even remove the condition completely:&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;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// keep going&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates an infinite loop.&lt;/p&gt;

&lt;p&gt;Of course, you'll usually want some way to escape it.&lt;/p&gt;

&lt;p&gt;That's where &lt;code&gt;break&lt;/code&gt; comes in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;for&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;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The loop keeps running until the &lt;code&gt;break&lt;/code&gt; statement is reached.&lt;/p&gt;

&lt;p&gt;Ruby has a similar concept:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="kp"&gt;loop&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;

  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
  &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, Go doesn't need a separate &lt;code&gt;loop&lt;/code&gt; construct.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for&lt;/code&gt; handles it.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Go also provides the familiar &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; keywords.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;break&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;break&lt;/code&gt; exits the loop completely.&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;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&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;i&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;break&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;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The loop stops when &lt;code&gt;i&lt;/code&gt; reaches &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;continue&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;continue&lt;/code&gt; skips the remaining code in the current iteration and moves to the next one.&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;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&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;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;continue&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;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This skips even numbers and prints the odd ones.&lt;/p&gt;

&lt;p&gt;Ruby has the same concepts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="k"&gt;next&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;even?&lt;/span&gt;

  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ruby uses &lt;code&gt;next&lt;/code&gt; where Go uses &lt;code&gt;continue&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;range&lt;/code&gt;: The Go Way to Iterate Over Collections
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop becomes even more interesting when combined with &lt;code&gt;range&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;range&lt;/code&gt; form allows you to iterate over collections such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;arrays&lt;/li&gt;
&lt;li&gt;slices&lt;/li&gt;
&lt;li&gt;strings&lt;/li&gt;
&lt;li&gt;maps&lt;/li&gt;
&lt;li&gt;channels&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Bob"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Charlie"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;names&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;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives us both the index and the value.&lt;/p&gt;

&lt;p&gt;The output would look conceptually like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 Alice
1 Bob
2 Charlie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Comparing &lt;code&gt;range&lt;/code&gt; with Ruby
&lt;/h3&gt;

&lt;p&gt;In Ruby, I'd probably write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Bob"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Charlie"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each_with_index&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, if I don't need the index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Go, &lt;code&gt;range&lt;/code&gt; handles both cases.&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;for&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;names&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;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if I only care about the value, I can ignore the index:&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;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;names&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;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;_&lt;/code&gt; tells Go that I intentionally don't need that value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Switch Statements
&lt;/h2&gt;

&lt;p&gt;Go also has &lt;code&gt;switch&lt;/code&gt; for conditional logic.&lt;/p&gt;

&lt;p&gt;A simple switch looks like this:&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;switch&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"Moneypenny"&lt;/span&gt;&lt;span class="o"&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;"Miss Moneypenny"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"Bond"&lt;/span&gt;&lt;span class="o"&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;"Bond, James Bond"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"Q"&lt;/span&gt;&lt;span class="o"&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;"This is Q"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&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;"Unknown"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike Ruby's &lt;code&gt;case&lt;/code&gt;, Go doesn't require an &lt;code&gt;end&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s2"&gt;"Moneypenny"&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Miss Moneypenny"&lt;/span&gt;
&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s2"&gt;"Bond"&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Bond, James Bond"&lt;/span&gt;
&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s2"&gt;"Q"&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"This is Q"&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Unknown"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there is another interesting form of Go's &lt;code&gt;switch&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Switch Without an Expression
&lt;/h2&gt;

&lt;p&gt;You can leave the value out completely:&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;switch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;18&lt;/span&gt;&lt;span class="o"&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;"Minor"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;18&lt;/span&gt;&lt;span class="o"&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;"Adult"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each &lt;code&gt;case&lt;/code&gt; is essentially a condition.&lt;/p&gt;

&lt;p&gt;This can be useful when you have several related conditions.&lt;/p&gt;

&lt;p&gt;Ruby's &lt;code&gt;case&lt;/code&gt; can also be used for conditional expressions, although the syntax and matching behavior are different.&lt;/p&gt;




&lt;h2&gt;
  
  
  Multiple Values in a Case
&lt;/h2&gt;

&lt;p&gt;A Go case can match multiple values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"Moneypenny"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Bond"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Dr No"&lt;/span&gt;&lt;span class="o"&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;"Secret agent"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="o"&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;"Unknown"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means we don't need separate cases for every value when they should produce the same result.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;code&gt;fallthrough&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;One thing that caught my attention was &lt;code&gt;fallthrough&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Normally, once a Go &lt;code&gt;switch&lt;/code&gt; finds a matching case, it stops.&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;switch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="o"&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;"first"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="o"&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;"second"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only the first case runs.&lt;/p&gt;

&lt;p&gt;If you explicitly use &lt;code&gt;fallthrough&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="o"&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;"first"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fallthrough&lt;/span&gt;

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="o"&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;"second"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="o"&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;"third"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first and second cases run, but the third does not.&lt;/p&gt;

&lt;p&gt;This is different from the behavior many people might expect from C-style switch statements, where falling through can happen unless you explicitly stop it.&lt;/p&gt;

&lt;p&gt;Go makes the behavior explicit with &lt;code&gt;fallthrough&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go vs Ruby: Control Flow
&lt;/h2&gt;

&lt;p&gt;Here's how the two languages compare.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;th&gt;Ruby&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Condition&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;if&lt;/code&gt; / &lt;code&gt;else&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;if&lt;/code&gt; / &lt;code&gt;else&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Not&lt;/td&gt;
&lt;td&gt;&lt;code&gt;!&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;!&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Equality&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inequality&lt;/td&gt;
&lt;td&gt;&lt;code&gt;!=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;!=&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Main loop&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Multiple constructs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;While loop&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for condition&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;while condition&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infinite loop&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for {}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;loop do&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Iterate collection&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for ... range&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;.each&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Skip iteration&lt;/td&gt;
&lt;td&gt;&lt;code&gt;continue&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;next&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exit loop&lt;/td&gt;
&lt;td&gt;&lt;code&gt;break&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;break&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Switch&lt;/td&gt;
&lt;td&gt;&lt;code&gt;switch&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;case&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple switch values&lt;/td&gt;
&lt;td&gt;&lt;code&gt;case "a", "b"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;when "a", "b"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Neither approach is necessarily better.&lt;/p&gt;

&lt;p&gt;They're just optimizing for different things.&lt;/p&gt;

</description>
      <category>go</category>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Design Patterns in Ruby #5: Abstract Factory Pattern</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Wed, 05 Aug 2026 16:02:00 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/design-patterns-in-ruby-5-abstract-factory-pattern-4ioa</link>
      <guid>https://dev.to/shroukabozeid/design-patterns-in-ruby-5-abstract-factory-pattern-4ioa</guid>
      <description>&lt;p&gt;In the previous article, we explored the &lt;strong&gt;Factory Method&lt;/strong&gt; pattern and saw how it delegates object creation to subclasses.&lt;/p&gt;

&lt;p&gt;But what if your application doesn't need to create just &lt;strong&gt;one object&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;What if it needs an entire &lt;strong&gt;family of related objects&lt;/strong&gt; that are designed to work together?&lt;/p&gt;

&lt;p&gt;That's exactly the problem the &lt;strong&gt;Abstract Factory&lt;/strong&gt; pattern solves.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Imagine you're building an e-commerce application that supports multiple payment providers.&lt;/p&gt;

&lt;p&gt;Your application currently integrates with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stripe&lt;/li&gt;
&lt;li&gt;PayPal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For each payment provider, you need several related objects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Payment processor&lt;/li&gt;
&lt;li&gt;A Refund processor&lt;/li&gt;
&lt;li&gt;A Customer service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Stripe, those objects might be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;StripePayment
StripeRefund
StripeCustomer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For PayPal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PayPalPayment
PayPalRefund
PayPalCustomer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you instantiate each object manually, your code quickly becomes filled with conditionals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="ss"&gt;:stripe&lt;/span&gt;
  &lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;StripePayment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="n"&gt;refund&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;StripeRefund&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;StripeCustomer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PayPalPayment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="n"&gt;refund&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PayPalRefund&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PayPalCustomer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem isn't creating a single object anymore.&lt;/p&gt;

&lt;p&gt;It's ensuring that &lt;strong&gt;all related objects come from the same payment provider&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The client shouldn't accidentally create a &lt;code&gt;StripePayment&lt;/code&gt; with a &lt;code&gt;PayPalRefund&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Abstract Factory
&lt;/h2&gt;

&lt;p&gt;The Gang of Four defines Abstract Factory as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Provide an interface for creating families of related or dependent objects without specifying their concrete classes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of asking for individual objects, the client asks a factory to provide an entire family.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;StripeFactory
 ├── Payment
 ├── Refund
 └── Customer

PayPalFactory
 ├── Payment
 ├── Refund
 └── Customer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client never knows which concrete classes it's using.&lt;/p&gt;

&lt;p&gt;It only communicates with the factory interface.&lt;/p&gt;




&lt;h2&gt;
  
  
  Structure
&lt;/h2&gt;

&lt;p&gt;The pattern consists of five participants.&lt;/p&gt;

&lt;h3&gt;
  
  
  Abstract Factory
&lt;/h3&gt;

&lt;p&gt;Defines the interface for creating every product in the family.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;create_payment&lt;/span&gt;
&lt;span class="n"&gt;create_refund&lt;/span&gt;
&lt;span class="n"&gt;create_customer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Concrete Factory
&lt;/h3&gt;

&lt;p&gt;Implements the Abstract Factory.&lt;/p&gt;

&lt;p&gt;Each concrete factory creates one complete product family.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;StripeFactory&lt;/li&gt;
&lt;li&gt;PayPalFactory&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Abstract Product
&lt;/h3&gt;

&lt;p&gt;Defines the common interface shared by each product.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Payment&lt;/li&gt;
&lt;li&gt;Refund&lt;/li&gt;
&lt;li&gt;Customer&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Concrete Products
&lt;/h3&gt;

&lt;p&gt;The concrete implementations belonging to a specific family.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;StripePayment
StripeRefund
StripeCustomer

PayPalPayment
PayPalRefund
PayPalCustomer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Client
&lt;/h3&gt;

&lt;p&gt;The client depends only on the Abstract Factory.&lt;/p&gt;

&lt;p&gt;At runtime, it receives whichever concrete factory is needed.&lt;/p&gt;

&lt;p&gt;This completely decouples the client from concrete implementations.&lt;/p&gt;




&lt;h2&gt;
  
  
  Ruby Example
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Products
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StripePayment&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Charging $&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; with Stripe"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StripeRefund&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transaction_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Refunding &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;transaction_id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; through Stripe"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StripeCustomer&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Creating Stripe customer: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PayPalPayment&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Charging $&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; with PayPal"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PayPalRefund&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transaction_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Refunding &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;transaction_id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; through PayPal"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PayPalCustomer&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Creating PayPal customer: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Abstract Factory
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentProviderFactory&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_payment&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;NotImplementedError&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_refund&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;NotImplementedError&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_customer&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;NotImplementedError&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Concrete Factories
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StripeFactory&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;PaymentProviderFactory&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_payment&lt;/span&gt;
    &lt;span class="no"&gt;StripePayment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_refund&lt;/span&gt;
    &lt;span class="no"&gt;StripeRefund&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_customer&lt;/span&gt;
    &lt;span class="no"&gt;StripeCustomer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PayPalFactory&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;PaymentProviderFactory&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_payment&lt;/span&gt;
    &lt;span class="no"&gt;PayPalPayment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_refund&lt;/span&gt;
    &lt;span class="no"&gt;PayPalRefund&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_customer&lt;/span&gt;
    &lt;span class="no"&gt;PayPalCustomer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Client
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CheckoutService&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@factory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_customer&lt;/span&gt;
    &lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@factory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_payment&lt;/span&gt;

    &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;refund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transaction_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;refund&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="vi"&gt;@factory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_refund&lt;/span&gt;
    &lt;span class="n"&gt;refund&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transaction_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;StripeFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;checkout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;CheckoutService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Alice"&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="n"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"txn_123"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Switching to PayPal requires changing only one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;PayPalFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;checkout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;CheckoutService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rest of the application remains unchanged.&lt;/p&gt;

&lt;p&gt;Notice that &lt;code&gt;CheckoutService&lt;/code&gt; never references &lt;code&gt;StripePayment&lt;/code&gt;, &lt;code&gt;PayPalPayment&lt;/code&gt;, or any other concrete implementation.&lt;/p&gt;

&lt;p&gt;It only communicates with the Abstract Factory.&lt;/p&gt;




&lt;h2&gt;
  
  
  Factory Method vs Abstract Factory
&lt;/h2&gt;

&lt;p&gt;Although both patterns deal with object creation, they solve different problems.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Factory Method&lt;/th&gt;
&lt;th&gt;Abstract Factory&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Creates one product&lt;/td&gt;
&lt;td&gt;Creates a family of related products&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Uses inheritance&lt;/td&gt;
&lt;td&gt;Uses object composition&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Subclasses decide what to create&lt;/td&gt;
&lt;td&gt;A factory object decides which family to create&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best when object creation varies&lt;/td&gt;
&lt;td&gt;Best when entire product families vary&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Another way to think about it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Factory Method&lt;/strong&gt; asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Which object should I create?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Abstract Factory&lt;/strong&gt; asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Which family of objects should I create?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Adding New Products vs New Families
&lt;/h2&gt;

&lt;p&gt;Suppose our payment providers now support &lt;strong&gt;webhooks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We need another product:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;create_webhook&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Abstract Factory, the abstract factory interface must change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentProviderFactory&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_payment&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_refund&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_customer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_webhook&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every concrete factory (&lt;code&gt;StripeFactory&lt;/code&gt;, &lt;code&gt;PayPalFactory&lt;/code&gt;, etc.) must also implement &lt;code&gt;create_webhook&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On the other hand, if you add a completely new payment provider—say &lt;strong&gt;Adyen&lt;/strong&gt;—you simply introduce a new concrete factory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AdyenFactory
 ├── AdyenPayment
 ├── AdyenRefund
 ├── AdyenCustomer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The existing factories remain unchanged.&lt;/p&gt;

&lt;p&gt;That's why people often say:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Factory Method is open to new creators.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Abstract Factory is open to new product families.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Where You'll See It in Rails
&lt;/h2&gt;

&lt;p&gt;Although Rails doesn't explicitly label it as the Abstract Factory pattern, the idea appears in several places.&lt;/p&gt;

&lt;h3&gt;
  
  
  Active Storage
&lt;/h3&gt;

&lt;p&gt;Depending on your configuration, Rails creates a family of storage-related objects for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Local Disk&lt;/li&gt;
&lt;li&gt;Amazon S3&lt;/li&gt;
&lt;li&gt;Google Cloud Storage&lt;/li&gt;
&lt;li&gt;Azure Blob Storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your application interacts with a common interface while Rails supplies the correct implementation behind the scenes.&lt;/p&gt;




&lt;h2&gt;
  
  
  When Should You Use It?
&lt;/h2&gt;

&lt;p&gt;Use Abstract Factory when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your application works with multiple product families.&lt;/li&gt;
&lt;li&gt;Objects within a family should always be used together.&lt;/li&gt;
&lt;li&gt;You want to switch implementations through configuration.&lt;/li&gt;
&lt;li&gt;You want to isolate your application from concrete classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Avoid it when you only need to create a single object. In that case, &lt;strong&gt;Factory Method&lt;/strong&gt; or even a &lt;strong&gt;Simple Factory&lt;/strong&gt; is usually a better fit.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Factory Method&lt;/strong&gt; pattern creates &lt;strong&gt;one object&lt;/strong&gt; and relies on &lt;strong&gt;inheritance&lt;/strong&gt; to determine which concrete product to instantiate.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Abstract Factory&lt;/strong&gt; pattern creates &lt;strong&gt;families of related objects&lt;/strong&gt; and relies on &lt;strong&gt;composition&lt;/strong&gt; to swap entire implementations at runtime.&lt;/p&gt;

&lt;p&gt;A simple rule to remember is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Factory Method answers:&lt;/strong&gt; &lt;em&gt;Which object should I create?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Abstract Factory answers:&lt;/strong&gt; &lt;em&gt;Which family of objects should I create?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;for a full example in ruby check this &lt;a href="https://github.com/ShroukAbozeid/design-pattern/tree/main/factory/abstract_factory" rel="noopener noreferrer"&gt;https://github.com/ShroukAbozeid/design-pattern/tree/main/factory/abstract_factory&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>Design Patterns in Ruby #4: Simple Factory vs Factory Method</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Tue, 04 Aug 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/design-patterns-in-ruby-4-simple-factory-vs-factory-method-4oml</link>
      <guid>https://dev.to/shroukabozeid/design-patterns-in-ruby-4-simple-factory-vs-factory-method-4oml</guid>
      <description>&lt;p&gt;When building applications, creating an object is rarely as simple as calling &lt;code&gt;User.new&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Different objects may require different initialization logic, validation, external API calls, or configuration. If every class is responsible for deciding &lt;strong&gt;what&lt;/strong&gt; object to create, this logic quickly becomes scattered throughout the codebase.&lt;/p&gt;

&lt;p&gt;Factory patterns solve this problem by separating &lt;strong&gt;object creation&lt;/strong&gt; from &lt;strong&gt;object usage&lt;/strong&gt;, making the code easier to maintain and extend.&lt;/p&gt;

&lt;p&gt;Let's look at the two most common approaches: &lt;strong&gt;Simple Factory&lt;/strong&gt; and &lt;strong&gt;Factory Method&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple Factory
&lt;/h2&gt;

&lt;p&gt;it is not actually a design pattern by the book but it is commonly used.&lt;/p&gt;

&lt;p&gt;it has a very basic idea of encapsulating the objects creation from it’s usage&lt;/p&gt;

&lt;p&gt;it has a class ‘Simple Factory’ that contains the logic of creating objects , that factory is used in a “client”  to create the objects then use it &lt;/p&gt;

&lt;p&gt;Instead of allowing clients to instantiate objects directly, a single factory class becomes responsible for deciding which object should be created.&lt;/p&gt;

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

&lt;p&gt;A Simple Factory consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;factory class&lt;/strong&gt; that contains all the object creation logic.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;single factory method&lt;/strong&gt; that returns different objects based on some condition.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;client&lt;/strong&gt; that uses the factory instead of creating objects directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;a naive implementation of simple factory in ruby&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserSimpleFactory&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s1"&gt;'admin'&lt;/span&gt;
      &lt;span class="no"&gt;Admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;email: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s1"&gt;'customer'&lt;/span&gt;
      &lt;span class="no"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;email: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="s1"&gt;'guest'&lt;/span&gt;
      &lt;span class="no"&gt;Guest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;country: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:country&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
   &lt;span class="k"&gt;else&lt;/span&gt;
     &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="s1"&gt;'INVALID_TYPE'&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RegisterHandler&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;UserSimpleFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:type&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:user&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="s2"&gt;"Hello &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Centralizes object creation.&lt;/li&gt;
&lt;li&gt;Keeps clients from depending on concrete classes.&lt;/li&gt;
&lt;li&gt;Easy to understand and implement.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;This works well while you only have a few product types.&lt;/p&gt;

&lt;p&gt;However, every time a new user type is introduced, the factory needs another &lt;code&gt;when&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;Eventually the factory becomes a large conditional statement responsible for creating every possible object.&lt;/p&gt;

&lt;p&gt;This violates the &lt;strong&gt;Open/Closed Principle&lt;/strong&gt; because adding a new product requires modifying an existing class.&lt;/p&gt;

&lt;p&gt;at that time you might think to extract parts of this method into different classes and voilà you get a factory method pattern&lt;/p&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzd98cxqmjrdlj8i7amvl.gif" 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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzd98cxqmjrdlj8i7amvl.gif" alt="iron man viola" width="480" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Factory Method
&lt;/h2&gt;

&lt;p&gt;The Gang of Four defines Factory Method as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Define an interface for creating an object, but let subclasses decide which class to instantiate.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;this pattern lets a class defer instantiation to subclass. and encapsulates the instantation of concrete types&lt;/p&gt;

&lt;p&gt;The parent class defines the workflow, while each subclass decides which concrete object should be created.&lt;/p&gt;

&lt;p&gt;This removes the need for long &lt;code&gt;case&lt;/code&gt; statements and replaces them with &lt;strong&gt;polymorphism&lt;/strong&gt;.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Abstract creator : it gives you an interface with method(THE factory method) to create object(product), any other method on this class operates on the object returned by the factory method&lt;/li&gt;
&lt;li&gt;Concrete creator : is a subclass that implement the factory method&lt;/li&gt;
&lt;li&gt;Abstract product; interface(parent) of all products that different concrete creators return&lt;/li&gt;
&lt;li&gt;Concrete product must implement the same interface so that the factories can refere to the interface not the concrete classes &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;this pattern decouples the implementation of the product from its use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RegisterHandler&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="no"&gt;NotImplementedError&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="s2"&gt;"Hello &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AdminRegister&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;RegisterHandler&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;Admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;email: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomerRegister&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;RegisterHandler&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;email: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GuestRegister&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;RegisterHandler&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;Guest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;country: &lt;/span&gt;&lt;span class="n"&gt;user_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:country&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the &lt;code&gt;register&lt;/code&gt; method is implemented only once.&lt;/p&gt;

&lt;p&gt;The only thing subclasses customize is &lt;strong&gt;how the user is created&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The registration workflow remains unchanged regardless of the user type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Eliminates large conditional statements.&lt;/li&gt;
&lt;li&gt;Follows the Open/Closed Principle.&lt;/li&gt;
&lt;li&gt;Makes it easy to introduce new product types.&lt;/li&gt;
&lt;li&gt;Uses polymorphism instead of branching logic.&lt;/li&gt;
&lt;li&gt;Keeps creation logic isolated inside dedicated creator classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The trade-off is that it introduces more classes, which can feel unnecessary for small applications.&lt;/p&gt;




&lt;h3&gt;
  
  
  Did you know that Rails framework is full of Factory patterns? 👀
&lt;/h3&gt;

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

&lt;p&gt;Every mailer inherits from &lt;code&gt;ApplicationMailer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When you define&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserMailer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationMailer&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;welcome&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;mail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="ss"&gt;:user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The framework handles the workflow of building and delivering an email.&lt;/p&gt;

&lt;p&gt;Your subclass supplies the message-specific details.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example: Active Job adapters
&lt;/h4&gt;

&lt;p&gt;You write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SendEmailJob&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationJob&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active_job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;queue_adapter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:solid_queue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;active_job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;queue_adapter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:sidekiq&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each adapter creates its own queue implementation.&lt;/p&gt;

&lt;p&gt;Rails defines the workflow.&lt;/p&gt;

&lt;p&gt;The adapter subclasses provide the implementation&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Simple Factory&lt;/strong&gt; centralizes object creation using conditional logic. It's a pragmatic solution for smaller applications but becomes harder to maintain as the number of products grows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Factory Method&lt;/strong&gt; takes a different approach by replacing those conditionals with polymorphism. Instead of modifying one factory every time a new product is added, you create a new creator class that knows how to build that product.&lt;/p&gt;

&lt;p&gt;A good rule of thumb is this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If your factory keeps getting longer every time you introduce a new product, it's probably time to replace it with Factory Method.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;for a full example in ruby check this &lt;a href="https://github.com/ShroukAbozeid/design-pattern/tree/main/factory" rel="noopener noreferrer"&gt;https://github.com/ShroukAbozeid/design-pattern/tree/main/factory&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>designpatterns</category>
      <category>oop</category>
    </item>
    <item>
      <title>Snehal Ahire - Rails Under a Microscope: Summarized with AI</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Wed, 29 Jul 2026 13:12:23 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/snehal-ahire-rails-under-a-microscope-summarized-with-ai-4424</link>
      <guid>https://dev.to/shroukabozeid/snehal-ahire-rails-under-a-microscope-summarized-with-ai-4424</guid>
      <description>&lt;p&gt;Sometimes I'm lazy to talk notes myself(which ofcourse is better) but instead of not talking notes at all i let ai summarize what i just watched to make sure i didn't drop anything.&lt;/p&gt;

&lt;p&gt;in this video Snehal Talks about performance problems that developers can face in large scale application and what could be some of the causes.&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/Jv2N_A1OaHI"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;So here is her talk in points :D &lt;/p&gt;

&lt;h2&gt;
  
  
  Rails Performance Challenges
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Performance issues in Rails applications, such as slow listing pages, delayed search queries, and long-running Sidekiq batches, cause significant frustration for both users and developers.&lt;/li&gt;
&lt;li&gt;Persistent performance bottlenecks lead to increased infrastructure costs and delays in feature development, making performance optimization a necessity for application survival.&lt;/li&gt;
&lt;li&gt;While developers may be tempted to rewrite slow Rails applications in languages like Rust or Go, such rewrites are often risky and costly; instead, applications should be analyzed to identify specific bottlenecks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data Retrieval Optimization
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Code that appears innocent in the early stages of an application can evolve into a performance bottleneck as the system scales over many years.&lt;/li&gt;
&lt;li&gt;Common performance issues in mature applications include memory bloat, memory leaks, and inefficient database queries.&lt;/li&gt;
&lt;li&gt;Fetching unnecessary data, such as loading all columns when only a few are needed, is inefficient and becomes increasingly problematic as the number of rows grows into the millions.&lt;/li&gt;
&lt;li&gt;Developers should optimize data retrieval by selecting only the specific columns required for the task.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pagination Strategies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Standard pagination using &lt;code&gt;offset&lt;/code&gt; is suitable for small datasets but becomes inefficient for large databases because the system must scan and discard all preceding records to reach the requested page.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Keyset pagination&lt;/code&gt; is a more efficient alternative for large datasets because it fetches only the required data without scanning and discarding previous records.&lt;/li&gt;
&lt;li&gt;Keyset pagination is &lt;strong&gt;index-friendly&lt;/strong&gt;, leveraging indexed columns like ID or created_at to provide consistent performance regardless of how deep into the dataset the user navigates.&lt;/li&gt;
&lt;li&gt;Keyset pagination is an &lt;strong&gt;effective strategy for features like "Load More" or infinite scrolls&lt;/strong&gt; because it uses pointers from the last row of a previous page to start the next query, though it prevents direct navigation to specific page numbers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Query and Caching Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To prevent N+1 queries, Rails 7 and later versions offer &lt;code&gt;strict loading&lt;/code&gt; features that can be used during development to avoid accidental performance issues.&lt;/li&gt;
&lt;li&gt;Caching should only be implemented when the value of recomputing data exceeds the costs associated with cache maintenance and invalidation, as improper caching can negatively impact performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Memory Management and Heap Fragmentation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Memory bloat occurs when there is a significant discrepancy between the actual size of live objects in an application and the Resident Set Size (RSS) reported by the system.&lt;/li&gt;
&lt;li&gt;The Memory Profiler gem is a tool used to diagnose memory issues by identifying memory allocations and retained objects, categorized by gem, location, class, or specific line of code.&lt;/li&gt;
&lt;li&gt;Memory consumption in Ruby is often driven by &lt;code&gt;arrays&lt;/code&gt;, &lt;code&gt;strings&lt;/code&gt;, and &lt;code&gt;hashes&lt;/code&gt;, which are dynamic allocations stored in the heap, while static functions are managed in the stack.&lt;/li&gt;
&lt;li&gt;Ruby manages heap memory by dividing pages into multiple slots; when a new object is created, Ruby searches for a free slot within these pages.&lt;/li&gt;
&lt;li&gt;When a page is full, Ruby requests additional pages from the system allocator—such as &lt;code&gt;Malloc&lt;/code&gt; or &lt;code&gt;Jemalloc&lt;/code&gt;—rather than communicating directly with the operating system.&lt;/li&gt;
&lt;li&gt;Heap fragmentation occurs when the garbage collector frees objects, leaving non-adjacent holes in memory that are too small or poorly positioned to accommodate new object allocations.&lt;/li&gt;
&lt;li&gt;Because Ruby cannot utilize these non-adjacent free slots, it is forced to request more pages from the system allocator, leading to an increase in total memory usage despite the presence of free space.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Allocator and Garbage Collection Dynamics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Allocator fragmentation also occurs when the system allocator cannot merge non-adjacent memory blocks, a problem that can be mitigated in multi-threaded environments like Sidekiq by configuring Malloc Arena Max to two or four.&lt;/li&gt;
&lt;li&gt;Increased numbers of arenas contribute to higher levels of memory fragmentation.&lt;/li&gt;
&lt;li&gt;Locators retain memory and maintain caches of different block sizes to facilitate instant future allocations.&lt;/li&gt;
&lt;li&gt;Garbage collectors do not immediately return memory to the system, instead retaining it for future use, which keeps process memory usage high even when objects are no longer needed.&lt;/li&gt;
&lt;li&gt;Configuring the heap growth factor can help manage fragmentation, as a higher factor results in less fragmentation.&lt;/li&gt;
&lt;li&gt;Ruby 3.2 introduced variable width allocation, which manages slots through size pools rather than statically to reduce memory bloat and fragmentation, though its effectiveness depends on code support.&lt;/li&gt;
&lt;li&gt;Memory bloat is primarily caused by heap fragmentation, allocator fragmentation, allocators reserving extra memory, and the garbage collector holding onto memory.&lt;/li&gt;
&lt;li&gt;Resident Set Size (RSS) refers to the amount of RAM a specific process uses.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Memory Optimization Techniques
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Loading large datasets into an array can be optimized by using batches with configurable batch sizes instead of loading all records at once.&lt;/li&gt;
&lt;li&gt;Converting large datasets to JSON within Ruby consumes significant memory; offloading this process to the database using functions like &lt;code&gt;row_to_json&lt;/code&gt; or &lt;code&gt;json_agg&lt;/code&gt; is more efficient.&lt;/li&gt;
&lt;li&gt;Reading large CSV files should be done line-by-line or using &lt;code&gt;foreach&lt;/code&gt; rather than loading millions of rows into memory at once.&lt;/li&gt;
&lt;li&gt;Creating temporary objects inside loops, such as date objects, leads to excessive memory usage over millions of iterations; these objects should be created once before the loop and reused, ideally using &lt;code&gt;freeze&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;include?&lt;/code&gt; on arrays inside loops creates new arrays in every iteration, which can be avoided by using a &lt;code&gt;Set&lt;/code&gt; for faster retrieval and freezing the collection.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bulk Operations and Database Efficiency
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;destroy&lt;/code&gt; method loads all associations into memory, whereas &lt;code&gt;delete_all&lt;/code&gt; performs a single operation without callbacks or associations, though it requires manual handling of child records to avoid orphaned objects.&lt;/li&gt;
&lt;li&gt;Per-record updates can trigger N+1 query issues and high memory usage; bulk operations like &lt;code&gt;upsert&lt;/code&gt;, &lt;code&gt;update_all&lt;/code&gt;, or &lt;code&gt;upsert_all&lt;/code&gt; are more efficient alternatives.&lt;/li&gt;
&lt;li&gt;Inserting rows one by one for associations, such as orders and line items, creates memory overhead when processing multiple rows.&lt;/li&gt;
&lt;li&gt;Bulk queries can be optimized by using &lt;code&gt;insert&lt;/code&gt; or &lt;code&gt;upsert&lt;/code&gt; operations instead of individual queries.&lt;/li&gt;
&lt;li&gt;Memory bloat issues can be addressed by utilizing batches, reusing data, and freezing objects to prevent redundant object creation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Identifying and Resolving Memory Leaks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A gradual, steady increase in memory usage over time often indicates a memory leak, even if restarting the process temporarily resolves the symptoms.&lt;/li&gt;
&lt;li&gt;Symptoms of a memory leak include a steadily growing Resident Set Size (RSS) that does not decrease after Sidekiq jobs, a heap of objects that are never deallocated, and frequent garbage collection (GC) pauses as the system struggles to clean up unreleased objects.&lt;/li&gt;
&lt;li&gt;Unbounded in-memory objects, such as those stored in class or global variables, can cause memory leaks because the garbage collector cannot release them as long as they remain referenced.&lt;/li&gt;
&lt;li&gt;Replacing class or global variables with local methods allows the garbage collector to automatically free memory after the data is used.&lt;/li&gt;
&lt;li&gt;Closures can lead to memory leaks if they capture large objects rather than specific values, as the captured variables are not released.&lt;/li&gt;
&lt;li&gt;Passing only the specific data required, such as a calculated size rather than an entire array, prevents unnecessary memory retention.&lt;/li&gt;
&lt;li&gt;Implementing these memory optimizations resulted in a 42% reduction in memory usage (from 788 MB to 459 MB) and an 80% reduction in the number of objects (from 11 million to 2 million), while performance time improved from 2 minutes to between 10 and 20 seconds.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Database Query Tuning and Indexing
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Database query performance can be analyzed and tuned using the &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; command in PostgreSQL.&lt;/li&gt;
&lt;li&gt;Adding specific conditions to a query can shift the execution plan from a slow parallel sequential scan to a significantly faster index scan, as demonstrated by a reduction in execution time from 2,800 milliseconds to 0.7 milliseconds.&lt;/li&gt;
&lt;li&gt;Partial indexes are effective only when queries include the specific index conditions, and neglecting these details can lead to high execution times.&lt;/li&gt;
&lt;li&gt;Small, neglected features or individual application problems can cause significant performance degradation across an entire system.&lt;/li&gt;
&lt;li&gt;Materialized views in Postgres serve as physical tables that store computed data, offering an alternative to complex joins across multiple tables or the complications of using read replicas.&lt;/li&gt;
&lt;li&gt;Postgres features such as TS vectors combined with GIN indexes allow for efficient searching across multiple columns by creating a vector for direct querying.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Scaling and Infrastructure Management
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sidekiq can be autoscaled using latency-based queuing, which organizes tasks into multiple queues based on their latency requirements.&lt;/li&gt;
&lt;li&gt;Specialized GIN indexes, which support various operators, can be utilized to achieve faster retrieval speeds for JSON columns.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Performance Culture and Workflow Integration
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Performance optimization should begin with **profiling and measurement **rather than relying on intuition to identify bottlenecks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database capabilities&lt;/strong&gt;, including indexes, aggregations, and bulk operations, should be leveraged for heavy tasks rather than attempting to reinvent these processes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identifying data patterns&lt;/strong&gt; and understanding how data is derived and processed is essential, as even minor inefficiencies accumulate at scale.&lt;/li&gt;
&lt;li&gt;Performance management is a collaborative responsibility that should be shared across the entire team.&lt;/li&gt;
&lt;li&gt;P*&lt;em&gt;erformance should be integrated into the development workflow&lt;/em&gt;* by setting budgets and monitoring metrics throughout the design, build, and shipping phases, rather than treating it as an afterthought.&lt;/li&gt;
&lt;li&gt;Addressing performance issues requires a &lt;strong&gt;continuous, multi-faceted approach&lt;/strong&gt; rather than a one-time fix or a complete rewrite of the application in a different language.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thanks Snehal for an interesting talk and a great presentation, if you know her profile tag her for credits &lt;/p&gt;

</description>
      <category>rails</category>
      <category>performance</category>
    </item>
    <item>
      <title>Learning Go as a Ruby Developer #3: Functions, Methods, and Why Go Doesn't Have Classes</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Mon, 27 Jul 2026 14:59:31 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-3-functions-methods-and-why-go-doesnt-have-classes-pc7</link>
      <guid>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-3-functions-methods-and-why-go-doesnt-have-classes-pc7</guid>
      <description>&lt;p&gt;One of the biggest mindset shifts I've had while learning Go came when I reached functions and methods.&lt;/p&gt;

&lt;p&gt;As a Ruby developer, almost everything revolves around objects. Every piece of behavior belongs to a class, and every function is really just a method attached to an object.&lt;/p&gt;

&lt;p&gt;Go takes a different approach.&lt;/p&gt;

&lt;p&gt;Functions exist independently, and methods are simply functions with a receiver. There are no classes, no inheritance, but still writing object-oriented code.&lt;/p&gt;

&lt;p&gt;Let's see how Go approaches behavior compared to Ruby.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Felt Strange Coming from Ruby
&lt;/h2&gt;

&lt;p&gt;In Ruby, we rarely think about standalone functions because they don't really exist.&lt;/p&gt;

&lt;p&gt;Everything is an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Calculator&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;calculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Want behavior?&lt;/p&gt;

&lt;p&gt;Create a class.&lt;/p&gt;

&lt;p&gt;Need reusable logic?&lt;/p&gt;

&lt;p&gt;Another class or a module.&lt;/p&gt;

&lt;p&gt;Go starts from a much simpler place.&lt;/p&gt;

&lt;p&gt;Most behavior begins as just... a function.&lt;/p&gt;




&lt;h2&gt;
  
  
  Functions in Go
&lt;/h2&gt;

&lt;p&gt;Functions are declared using the &lt;code&gt;func&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax may look unusual at first, especially if you've spent years writing Ruby.&lt;/p&gt;

&lt;p&gt;Instead of writing the return type first, Go places it &lt;strong&gt;after&lt;/strong&gt; the parameter list.&lt;/p&gt;

&lt;p&gt;Calling the function is exactly what you'd expect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Grouping Parameters
&lt;/h2&gt;

&lt;p&gt;Go lets you group parameters that share the same type.&lt;/p&gt;

&lt;p&gt;Instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a small feature, but it keeps function declarations much cleaner.&lt;/p&gt;




&lt;h2&gt;
  
  
  Returning Multiple Values
&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;func&lt;/span&gt; &lt;span class="n"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you can capture both results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;quotient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Coming from Ruby, this felt familiar because Ruby also supports multiple assignment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;quotient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;remainder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;divide&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;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference is that in Ruby we're really returning an array, while in Go multiple return values are part of the language itself.&lt;/p&gt;




&lt;h2&gt;
  
  
  Named Return Values
&lt;/h2&gt;

&lt;p&gt;One feature I wasn't expecting was named return values.&lt;/p&gt;

&lt;p&gt;Instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the bare &lt;code&gt;return&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;result&lt;/code&gt; was named in the function signature, Go automatically returns it.&lt;/p&gt;

&lt;p&gt;This is called a &lt;strong&gt;naked return&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Anonymous Functions
&lt;/h2&gt;

&lt;p&gt;Go also supports anonymous functions.&lt;/p&gt;

&lt;p&gt;You can assign them to variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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="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;"Hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or execute them immediately.&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="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;"Running immediately"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This reminded me a lot of Ruby blocks and lambdas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although the syntax is different, the concept feels familiar.&lt;/p&gt;




&lt;h2&gt;
  
  
  Variadic Functions
&lt;/h2&gt;

&lt;p&gt;Sometimes you don't know how many arguments a function should accept.&lt;/p&gt;

&lt;p&gt;Go solves this using variadic parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;printNumbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;...&lt;/code&gt; tells Go to collect all arguments into a slice.&lt;/p&gt;

&lt;p&gt;Ruby has something very similar.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was one of the easiest concepts for me to understand because the two languages approach it almost the same way.&lt;/p&gt;




&lt;h2&gt;
  
  
  So Where Are the Classes?
&lt;/h2&gt;

&lt;p&gt;This was the question I kept asking while reading through the documentation.&lt;/p&gt;

&lt;p&gt;If Go doesn't have classes...&lt;/p&gt;

&lt;p&gt;Where do methods live?&lt;/p&gt;

&lt;p&gt;The answer surprised me.&lt;/p&gt;

&lt;p&gt;Methods belong to &lt;strong&gt;types&lt;/strong&gt;, not classes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Methods in Go
&lt;/h2&gt;

&lt;p&gt;A method is simply a function with a receiver.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;SayHello&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;"Hello,"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can call it like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax felt unusual at first.&lt;/p&gt;

&lt;p&gt;Instead of defining the method inside the type like Ruby does, the receiver appears before the method name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;MethodName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Value Receivers
&lt;/h2&gt;

&lt;p&gt;By default, methods receive a copy of the value.&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;p&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Changing &lt;code&gt;p.Name&lt;/code&gt; only changes the copy.&lt;/p&gt;

&lt;p&gt;The original object stays exactly the same.&lt;/p&gt;

&lt;p&gt;This is called a &lt;strong&gt;value receiver&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pointer Receivers
&lt;/h2&gt;

&lt;p&gt;If you want to modify the original value, you use a pointer receiver.&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;p&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now changes affect the original struct.&lt;/p&gt;

&lt;p&gt;This distinction between value and pointer receivers was one of the first places where Go reminded me of C++.&lt;/p&gt;

&lt;p&gt;Ruby developers usually don't think about this because objects are already passed around by reference-like semantics.&lt;/p&gt;

&lt;p&gt;Go asks you to decide whether you're working with a copy or the original value.&lt;/p&gt;




&lt;h2&gt;
  
  
  Side-by-Side Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;th&gt;Ruby&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Standalone functions&lt;/td&gt;
&lt;td&gt;Everything is a method&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;func&lt;/code&gt; keyword&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;def&lt;/code&gt; keyword&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Multiple return values&lt;/td&gt;
&lt;td&gt;Usually arrays or multiple assignment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anonymous functions&lt;/td&gt;
&lt;td&gt;Lambdas and Procs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Variadic parameters (&lt;code&gt;...&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Splat operator (&lt;code&gt;*&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Methods attached to types&lt;/td&gt;
&lt;td&gt;Methods defined inside classes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Value receivers&lt;/td&gt;
&lt;td&gt;Objects usually mutated directly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pointer receivers&lt;/td&gt;
&lt;td&gt;Reference-like object semantics&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Thoughts
&lt;/h2&gt;

&lt;p&gt;Ruby's object model is incredibly elegant.&lt;/p&gt;

&lt;p&gt;Everything feels consistent because everything is an object.&lt;/p&gt;

&lt;p&gt;Go takes a more lightweight approach.&lt;/p&gt;

&lt;p&gt;Instead of encouraging you to wrap every behavior inside a class, it asks a simple question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does this really need to belong to a type?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sometimes the answer is yes—a method makes sense.&lt;/p&gt;

&lt;p&gt;Other times, a standalone function is perfectly fine.&lt;/p&gt;

&lt;p&gt;Go is trying to remove the parts it considers unnecessary while keeping the parts that are genuinely useful.&lt;/p&gt;

&lt;p&gt;This has been one of the most interesting lessons so far in my Go journey, what about you?&lt;/p&gt;

</description>
      <category>go</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Learning Go as a Ruby Developer # 2: Variables, Constants, and Why Go Doesn't Trust You</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Sat, 25 Jul 2026 14:20:50 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-2-variables-constants-and-why-go-doesnt-trust-you-49ji</link>
      <guid>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-2-variables-constants-and-why-go-doesnt-trust-you-49ji</guid>
      <description>&lt;p&gt;One of the first differences I noticed when learning Go was how seriously it takes types.&lt;/p&gt;

&lt;p&gt;Coming from Ruby. I rarely think about their types because Ruby figures everything out at runtime.&lt;/p&gt;

&lt;p&gt;Go has a very different philosophy.&lt;/p&gt;

&lt;p&gt;It wants to know exactly what every variable is, and if it doesn't know, it'll ask you to be explicit.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Felt Strange Coming from Ruby
&lt;/h2&gt;

&lt;p&gt;In Ruby, creating variables couldn't be easier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Alice"&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="n"&gt;admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ruby decides the type at runtime.&lt;/p&gt;

&lt;p&gt;Later I can even do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"hello"&lt;/span&gt;
&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same variable can hold completely different kinds of objects throughout the program.&lt;/p&gt;

&lt;p&gt;Go doesn't allow that.&lt;/p&gt;

&lt;p&gt;Once a variable has a type, that type is fixed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Declaring Variables in Go
&lt;/h2&gt;

&lt;p&gt;The most explicit way to declare a variable is by specifying its type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is checked during compilation, long before your program ever runs.&lt;/p&gt;

&lt;p&gt;If you later try to assign a different type, the compiler will stop you immediately.&lt;/p&gt;

&lt;p&gt;This felt a bit strict—but it also means many bugs are caught before the program starts.&lt;/p&gt;




&lt;h2&gt;
  
  
  Type Inference
&lt;/h2&gt;

&lt;p&gt;Fortunately, Go doesn't always require you to write the type yourself.&lt;/p&gt;

&lt;p&gt;It can often infer it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go knows this is a &lt;code&gt;bool&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;becomes a &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Short Variable Declaration
&lt;/h2&gt;

&lt;p&gt;Inside functions, Go provides an even shorter syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;
&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;:=&lt;/code&gt; operator both declares the variable and lets Go infer its type.&lt;/p&gt;

&lt;p&gt;If you're writing local variables, you'll probably use this style most of the time.&lt;/p&gt;

&lt;p&gt;One thing I learned from my notes is that this syntax &lt;strong&gt;can only be used inside functions&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Declaring Multiple Variables
&lt;/h2&gt;

&lt;p&gt;Go also makes it easy to declare multiple variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or with inferred types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For larger groups, Go encourages this style:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;host&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"localhost"&lt;/span&gt;
    &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8080&lt;/span&gt;
    &lt;span class="n"&gt;debug&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Constants
&lt;/h2&gt;

&lt;p&gt;Constants are declared using &lt;code&gt;const&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;MaxUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also group them.&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;const&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;Enabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;Pi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;3.14&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike variables, constants cannot be changed after they're declared.&lt;/p&gt;

&lt;p&gt;Ruby has constants too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;MAX_USERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But every Ruby developer knows the truth...&lt;/p&gt;

&lt;p&gt;Ruby will happily let you change it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;MAX_USERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll get a warning, not an error.&lt;/p&gt;

&lt;p&gt;Go, on the other hand, simply refuses.&lt;/p&gt;




&lt;h2&gt;
  
  
  Zero Values
&lt;/h2&gt;

&lt;p&gt;If you declare a variable without assigning anything, Go automatically initializes it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The values become:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Zero Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;float64&lt;/td&gt;
&lt;td&gt;0.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bool&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;string&lt;/td&gt;
&lt;td&gt;""&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Coming from Ruby, I expected something similar to &lt;code&gt;nil&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead, Go gives every type a sensible default.&lt;/p&gt;

&lt;p&gt;That design avoids a lot of unnecessary initialization code.&lt;/p&gt;




&lt;h2&gt;
  
  
  Type Conversion
&lt;/h2&gt;

&lt;p&gt;Another thing that immediately stood out was that Go &lt;strong&gt;doesn't automatically convert between types&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Suppose we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;45&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Converting it to a floating-point number requires an explicit conversion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="kt"&gt;uint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;uint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every conversion must be intentional.&lt;/p&gt;

&lt;p&gt;Ruby is much more forgiving.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"45"&lt;/span&gt;

&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;
&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_f&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in many cases Ruby performs conversions automatically for you.&lt;/p&gt;

&lt;p&gt;Go deliberately avoids this.&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating Values with &lt;code&gt;make&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;One line in my notes that initially confused me was this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike arrays in many languages, &lt;code&gt;make&lt;/code&gt; creates and initializes certain built-in data structures.&lt;/p&gt;

&lt;p&gt;In this example, it creates a slice with a length of three.&lt;/p&gt;

&lt;p&gt;I'll dive much deeper into slices later in the series, but for now it's enough to know that &lt;code&gt;make&lt;/code&gt; is commonly used when creating slices, maps, and channels.&lt;/p&gt;




&lt;h2&gt;
  
  
  So to conclude:
&lt;/h2&gt;

&lt;p&gt;Changing a type?&lt;/p&gt;

&lt;p&gt;Be explicit.&lt;/p&gt;

&lt;p&gt;Creating a variable?&lt;/p&gt;

&lt;p&gt;Be explicit—or let the compiler infer it.&lt;/p&gt;

&lt;p&gt;Converting numbers?&lt;/p&gt;

&lt;p&gt;Be explicit.&lt;/p&gt;

&lt;p&gt;At first it felt like the language didn't trust me :D&lt;/p&gt;

&lt;p&gt;Now I think it's more accurate to say Go doesn't trust assumptions.&lt;/p&gt;

&lt;p&gt;Instead of guessing what I meant, it asks me to say exactly what I want.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby optimizes for flexibility.&lt;br&gt;
Go optimizes for correctness.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>ruby</category>
      <category>rails</category>
      <category>programming</category>
    </item>
    <item>
      <title>Learning Go as a Ruby Developer # 1: How Go Programs Actually Run</title>
      <dc:creator>Shrouk Abozeid</dc:creator>
      <pubDate>Tue, 14 Jul 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-part-1-understanding-how-go-programs-actually-run-3eh0</link>
      <guid>https://dev.to/shroukabozeid/learning-go-as-a-ruby-developer-part-1-understanding-how-go-programs-actually-run-3eh0</guid>
      <description>&lt;p&gt;Recently I started learning Go, and one of the first things I noticed was how much it reminded me of C++.&lt;br&gt;
I haven't touched C++ since college, so Go gives me a weird sense of nostalgia :D&lt;/p&gt;

&lt;p&gt;As a Ruby developer, I'm used to writing code and simply executing it.&lt;br&gt;
Go, however, has a much stronger notion of what a program actually is and how it starts running.&lt;/p&gt;

&lt;p&gt;In this article, I want to explore how a Go program is structured and compare it with how we typically think about programs in Ruby&lt;/p&gt;
&lt;h2&gt;
  
  
  Why This Felt Strange Coming from Ruby
&lt;/h2&gt;

&lt;p&gt;In Ruby, writing a program is straightforward:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;puts "Hello World"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Save it as &lt;em&gt;hello.rb&lt;/em&gt; and run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ruby hello.rb&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There's no special package declaration, no imports required for basic programs, and no designated entry point function.&lt;/p&gt;

&lt;p&gt;Go takes a very different approach.&lt;/p&gt;

&lt;p&gt;Every executable Go program follows a specific structure, and understanding this structure is one of the first steps to becoming comfortable with the language.&lt;/p&gt;
&lt;h2&gt;
  
  
  How Go Programs Are Structured
&lt;/h2&gt;

&lt;p&gt;A typical Go file looks like this:&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;"fmt"&lt;/span&gt; 
&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;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;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are three important pieces here:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Package Declaration
&lt;/h3&gt;

&lt;p&gt;Every Go file begins with a package declaration:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;package main&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Packages are Go's mechanism for organizing code.&lt;/p&gt;

&lt;p&gt;If you want to build an executable application, the package must be named main.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Imports
&lt;/h3&gt;

&lt;p&gt;Go requires explicit imports:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler will actually complain if you import something you don't use.&lt;/p&gt;

&lt;p&gt;As a Ruby developer, this felt unusual because Ruby happily allows unused &lt;br&gt;
requires:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'json'&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'csv'&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go prefers keeping dependencies explicit and clean.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The main Function
&lt;/h3&gt;

&lt;p&gt;Every executable Go program starts execution in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;main&lt;/code&gt;function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;accepts no arguments&lt;/li&gt;
&lt;li&gt;returns no values&lt;/li&gt;
&lt;li&gt;acts as the application's entry point&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When &lt;code&gt;main()&lt;/code&gt; finishes, the program exits.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Program Execution Works
&lt;/h2&gt;

&lt;p&gt;According to the Go specification, a complete program consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one package named main&lt;/li&gt;
&lt;li&gt;all packages imported by main&lt;/li&gt;
&lt;li&gt;all transitive dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Program execution happens in two stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize packages&lt;/li&gt;
&lt;li&gt;Execute main()&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once &lt;code&gt;main()&lt;/code&gt;returns, the program terminates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running vs Building Programs
&lt;/h2&gt;

&lt;p&gt;Go provides two common ways to execute code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Run directly
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;go run hello.go&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This compiles and runs the program immediately.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build an executable
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;go build hello.go
./hello
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a standalone binary.&lt;/p&gt;

&lt;p&gt;In Ruby: applications are distributed as source code and interpreted at runtime.&lt;/p&gt;

&lt;p&gt;Coming from Rails, where so much setup happens automatically, Go feels so explicit.&lt;/p&gt;

&lt;p&gt;At the same time, that explicitness means you have to think about things that Ruby usually hides from you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;while Ruby optimizes for developer happiness and speed of iteration, Go forces me to understand what's actually happening.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What about you? do you use Ruby or Go, or both ? and how was the transition for you ?&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>go</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
