<?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: Adetola Esther Aderibigbe</title>
    <description>The latest articles on DEV Community by Adetola Esther Aderibigbe (@adetolaesther).</description>
    <link>https://dev.to/adetolaesther</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%2F2585419%2F4c44abd9-7224-4676-b61b-129787412327.png</url>
      <title>DEV Community: Adetola Esther Aderibigbe</title>
      <link>https://dev.to/adetolaesther</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adetolaesther"/>
    <language>en</language>
    <item>
      <title>Data Structures in Go[Maps]</title>
      <dc:creator>Adetola Esther Aderibigbe</dc:creator>
      <pubDate>Fri, 06 Nov 2026 17:20:25 +0000</pubDate>
      <link>https://dev.to/adetolaesther/data-structures-in-gomaps-1h0k</link>
      <guid>https://dev.to/adetolaesther/data-structures-in-gomaps-1h0k</guid>
      <description>&lt;h2&gt;
  
  
  Introduction to Maps
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;map&lt;/strong&gt; is a built in Go data structure that stores information as &lt;strong&gt;key value pairs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unlike arrays and slices, where elements are accessed using their positions (indexes), values in a map are accessed using their keys.&lt;/p&gt;

&lt;p&gt;Every entry in a Go map consists of two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Key:&lt;/strong&gt; An identifier used to find an entry in the map.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Value:&lt;/strong&gt; The information associated with that key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, imagine storing information about a user. We could use the user's name and email as key value pairs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"Adetola"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"email"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"adetola@yopmail.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  syntax
&lt;/h3&gt;

&lt;p&gt;The general syntax for declaring a map 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;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;keyType&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;valueType&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our 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;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt; tells Go that we are declaring a map.&lt;/li&gt;
&lt;li&gt;The first &lt;code&gt;string&lt;/code&gt; specifies the data type of the keys.&lt;/li&gt;
&lt;li&gt;The second &lt;code&gt;string&lt;/code&gt; specifies the data type of the values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means we have a map where both the keys and values are strings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can keys and values have different data types?
&lt;/h3&gt;

&lt;p&gt;Yes. The key and value types do not have to be the same.&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;ages&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Adetola"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Tobi"&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;Here, the keys are strings, while the values are integers.&lt;/p&gt;

&lt;p&gt;Other possible combinations include:&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;map&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;string&lt;/span&gt;
&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;float64&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, all keys in a particular map must have the same type, and all values must have the same type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key takeaway:&lt;/strong&gt; A Go map stores data in key-value pairs. Unlike arrays and slices, we use keys rather than indexes to access its values. The key and value types can be different, but each must remain consistent throughout the map.&lt;/p&gt;

&lt;h2&gt;
  
  
  Declaring and Initialising Maps
&lt;/h2&gt;

&lt;p&gt;There are several ways to create a map in Go.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Using a map literal
&lt;/h3&gt;

&lt;p&gt;A map literal is useful when you already know the entries you want to store. The map is created and populated with values 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="n"&gt;userAges&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Tola"&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;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;userAges&lt;/code&gt; is the variable name.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map[string]int&lt;/code&gt; declares a map with string keys and integer values.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"Ade"&lt;/code&gt; and &lt;code&gt;"Tola"&lt;/code&gt; are the keys.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;25&lt;/code&gt; and &lt;code&gt;30&lt;/code&gt; are their corresponding values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A map literal allows us to create and populate a map in one step.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Using make()
&lt;/h3&gt;

&lt;p&gt;The built-in &lt;code&gt;make()&lt;/code&gt; function is used when you want to create an empty map and add entries later.&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;userAges&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="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="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;This creates an initialized, empty map that is ready to receive entries.&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;userAges&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="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;
&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Tola"&lt;/span&gt;&lt;span class="p"&gt;]&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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;map[Ade:25 Tola:30]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also provide an optional initial capacity hint:&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;userAges&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="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells Go that you expect the map to hold approximately 10 entries. It does not limit the map to 10 entries.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Declaring a map without initialising it
&lt;/h3&gt;

&lt;p&gt;You can declare a map without initialising 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;userAges&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a &lt;strong&gt;nil map&lt;/strong&gt;. A nil map can be read from, and its length can be checked, but you cannot add entries to 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;userAges&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&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;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c"&gt;// 0&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;   &lt;span class="c"&gt;// 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, attempting to add an entry causes a runtime panic.&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;userAges&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;

&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; &lt;span class="c"&gt;// Runtime panic&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Because the map has been declared but not initialised. It does not have an underlying map structure ready to store entries. To add entries, initialise it first using either a map literal or &lt;code&gt;make()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; &lt;code&gt;var userAges map[string]int&lt;/code&gt; creates a nil map, while &lt;code&gt;make(map[string]int)&lt;/code&gt; creates an initialized, empty map.&lt;/p&gt;

&lt;p&gt;Both initially have zero entries, but only the initialized map can accept new entries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding and Updating Values in a Map
&lt;/h2&gt;

&lt;p&gt;Go uses the same syntax to add a new entry and update an existing entry.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;mapName&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1. Adding a new entry
&lt;/h3&gt;

&lt;p&gt;When the key does not already exist, Go creates a new entry.&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;userAges&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="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;
&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Tola"&lt;/span&gt;&lt;span class="p"&gt;]&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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;map[Ade:25 Tola:30]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first assignment creates an entry with the key &lt;code&gt;"Ade"&lt;/code&gt; and value &lt;code&gt;25&lt;/code&gt; and the second assignment creates another entry with the key &lt;code&gt;"Tola"&lt;/code&gt; and value &lt;code&gt;30&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Updating an existing entry
&lt;/h3&gt;

&lt;p&gt;When the key already exists, assigning a new value updates the existing entry.&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;userAges&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Tola"&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;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;26&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;userAges&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;map[Ade:26 Tola:30]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;"Ade"&lt;/code&gt; originally had a value of &lt;code&gt;25&lt;/code&gt;. After the assignment, its value becomes &lt;code&gt;26&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Go does not create another &lt;code&gt;"Ade"&lt;/code&gt; entry. It updates the existing one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key takeaway:&lt;/strong&gt; The assignment syntax is the same for adding and updating. Go checks whether the key already exists and either creates a new entry or replaces the existing value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing Values in a Map
&lt;/h2&gt;

&lt;p&gt;To retrieve a value from a map, you use its key. 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="n"&gt;mapName&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;userAges&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Tola"&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;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;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Go looks for the key &lt;code&gt;"Ade"&lt;/code&gt; and returns the value associated with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens when a key does not exist?
&lt;/h3&gt;

&lt;p&gt;If you try to access a key that does not exist, Go returns the zero value of the map's value type.&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;userAges&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Tola"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Because the value type is &lt;code&gt;int&lt;/code&gt;, Go returns the integer zero.&lt;/p&gt;

&lt;p&gt;Other examples:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Map type&lt;/th&gt;
&lt;th&gt;Zero value for a missing key&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;map[string]int&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;map[string]string&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;&lt;code&gt;map[string]bool&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;map[string]float64&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0.0&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;However, this creates a problem like what if a key does not exist, but another key exists with an actual value of &lt;code&gt;0&lt;/code&gt;?, How do we distinguish between the two?&lt;/p&gt;

&lt;p&gt;Go solves this using the &lt;strong&gt;comma-ok idiom&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Checking Whether a Key Exists (Comma-OK Idiom)
&lt;/h2&gt;

&lt;p&gt;Go allows you to retrieve a value and check whether its key exists at the same time.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;userAges&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Tola"&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;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exists&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Ade"&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;exists&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;"Age:"&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"User not found"&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Age: 25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break this down:&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exists&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go returns two values:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;age&lt;/code&gt;: The value associated with the key.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;exists&lt;/code&gt;: A boolean indicating whether the key exists in the map.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the key exists, &lt;code&gt;exists&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If the key does not exist, &lt;code&gt;exists&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's look at another 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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exists&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Femi"&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;exists&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;"Age:"&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"User not found"&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User not found
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though Go returns the zero value for a missing key, the &lt;code&gt;exists&lt;/code&gt; variable tells us that the key was not found.&lt;/p&gt;

&lt;p&gt;This is particularly useful when working with user records, configuration, permissions, and other data where knowing whether an entry exists matters.&lt;/p&gt;

&lt;p&gt;It is called the &lt;strong&gt;comma-ok idiom&lt;/strong&gt; because of the two-value assignment used to check the result.&lt;/p&gt;




&lt;h2&gt;
  
  
  Deleting Entries from a Map
&lt;/h2&gt;

&lt;p&gt;Go has a built-in function called &lt;code&gt;delete()&lt;/code&gt; for removing entries from a map.&lt;/p&gt;

&lt;p&gt;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="nb"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mapName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;userAges&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Tola"&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="s"&gt;"Femi"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Tola"&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;userAges&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Ade&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="m"&gt;25&lt;/span&gt; &lt;span class="n"&gt;Femi&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="m"&gt;28&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;delete()&lt;/code&gt; function removes the entry associated with the specified key.&lt;/p&gt;

&lt;p&gt;A few things to remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deleting an existing key removes its entry.&lt;/li&gt;
&lt;li&gt;Deleting a key that does not exist does nothing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;delete()&lt;/code&gt; does not return a value.&lt;/li&gt;
&lt;li&gt;Calling &lt;code&gt;delete()&lt;/code&gt; on a nil map is safe and does nothing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting the Number of Entries in a Map
&lt;/h2&gt;

&lt;p&gt;You can use the built-in &lt;code&gt;len()&lt;/code&gt; function to find out how many entries a map contains.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;userAges&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;26&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Tola"&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="s"&gt;"Femi"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;28&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;len()&lt;/code&gt; function returns the number of key-value pairs currently stored in the map.&lt;/p&gt;

&lt;p&gt;Unlike an array, a map does not have a fixed number of entries. Its length changes as you add or delete entries.&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;userAges&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="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// 0&lt;/span&gt;

&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;26&lt;/span&gt;
&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Tola"&lt;/span&gt;&lt;span class="p"&gt;]&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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// 2&lt;/span&gt;

&lt;span class="nb"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Ade"&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Iterating Over a Map
&lt;/h2&gt;

&lt;p&gt;Sometimes you don't want to retrieve just one value. You want to go through every entry in a map.&lt;/p&gt;

&lt;p&gt;Go uses the &lt;code&gt;range&lt;/code&gt; keyword to iterate over maps.&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;userAges&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Ade"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Tola"&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="s"&gt;"Femi"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&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;name&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="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;userAges&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="n"&gt;age&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;Possible output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ade 25
Tola 30
Femi 28
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output order may be different when you run the program.&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;name&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="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;userAges&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;for&lt;/code&gt; starts the loop.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;range&lt;/code&gt; allows us to iterate over the map.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;userAges&lt;/code&gt; is the map we want to iterate through.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; receives the key from each entry.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;age&lt;/code&gt; receives the corresponding value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For every entry in the map, Go assigns its key to &lt;code&gt;name&lt;/code&gt; and its value to &lt;code&gt;age&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The loop then executes the code inside its body.&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;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;This prints the key and its corresponding value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Important: Map iteration order
&lt;/h3&gt;

&lt;p&gt;Go does not guarantee the order in which map entries are returned during iteration. This means you should not rely on a map to preserve the order in which entries were inserted.&lt;br&gt;
 If you need a specific order, you must collect the keys and sort them before iterating.&lt;/p&gt;
&lt;h2&gt;
  
  
  Important Characteristics of Go Maps
&lt;/h2&gt;
&lt;h3&gt;
  
  
  A. Map keys must be comparable
&lt;/h3&gt;

&lt;p&gt;Go requires map keys to be types that can be compared using the &lt;code&gt;==&lt;/code&gt; operator.&lt;/p&gt;

&lt;p&gt;Common valid key types include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strings&lt;/li&gt;
&lt;li&gt;Integers&lt;/li&gt;
&lt;li&gt;Floating-point numbers&lt;/li&gt;
&lt;li&gt;Booleans&lt;/li&gt;
&lt;li&gt;Pointers&lt;/li&gt;
&lt;li&gt;Arrays whose element types are comparable&lt;/li&gt;
&lt;li&gt;Structs whose fields are all comparable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Slices, maps, and functions cannot be used directly as map keys.&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="c"&gt;// Valid&lt;/span&gt;
&lt;span class="n"&gt;ages&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="c"&gt;// Invalid&lt;/span&gt;
&lt;span class="c"&gt;// users := map[[]int]string{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why? Because slices cannot be compared using &lt;code&gt;==&lt;/code&gt; in Go.&lt;/p&gt;

&lt;p&gt;This restriction exists because Go needs to be able to compare keys when storing and retrieving map entries.&lt;/p&gt;

&lt;h3&gt;
  
  
  B. Maps are reference like types
&lt;/h3&gt;

&lt;p&gt;When you assign a map to another variable, Go does not create an independent copy of all its entries.&lt;/p&gt;

&lt;p&gt;Both variables refer to the same underlying map data.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Adetola"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;

&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Adetola"&lt;/span&gt;&lt;span class="p"&gt;]&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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Adetola"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c"&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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Adetola"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c"&gt;// 30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that changing the value through &lt;code&gt;second&lt;/code&gt; also affects what we see through &lt;code&gt;first&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is different from arrays, where assigning one array to another copies the entire array.&lt;/p&gt;

&lt;h3&gt;
  
  
  C. A map is not safe for concurrent writes by default
&lt;/h3&gt;

&lt;p&gt;If multiple goroutines access a map concurrently and at least one of them writes to it, you need synchronization.&lt;/p&gt;

&lt;p&gt;Ordinary Go maps are not automatically safe for concurrent writes.&lt;/p&gt;

&lt;p&gt;We'll explore goroutines, concurrency, and synchronization later in your Go learning journey.&lt;/p&gt;

&lt;p&gt;For now, remember that when multiple goroutines need to modify the same map, you must protect those operations appropriately.&lt;/p&gt;




&lt;h2&gt;
  
  
  Go Maps vs JavaScript Objects
&lt;/h2&gt;

&lt;p&gt;Since you already know JavaScript, let's connect the concepts. In JavaScript, you might represent the same information using an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userAges&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;Adetola&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;Tobi&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;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userAges&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Adetola&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Go:&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;userAges&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"Adetola"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Tobi"&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;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;userAges&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Adetola"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They can serve similar purposes, but they are not exactly the same.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Go map&lt;/th&gt;
&lt;th&gt;JavaScript object&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Structure&lt;/td&gt;
&lt;td&gt;Key-value pairs&lt;/td&gt;
&lt;td&gt;Properties and values&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Key types&lt;/td&gt;
&lt;td&gt;One declared comparable type&lt;/td&gt;
&lt;td&gt;Strings or symbols&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Value types&lt;/td&gt;
&lt;td&gt;One declared value type&lt;/td&gt;
&lt;td&gt;Can hold different types&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Access&lt;/td&gt;
&lt;td&gt;&lt;code&gt;users["Adetola"]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;users.Adetola&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Add/update&lt;/td&gt;
&lt;td&gt;&lt;code&gt;users["name"] = value&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;users.name = value&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delete&lt;/td&gt;
&lt;td&gt;&lt;code&gt;delete(users, "name")&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;delete users.name&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Missing key&lt;/td&gt;
&lt;td&gt;Returns zero value; comma-ok checks existence&lt;/td&gt;
&lt;td&gt;Returns &lt;code&gt;undefined&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Iteration&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for range&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;for...in&lt;/code&gt;, &lt;code&gt;Object.keys()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  JavaScript also has a built-in Map
&lt;/h3&gt;

&lt;p&gt;JavaScript's &lt;code&gt;Map&lt;/code&gt; object is another useful comparison to Go maps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;ages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Adetola&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;ages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tobi&lt;/span&gt;&lt;span class="dl"&gt;"&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;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Adetola&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript's &lt;code&gt;Map&lt;/code&gt; supports keys of different types, while a Go map has one declared key type.&lt;/p&gt;

&lt;p&gt;For example, JavaScript can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;example&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Adetola&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go requires all keys in a particular map to have the same declared type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Example: User Lookup
&lt;/h2&gt;

&lt;p&gt;Imagine you're building a backend service and need to look up a user's account balance using their user ID.&lt;/p&gt;

&lt;p&gt;A map can help you associate each user ID with a corresponding balance.&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;balances&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"USER-001"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15000.50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"USER-002"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25000.00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"USER-003"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8000.75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exists&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"USER-001"&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;exists&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;"Account balance:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="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;"Account not found"&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Account balance: 15000.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a practical use of maps: you have an identifier and need to retrieve its associated information.&lt;/p&gt;

&lt;p&gt;Notice that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The keys are strings because the user IDs are strings.&lt;/li&gt;
&lt;li&gt;The values are &lt;code&gt;float64&lt;/code&gt; because the balances contain decimal values.&lt;/li&gt;
&lt;li&gt;The comma-ok idiom checks whether the requested user ID exists.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a simplified example. In production financial systems, monetary values generally require more careful representation than ordinary floating-point numbers.&lt;/p&gt;




&lt;h2&gt;
  
  
  Lesson Takeaways
&lt;/h2&gt;

&lt;p&gt;These are the points worth keeping in your study notes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A map is a built-in Go data structure that stores data as key-value pairs.&lt;/li&gt;
&lt;li&gt;A map is declared using &lt;code&gt;map[keyType]valueType&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Keys and values can have different types, but each type must remain consistent within its map.&lt;/li&gt;
&lt;li&gt;Map keys must be comparable. Slices, maps, and functions cannot be used directly as keys.&lt;/li&gt;
&lt;li&gt;You can initialise a map using a map literal or &lt;code&gt;make()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A nil map can be read from but cannot be used to add entries.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;mapName[key] = value&lt;/code&gt; to add or update an entry.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;mapName[key]&lt;/code&gt; to retrieve a value.&lt;/li&gt;
&lt;li&gt;A missing key returns the zero value of the map's value type.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;value, exists := mapName[key]&lt;/code&gt; to check whether a key exists. This is the comma-ok idiom.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;delete(mapName, key)&lt;/code&gt; to remove an entry.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;len(mapName)&lt;/code&gt; to get the number of entries.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;for range&lt;/code&gt; to iterate over a map.&lt;/li&gt;
&lt;li&gt;Map iteration order is not guaranteed.&lt;/li&gt;
&lt;li&gt;Assigning a map to another variable does not create an independent copy; both variables refer to the same map data.&lt;/li&gt;
&lt;li&gt;Ordinary Go maps are not safe for concurrent writes without synchronization.&lt;/li&gt;
&lt;/ol&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%2F7jlrbe3poeh1o6uhurbj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7jlrbe3poeh1o6uhurbj.png" alt=" "&gt;&lt;/a&gt;&lt;br&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%2Fb6g1q92jhpenzvj02xtg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb6g1q92jhpenzvj02xtg.png" alt=" "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map[string]int&lt;/code&gt; means string keys and integer values.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;value, exists := myMap[key]&lt;/code&gt; lets you distinguish a missing key from a key containing a zero value.&lt;/li&gt;
&lt;li&gt;Maps are useful when you need to retrieve information using an identifier rather than a numerical position.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>computerscience</category>
      <category>go</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Data Structures in Go[Arrays and Slices]</title>
      <dc:creator>Adetola Esther Aderibigbe</dc:creator>
      <pubDate>Fri, 06 Nov 2026 15:46:15 +0000</pubDate>
      <link>https://dev.to/adetolaesther/data-structures-in-goarrays-and-slices-57ek</link>
      <guid>https://dev.to/adetolaesther/data-structures-in-goarrays-and-slices-57ek</guid>
      <description>&lt;h2&gt;
  
  
  Introduction: Why Do We Need Data Structures?
&lt;/h2&gt;

&lt;p&gt;So far, we've covered the foundations of Go, including variables, data types, operators, conditional statements, loops, and functions.&lt;/p&gt;

&lt;p&gt;These concepts give us the building blocks we need to write Go programs. However, as we begin building more practical applications, we'll encounter a new challenge such as working with large amounts of data.&lt;/p&gt;

&lt;p&gt;Think about an application that manages users, processes transactions, stores product information, or tracks customer records.&lt;/p&gt;

&lt;p&gt;We won't always be working with a single value at a time. We'll often need to store, organise, access, modify, and manipulate collections of related data. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A banking application might need to store multiple customer account balances.&lt;/li&gt;
&lt;li&gt;An e-commerce application might need to manage a collection of products.
Storing each value in a separate variable would quickly become impractical and difficult to manage.
&lt;strong&gt;This is where data structures come in.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Data structures are ways of organising and storing data in a program so that we can access, manage, and manipulate it efficiently.&lt;/p&gt;

&lt;p&gt;Go provides several built-in data structures and mechanisms that help us work with collections of data.&lt;/p&gt;

&lt;p&gt;In this module, we'll explore four important concepts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Arrays and Slices:&lt;/strong&gt; Used to store collections of values of the same type.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maps:&lt;/strong&gt; Used to store data as key-value pairs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structs:&lt;/strong&gt; Used to group related values, potentially of different types, into a single custom data type.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pointers:&lt;/strong&gt; Used to store and work with the memory address of another value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We'll begin with arrays, slices, and maps before moving on to structs and pointers.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Arrays and Slices in Go&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In Go, arrays and slices are data structures that consist of an ordered sequence of elements. They are useful when we want to work with multiple related values, as they allow us to keep data that belongs together in a single variable and perform similar operations on multiple values at once.&lt;/p&gt;

&lt;p&gt;Coming from a JavaScript background, I'd assume most of us are already familiar with arrays and how they work. However, Go approaches arrays and slices a little differently, so I'd still love to go over them again.&lt;/p&gt;

&lt;p&gt;Although both are used to store multiple values of the same data type in a single variable, the major difference between them is their length.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;array has a fixed length&lt;/strong&gt;, meaning the number of elements it can contain is determined when it is created and cannot be changed.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;slice, on the other hand, is flexible&lt;/strong&gt;. Its length can change as we add or remove elements, making it more convenient when working with collections of data that may grow or shrink over time.&lt;/p&gt;

&lt;p&gt;We'll explore both concepts, how to define and initialise them, how to access and manipulate their elements, and the differences between them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining an Array
&lt;/h3&gt;

&lt;p&gt;An array is a collection of elements of the same data type with a fixed length. The length is part of its type.&lt;/p&gt;

&lt;p&gt;In Go, arrays are defined by specifying the size (length) of the array inside square brackets[], followed by the data type of its elements.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&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;span class="kt"&gt;int&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;var - Keyword used to declare a variable.&lt;br&gt;
numbers - Variable name.&lt;br&gt;
[3]- Length of the array (number of elements it can hold). &lt;br&gt;
int - Data type of the elements stored in the array. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important to note:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once an array has been declared, its length cannot be changed. However, its elements can be modified, and we can also initialise an array with values. &lt;br&gt;
 it's important to understand that &lt;strong&gt;an array's length is part of its type&lt;/strong&gt;. This is one of the major differences between arrays in Go and arrays in JavaScript.&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;first&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="kt"&gt;int&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;second&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;span class="kt"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although both arrays contain integers, they have different types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;first&lt;/code&gt; has the type &lt;code&gt;[2]int&lt;/code&gt; (an array of 2 integers).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;second&lt;/code&gt; has the type &lt;code&gt;[3]int&lt;/code&gt; (an array of 3 integers).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because their lengths are different, their types are also different.&lt;/p&gt;

&lt;p&gt;This means you cannot assign one array to the other or compare them directly as though they were the same type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important to note:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array length is part of its type.&lt;/li&gt;
&lt;li&gt;Arrays of different lengths are different types.&lt;/li&gt;
&lt;li&gt;Arrays of the same type can be compared, provided their element type is comparable.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Declaring and Initialising Arrays
&lt;/h1&gt;

&lt;p&gt;There are several ways to declare and initialise an array in Go.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Declaring an Array Without Specifying Initial Values
&lt;/h2&gt;

&lt;p&gt;We can declare an array without providing any initial 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;var&lt;/span&gt; &lt;span class="n"&gt;numbers&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;span class="kt"&gt;int&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;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0 0 0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates an array with three integer elements and since we haven't provided any initial values, Go automatically initialises each element with the zero value of its respective data type.&lt;/p&gt;

&lt;p&gt;For integers (&lt;code&gt;int&lt;/code&gt;), the zero value is &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This behaviour also applies to other data 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;names&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;span class="kt"&gt;string&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;names&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;strong&gt;Output:&lt;/strong&gt;
&lt;/h1&gt;



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

&lt;/div&gt;



&lt;p&gt;For strings, the zero value is an empty string (&lt;code&gt;""&lt;/code&gt;). When printed as part of an array, the empty strings appear as blank spaces between the brackets.&lt;br&gt;
 This is an important characteristic of Go. When variables are declared without explicit initial values, Go automatically assigns them their respective zero values. Arrays follow this rule element by element.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Initialising an Array With Explicit Values
&lt;/h2&gt;

&lt;p&gt;We can also provide initial values when declaring an array.&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;score&lt;/span&gt; &lt;span class="o"&gt;:=&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;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;85&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;95&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;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[85 90 95]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are declaring and initialising an array called &lt;code&gt;score&lt;/code&gt; with three integer values. The values are assigned in order, starting from index &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;90&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;95&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The syntax consists of three main parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The array's length: &lt;code&gt;[3]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The data type: &lt;code&gt;int&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The initial values: &lt;code&gt;{85, 90, 95}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Specifying the Length but Providing Fewer Initial Values
&lt;/h2&gt;

&lt;p&gt;Go also allows us to initialise an array with fewer values than its declared length.&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="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;4&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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&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;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight matlab"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="mi"&gt;0&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've declared an array called &lt;code&gt;numbers&lt;/code&gt; with a length of four, but we've only provided two initial values. What happens?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first element (index 0) receives &lt;code&gt;10&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The second element (index 1) receives &lt;code&gt;20&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The third element (index 2) receives the zero value, &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The fourth element (index 3) also receives the zero value, &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The remaining elements are automatically initialised with the zero value of their data type.Even though we provided only two initial values, the array still has a length of four which does not change because the length was already specified during declaration.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Allowing Go to Infer the Array Length
&lt;/h2&gt;

&lt;p&gt;Go provides the &lt;code&gt;...&lt;/code&gt; notation for array literals when we want the compiler to determine the array's length automatically. Instead of manually specifying the number of elements, we allow Go to count the initial values and determine the length.&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="p"&gt;[&lt;/span&gt;&lt;span class="o"&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="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&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="m"&gt;40&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;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight matlab"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="mi"&gt;40&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've used &lt;code&gt;...&lt;/code&gt; instead of explicitly specifying the array's length.&lt;/p&gt;

&lt;p&gt;Go counts the number of initial values and infers the array's type as &lt;code&gt;[4]int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It’s important to know that the &lt;code&gt;...&lt;/code&gt; notation does not mean that the array has a variable length, it simply tells the compiler to determine the array's length from the number of initial values provided. &lt;strong&gt;Once the array has been initialised, its length is fixed&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go vs JavaScript: Array Initialisation
&lt;/h2&gt;

&lt;p&gt;Coming from a JavaScript background, let's look at how array initialisation differs in both languages.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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;20&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&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;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&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="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In JavaScript, we can initialise an array with two elements and use the &lt;code&gt;push()&lt;/code&gt; method to add another element.&lt;/p&gt;

&lt;p&gt;The array grows from two elements to three.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;4&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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&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;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight matlab"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Go, we've declared an array with a length of four, even though we've only provided two initial values.&lt;/p&gt;

&lt;p&gt;The remaining two elements are automatically assigned their zero values.&lt;/p&gt;

&lt;p&gt;The main difference is that Go arrays have a fixed length, whereas JavaScript arrays can grow or shrink as elements are added or removed.&lt;/p&gt;

&lt;p&gt;To create a Go collection that can grow dynamically, we typically use &lt;strong&gt;slices&lt;/strong&gt;, which we'll explore later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson Takeaway
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;The length of an array is part of its type in Go.&lt;/li&gt;
&lt;li&gt;Go automatically assigns zero values to elements that aren't explicitly initialised.&lt;/li&gt;
&lt;li&gt;You can initialise an array with fewer values than its declared length.&lt;/li&gt;
&lt;li&gt;The ... notation allows Go to infer the array's length.&lt;/li&gt;
&lt;li&gt;Array elements can be modified, but the array's length cannot be changed.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Finding the Length of an Array Using &lt;code&gt;len()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Go provides a built-in function called &lt;code&gt;len()&lt;/code&gt; that allows us to obtain the number of elements in an array or slice. This returns the total number of elements, not the highest 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="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;:=&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;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;20&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="m"&gt;40&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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 plaintext"&gt;&lt;code&gt;3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although the array's highest index is &lt;code&gt;2&lt;/code&gt;, its length is &lt;code&gt;3&lt;/code&gt; because it contains three elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Go vs JavaScript: Finding Array Length
&lt;/h3&gt;

&lt;p&gt;In JavaScript, we use the &lt;code&gt;.length&lt;/code&gt; property to determine the number of elements in an array.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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;20&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;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;4&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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&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="m"&gt;40&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;The difference is simply how both languages provide access to the length:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript uses the &lt;code&gt;.length&lt;/code&gt; property.&lt;/li&gt;
&lt;li&gt;Go uses the built-in &lt;code&gt;len()&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why is &lt;code&gt;len()&lt;/code&gt; important?
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;len()&lt;/code&gt; function is frequently used when working with arrays and slices, particularly when iterating through their elements using loops. It allows us to determine the number of elements we need to work with without manually counting them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Array Assignments in Go
&lt;/h2&gt;

&lt;p&gt;One of the important differences between arrays in Go and JavaScript is how they behave when assigned to another variable.&lt;/p&gt;

&lt;p&gt;In Go, &lt;strong&gt;arrays are value types&lt;/strong&gt;. This means that when we assign an array to another variable, Go copies the entire array, including all its elements.The two variables now hold separate copies of the array meaning modifying one does not affect the other.&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;first&lt;/span&gt; &lt;span class="o"&gt;:=&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;span class="kt"&gt;int&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;20&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="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;

&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;90&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;first&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;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[10 20 30]
[90 20 30]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down what's happening:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We declare an array called &lt;code&gt;first&lt;/code&gt; containing three integers.&lt;/li&gt;
&lt;li&gt;We create another variable called &lt;code&gt;second&lt;/code&gt; and assign &lt;code&gt;first&lt;/code&gt; to it.&lt;/li&gt;
&lt;li&gt;Go copies all the elements of &lt;code&gt;first&lt;/code&gt; into &lt;code&gt;second&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We modify the element at index &lt;code&gt;0&lt;/code&gt; of &lt;code&gt;second&lt;/code&gt;, changing its value from &lt;code&gt;10&lt;/code&gt; to &lt;code&gt;90&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When we print both arrays, we see that &lt;code&gt;first&lt;/code&gt; remains unchanged while &lt;code&gt;second&lt;/code&gt; contains the modified value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This happens because both variables hold separate copies of the array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Go vs JavaScript: Array Assignment
&lt;/h3&gt;

&lt;p&gt;JavaScript handles array assignment differently. In JavaScript, arrays are reference types. Assigning an array to another variable copies the reference to the same underlying array object, rather than creating an independent copy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&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;20&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[90, 20, 30]
[90, 20, 30]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that modifying &lt;code&gt;second&lt;/code&gt; also affects &lt;code&gt;first&lt;/code&gt;, This happens because both variables refer to the same array object in memory.&lt;/p&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;JavaScript&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Arrays are value types.&lt;/td&gt;
&lt;td&gt;Arrays are reference types.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assigning an array copies its elements.&lt;/td&gt;
&lt;td&gt;Assigning an array copies its reference.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Each variable holds an independent array copy.&lt;/td&gt;
&lt;td&gt;Both variables can refer to the same array object.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Modifying one copy does not affect the other.&lt;/td&gt;
&lt;td&gt;Modifying the shared array affects both references.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This value-copying behaviour also applies when passing arrays to functions in Go. When an array is passed as an argument to a function, the function receives a copy of the array.&lt;/p&gt;

&lt;p&gt;This means that even if the function modifies an element, the original array outside the function remains unchanged. We'll explore this behaviour further when we get into functions and pointers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Are Arrays Less Common Than Slices in Go?
&lt;/h2&gt;

&lt;p&gt;Although arrays are useful in Go, they are less commonly used than slices in everyday application development. The main reason is their fixed length.&lt;br&gt;
 In real-world applications, the amount of data we work with can change frequently. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The number of users in an application can increase or decrease.&lt;/li&gt;
&lt;li&gt;The number of products in an online store can change.&lt;/li&gt;
&lt;li&gt;The number of transactions processed by a banking application can vary.&lt;/li&gt;
&lt;li&gt;The number of records returned from an API can change depending on the request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Arrays are not particularly convenient for these situations because their length cannot be changed after declaration. Slices, on the other hand, provide more flexibility because their length can grow or shrink as needed.&lt;/p&gt;
&lt;h3&gt;
  
  
  So When Should We Use Arrays?
&lt;/h3&gt;

&lt;p&gt;Arrays are still useful, particularly when we know the exact number of elements we need and that number is not expected to change.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Storing the RGB values of a colour (three integers).&lt;/li&gt;
&lt;li&gt;Representing a fixed number of coordinates.&lt;/li&gt;
&lt;li&gt;Working with fixed-size cryptographic keys.&lt;/li&gt;
&lt;li&gt;Storing a fixed set of measurements.
&lt;strong&gt;Takeaway&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Use arrays when a fixed size is an intentional requirement.&lt;/li&gt;
&lt;li&gt;Use slices for most collections where the number of elements may grow, shrink, or change over time.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Slices in Go
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Defining Slices
&lt;/h2&gt;

&lt;p&gt;A slice is a flexible, dynamically sized sequence of elements that describes a section of an underlying array. We can also think of a slice as a dynamic array because, unlike arrays, its length can grow or shrink as needed.&lt;br&gt;
However, it's important to understand that &lt;strong&gt;a slice itself is not an underlying array&lt;/strong&gt;. A slice is a value that describes a portion of an array that it exposes.&lt;br&gt;
 A slice provides access to a section of an underlying array, and multiple slices can refer to the same underlying array, this means that different slices can share the same underlying data. Changes made through one slice can affect what another slice sees if they refer to the same elements.&lt;/p&gt;
&lt;h3&gt;
  
  
  Slice Syntax
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;collection&lt;/code&gt;: The array or slice we want to extract elements from.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;start&lt;/code&gt;: The index where the slice begins. This index is inclusive.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;end&lt;/code&gt;: The index where the slice stops. This index is exclusive.
The start index is included in the resulting slice, while the end index is excluded.
&lt;/li&gt;
&lt;/ul&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="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;5&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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&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="m"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;50&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, let's create a slice called &lt;code&gt;middle&lt;/code&gt; from this array:&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;middle&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="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="m"&gt;4&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;middle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight matlab"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our original array contains five elements so when we 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="n"&gt;middle&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="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're extracting a section of the original array, starting at index &lt;code&gt;1&lt;/code&gt; and stopping before index &lt;code&gt;4&lt;/code&gt;.&lt;br&gt;
This means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Index &lt;code&gt;1&lt;/code&gt; is included (value: &lt;code&gt;20&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Index &lt;code&gt;2&lt;/code&gt; is included (value: &lt;code&gt;30&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Index &lt;code&gt;3&lt;/code&gt; is included (value: &lt;code&gt;40&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Index &lt;code&gt;4&lt;/code&gt; is excluded (value: &lt;code&gt;50&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The resulting slice contains &lt;code&gt;[20 30 40]&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Declaring and Initialising Slices
&lt;/h2&gt;

&lt;p&gt;There are several ways to declare and initialise slices in Go. Let's explore them.&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Declaring a Slice Without Initialising It:
&lt;/h3&gt;

&lt;p&gt;We can declare a slice without providing any initial 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;var&lt;/span&gt; &lt;span class="n"&gt;numbers&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;This declares a slice of integers but unlike arrays, there is no fixed length included in the declaration. &lt;br&gt;
However, there is an important distinction between declaring a slice and allowing Go to infer the length of an array. Although the declarations might look similar, they behave differently.&lt;/p&gt;

&lt;p&gt;A newly declared slice without an initial value is a nil slice. A nil slice has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Length of &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Capacity of &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;No underlying array currently associated with it.
We can verify this using the &lt;code&gt;len()&lt;/code&gt; and &lt;code&gt;cap()&lt;/code&gt; functions.
&lt;/li&gt;
&lt;/ul&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;numbers&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&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;numbers&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="nb"&gt;cap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[]
0
0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though the slice has no elements, we can still add elements to it using the &lt;code&gt;append()&lt;/code&gt; 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;var&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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;20&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;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight matlab"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works because &lt;code&gt;append()&lt;/code&gt; returns a slice value containing the added elements, which we assign back to &lt;code&gt;numbers&lt;/code&gt;. It’s also important to not that a nil slice is not the same as an array containing zero values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;numbers&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;span class="kt"&gt;int&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;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0 0 0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This array contains three elements, even though all three elements have the zero value of &lt;code&gt;int&lt;/code&gt;. However:&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;numbers&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&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;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This slice contains no elements. The difference is that an array has a fixed number of elements, while a nil slice has a length and capacity of zero.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Initialising a Slice Using a Slice Literal
&lt;/h3&gt;

&lt;p&gt;Another way to create a slice is by using a slice literal. A slice literal looks similar to an array literal, except that we omit the length inside the square brackets.&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="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;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&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="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;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight matlab"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;20&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;This creates a slice containing three integers but  lrt's notice the difference&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="c"&gt;// Array&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;:=&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;span class="kt"&gt;int&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;20&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="c"&gt;// Slice&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&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="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&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;For an array, we specify the length inside the brackets while for a slice, we leave the brackets empty. The type of our slice is &lt;code&gt;[]int&lt;/code&gt;, not &lt;code&gt;[3]int&lt;/code&gt;.&lt;br&gt;
 We can also initialise slices containing other data types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;String slice:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;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;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Mary"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Peter"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Boolean slice:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Floating-point slice:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;prices&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;float64&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;10.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;30.5&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just like arrays, all elements in a slice must have the same data type.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Creating a Slice Using &lt;code&gt;make()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Go provides a built-in function called &lt;code&gt;make()&lt;/code&gt; that allows us to create and initialise slices with a specified length and optional capacity.&lt;br&gt;
&lt;strong&gt;syntax:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;make&lt;/code&gt;: The built-in function used to create the slice.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[]T&lt;/code&gt;: Specifies the slice's data type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;length&lt;/code&gt;: Specifies the initial number of elements in the slice.
&lt;/li&gt;
&lt;/ul&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;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;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0 0 0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a slice of integers with a length of three with all three elements initially assigned the zero value of &lt;code&gt;int&lt;/code&gt;, which is &lt;code&gt;0&lt;/code&gt;. We can then assign values to individual elements using their indices.&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;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;
&lt;span class="n"&gt;numbers&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="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;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight matlab"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;20&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;The &lt;code&gt;make()&lt;/code&gt; function is useful when we know how many elements we want to work with from the beginning, but we don't necessarily have their actual values yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Creating a Slice With a Specified Capacity
&lt;/h3&gt;

&lt;p&gt;We can also provide a capacity when creating a slice using &lt;code&gt;make()&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[]T&lt;/code&gt;: Specifies the slice's data type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;length&lt;/code&gt;: Specifies the number of elements the slice currently contains.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;capacity&lt;/code&gt;: Specifies the maximum number of elements the slice can hold before needing a larger underlying array.
&lt;/li&gt;
&lt;/ul&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;span class="m"&gt;5&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;numbers&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="nb"&gt;cap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0 0 0]
3
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a slice of integers with Length: &lt;code&gt;3&lt;/code&gt;, Capacity: &lt;code&gt;5&lt;/code&gt;, with the slice currently containing three elements, all initialised to zero. However, its underlying array has room for five elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Length vs Capacity
&lt;/h3&gt;

&lt;p&gt;This is an important distinction when working with slices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Length (&lt;code&gt;len&lt;/code&gt;)&lt;/strong&gt; refers to the number of elements currently contained in the slice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Capacity (&lt;code&gt;cap&lt;/code&gt;)&lt;/strong&gt; refers to the number of elements between the beginning of the slice and the end of its underlying array.&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;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The slice has three elements, but its capacity is four, it's important to note that capacity does not mean the slice already contains five elements, It currently contains only three elements.&lt;br&gt;
 To access an element directly using its index, that index must be within the slice's current length, not merely within its capacity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;numbers&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;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would cause an &lt;code&gt;index out of range&lt;/code&gt; panic because the slice's current length is three, and its valid indices are &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, and &lt;code&gt;2&lt;/code&gt;. To add a fourth element, we use `append()&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := make([]int, 3, 4)&lt;/p&gt;

&lt;p&gt;numbers[0] = 10&lt;br&gt;
numbers[1] = 20&lt;br&gt;
numbers[2] = 30&lt;br&gt;
numbers = append(numbers, 40)&lt;br&gt;
fmt.Println(numbers)&lt;br&gt;
fmt.Println(len(numbers))&lt;br&gt;
fmt.Println(cap(numbers))&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`plaintext&lt;br&gt;
[10 20 30 40]&lt;br&gt;
4&lt;br&gt;
4&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
Notice what happened, Initially the slice had a length of &lt;code&gt;3&lt;/code&gt; and capacity of &lt;code&gt;5&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We used &lt;code&gt;append()&lt;/code&gt; to add another element.&lt;/li&gt;
&lt;li&gt;The length increased from &lt;code&gt;3&lt;/code&gt; to &lt;code&gt;4&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The capacity remained &lt;code&gt;5&lt;/code&gt; with enough room to still grow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is how we can add elements to a slice without directly accessing an index outside its current length.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shared Underlying Arrays
&lt;/h2&gt;

&lt;p&gt;When you create a slice from an array, the slice does not create an independent copy of the selected elements. Instead, it refers to the relevant portion of the original array. Let's look at an example:&lt;br&gt;
&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := [5]int{10, 20, 30, 40, 50}&lt;/p&gt;

&lt;p&gt;middle := numbers[1:4]&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers) // [10 20 30 40 50]&lt;br&gt;
fmt.Println(middle)  // [20 30 40]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The original array still contains all five elements:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Index&lt;/th&gt;
&lt;th&gt;0&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;2&lt;/th&gt;
&lt;th&gt;3&lt;/th&gt;
&lt;th&gt;4&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Value&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The slice &lt;code&gt;middle&lt;/code&gt; starts at index 1 and ends before index 4. Therefore, it contains the values at indexes 1, 2 and 3: &lt;code&gt;20, 30, 40&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;What happens when we modify the slice?&lt;/strong&gt;&lt;br&gt;
We can modify an element through the slice:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
middle[0] = 99&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers) // [10 99 30 40 50]&lt;br&gt;
fmt.Println(middle)  // [99 30 40]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Notice that changing &lt;code&gt;middle[0]&lt;/code&gt; also changes &lt;code&gt;numbers[1]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This happens because &lt;code&gt;middle[0]&lt;/code&gt; refers to the same underlying array element as &lt;code&gt;numbers[1]&lt;/code&gt;. The slice is not an independent copy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiple Slices Can Share One Underlying Array
&lt;/h3&gt;

&lt;p&gt;More than one slice can refer to different portions of the same underlying array. If those portions overlap, changes made through one slice can be visible through the other.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := [5]int{10, 20, 30, 40, 50}&lt;/p&gt;

&lt;p&gt;first := numbers[0:3]&lt;br&gt;
second := numbers[2:5]&lt;/p&gt;

&lt;p&gt;fmt.Println(first)  // [10 20 30]&lt;br&gt;
fmt.Println(second) // [30 40 50]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Both slices include the element at index 2, which has the value &lt;code&gt;30&lt;/code&gt;, Let's modify the elements through the slices:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
first[1] = 28&lt;br&gt;
first[2] = 29&lt;br&gt;
second[2] = 45&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers) // [10 28 29 40 45]&lt;br&gt;
fmt.Println(first)   // [10 28 29]&lt;br&gt;
fmt.Println(second)  // [29 40 45]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;first[1] = 28&lt;/code&gt; changed &lt;code&gt;numbers[1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;first[2] = 29&lt;/code&gt; changed &lt;code&gt;numbers[2]&lt;/code&gt;, which is also visible through &lt;code&gt;second[0]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;second[2] = 45&lt;/code&gt; changed &lt;code&gt;numbers[4]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;second&lt;/code&gt; are distinct slice values, they share the same underlying array. It’s important to note that When multiple slices share an underlying array, modifying an element through one slice can affect the other slices if they refer to that same element.&lt;/p&gt;

&lt;p&gt;You can read more about this in the official &lt;a href="https://go.dev/blog/slices" rel="noopener noreferrer"&gt;Go blog article&lt;/a&gt;, &lt;/p&gt;

&lt;h2&gt;
  
  
  Slice Length and Capacity
&lt;/h2&gt;

&lt;p&gt;Every slice has two important properties:&lt;/p&gt;

&lt;h3&gt;
  
  
  Length (&lt;code&gt;len&lt;/code&gt;): The length is the number of elements currently accessible through a slice. You can get it using Go's built-in &lt;code&gt;len()&lt;/code&gt; function.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Capacity (&lt;code&gt;cap&lt;/code&gt;): The capacity is the number of elements from the slice's starting position to the end of its underlying array. You can get it using Go's built-in &lt;code&gt;cap()&lt;/code&gt; function.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Capacity is always greater than or equal to length.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The difference is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;len()&lt;/code&gt; tells you how many elements are currently part of the slice.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cap()&lt;/code&gt; tells you how much space is available in the underlying array from the slice's starting position.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
numbers := make([]int, 2, 5)&lt;br&gt;
fmt.Println(numbers)      // [0 0]&lt;br&gt;
fmt.Println(len(numbers)) // 2&lt;br&gt;
fmt.Println(cap(numbers)) // 5&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[]int&lt;/code&gt; means we are creating a slice of integers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2&lt;/code&gt; is the length.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;5&lt;/code&gt; is the capacity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The slice currently has two elements, both initialised to their zero value (&lt;code&gt;0&lt;/code&gt;) and although its capacity is five, only two elements are currently accessible through the slice. We can still assign values to the existing elements&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers[0] = 10&lt;br&gt;
numbers[1] = 20&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers) // [10 20]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Both assignments are valid because indexes &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; are within the current length of the slice.&lt;br&gt;
However, this would cause a runtime panic:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;javascript&lt;br&gt;
numbers[2] = 30&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Why?&lt;br&gt;
Because the slice's length is currently &lt;code&gt;2&lt;/code&gt;, meaning its valid indexes are &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; and even though the capacity is &lt;code&gt;5&lt;/code&gt;, index &lt;code&gt;2&lt;/code&gt; is not yet part of the slice's accessible elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Capacity does not give you permission to access elements beyond the current length.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To make the slice longer, we need to use &lt;code&gt;append()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
numbers = append(numbers, 30)&lt;br&gt;
fmt.Println(numbers)      // [10 20 30]&lt;br&gt;
fmt.Println(len(numbers)) // 3&lt;br&gt;
fmt.Println(cap(numbers)) // 5&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;append()&lt;/code&gt; function adds an element to the slice and returns the updated slice. We assign the result back to &lt;code&gt;numbers&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The length increases from &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;3&lt;/code&gt;, while the capacity remains &lt;code&gt;5&lt;/code&gt; because there is still room in the underlying array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lesson Takeaway
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;len()&lt;/code&gt; tells you how many elements you can access in the slice right now.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cap()&lt;/code&gt; tells you how much room is available from the slice's starting position to the end of its underlying array.&lt;/li&gt;
&lt;li&gt;You cannot access elements using an index beyond the current length, even if the capacity allows room for more elements.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;append()&lt;/code&gt; to grow a slice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Adding Elements to Slices with &lt;code&gt;append()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;We've already seen that slices can grow beyond their initial length. Go provides a built-in function called &lt;code&gt;append()&lt;/code&gt; to add elements to a slice.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;append()&lt;/code&gt; function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adds one or more elements to an existing slice.&lt;/li&gt;
&lt;li&gt;Returns a new slice value representing the result.&lt;/li&gt;
&lt;li&gt;Does not necessarily create a new underlying array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. Appending a Single Element
&lt;/h3&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
slice = append(slice, value)&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
Let's look at an example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20, 30}&lt;/p&gt;

&lt;p&gt;numbers = append(numbers, 40)&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers) // [10 20 30 40]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is what happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We create a slice called &lt;code&gt;numbers&lt;/code&gt; containing &lt;code&gt;10, 20, 30&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We call &lt;code&gt;append(numbers, 40)&lt;/code&gt; to add &lt;code&gt;40&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;append()&lt;/code&gt; returns the resulting slice.&lt;/li&gt;
&lt;li&gt;We assign the returned slice back to &lt;code&gt;numbers&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Why do we assign the result back?&lt;/strong&gt;&lt;br&gt;
Because &lt;code&gt;append()&lt;/code&gt; returns a slice value. It does not simply modify the existing slice variable in place. This is why we write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
numbers = append(numbers, 40)&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
Rather than just:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;python&lt;br&gt;
append(numbers, 40)&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Appending Multiple Elements
&lt;/h3&gt;

&lt;p&gt;You can append more than one element in a single operation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20}&lt;/p&gt;

&lt;p&gt;numbers = append(numbers, 30, 40, 50)&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers) // [10 20 30 40 50]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, we added three elements (&lt;code&gt;30&lt;/code&gt;, &lt;code&gt;40&lt;/code&gt; and &lt;code&gt;50&lt;/code&gt;) in one &lt;code&gt;append()&lt;/code&gt; operation.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Appending One Slice to Another
&lt;/h3&gt;

&lt;p&gt;You can also append the elements of one slice to another.&lt;/p&gt;

&lt;p&gt;To do this, Go uses the &lt;code&gt;...&lt;/code&gt; operator.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
first := []int{10, 20, 30}&lt;br&gt;
second := []int{40, 50}&lt;/p&gt;

&lt;p&gt;first = append(first, second...)&lt;/p&gt;

&lt;p&gt;fmt.Println(first) // [10 20 30 40 50]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;...&lt;/code&gt; operator expands the elements of &lt;code&gt;second&lt;/code&gt; so that they can be passed as individual arguments to &lt;code&gt;append()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Without &lt;code&gt;...&lt;/code&gt;, Go would try to append the entire &lt;code&gt;second&lt;/code&gt; slice as one element, which would cause a type error because &lt;code&gt;first&lt;/code&gt; is a slice of integers, not a slice of slices.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Does append() Always Create a New Underlying Array?
&lt;/h3&gt;

&lt;p&gt;No, This is an important part of understanding how slices work, Let’s remember every slice has a length and a capacity. So when you append elements, Go checks whether the underlying array has enough remaining capacity.&lt;br&gt;
There are two possible scenarios:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario A: There is sufficient capacity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the underlying array has enough capacity, Go reuses the existing array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := make([]int, 2, 5)&lt;br&gt;
numbers[0] = 10&lt;br&gt;
numbers[1] = 20&lt;/p&gt;

&lt;p&gt;numbers = append(numbers, 30)&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers)      // [10 20 30]&lt;br&gt;
fmt.Println(len(numbers)) // 3&lt;br&gt;
fmt.Println(cap(numbers)) // 5&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
Initially:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Length = 2&lt;/li&gt;
&lt;li&gt;Capacity = 5
After appending &lt;code&gt;30&lt;/code&gt;:&lt;/li&gt;
&lt;li&gt;Length = 3&lt;/li&gt;
&lt;li&gt;Capacity = 5&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since the underlying array has enough capacity, Go can reuse it without allocating a new one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario B: There is insufficient capacity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the underlying array does not have enough capacity, Go allocates a new underlying array with sufficient capacity and copies the existing elements into it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := make([]int, 2, 2)&lt;/p&gt;

&lt;p&gt;numbers[0] = 10&lt;br&gt;
numbers[1] = 20&lt;/p&gt;

&lt;p&gt;numbers = append(numbers, 30)&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers)      // [10 20 30]&lt;br&gt;
fmt.Println(len(numbers)) // 3&lt;br&gt;
fmt.Println(cap(numbers)) // At least 3&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Initially:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Length = 2&lt;/li&gt;
&lt;li&gt;Capacity = 2&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When we append &lt;code&gt;30&lt;/code&gt;, the slice needs to grow beyond its available capacity, Go therefore allocates a new underlying array, copies the existing elements into it and adds the new element. The resulting slice now refers to the new underlying array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Go determines the new capacity internally. You should not assume that it will always double the previous capacity or grow by a particular amount.&lt;/p&gt;

&lt;h3&gt;
  
  
  Go vs JavaScript: append() and push()
&lt;/h3&gt;

&lt;p&gt;Coming from JavaScript, you can think of Go's &lt;code&gt;append()&lt;/code&gt; as having a similar purpose to JavaScript's &lt;code&gt;.push()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Both are used to add elements to collections.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`javascript&lt;br&gt;
const numbers = [10, 20, 30];&lt;/p&gt;

&lt;p&gt;numbers.push(40);&lt;/p&gt;

&lt;p&gt;console.log(numbers); // [10, 20, 30, 40]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20, 30}&lt;/p&gt;

&lt;p&gt;numbers = append(numbers, 40)&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers) // [10 20 30 40]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However, there is an important difference:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;JavaScript&lt;/th&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Uses &lt;code&gt;.push()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Uses &lt;code&gt;append()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.push()&lt;/code&gt; modifies the existing array&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;append()&lt;/code&gt; returns a slice value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No need to assign the result back when using &lt;code&gt;.push()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;You should assign the result back when using &lt;code&gt;append()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JavaScript arrays grow dynamically&lt;/td&gt;
&lt;td&gt;Go slices grow dynamically, but their underlying arrays have a capacity&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Lesson takeaway:&lt;/strong&gt; &lt;br&gt;
&lt;code&gt;append()&lt;/code&gt; adds elements to a slice and returns the resulting slice. Depending on the available capacity, Go may reuse the existing underlying array or allocate a new one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Slicing an Array or Another Slice
&lt;/h2&gt;

&lt;p&gt;We've already seen how to create a slice from an array. We can also use slicing expressions to select different portions of an array or an existing slice. Let's start with an example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20, 30, 40, 50}&lt;/p&gt;

&lt;p&gt;parts := numbers[1:4]&lt;/p&gt;

&lt;p&gt;fmt.Println(parts) // [20 30 40]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The expression &lt;code&gt;numbers[1:4]&lt;/code&gt; means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start at index &lt;code&gt;1&lt;/code&gt; (inclusive).&lt;/li&gt;
&lt;li&gt;Stop before index &lt;code&gt;4&lt;/code&gt; (exclusive), therefore the resulting slice contains &lt;code&gt;20, 30, 40&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. Omitting the Start Index
&lt;/h3&gt;

&lt;p&gt;You can omit the start index to select elements from the beginning of the slice.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20, 30, 40, 50}&lt;/p&gt;

&lt;p&gt;parts := numbers[:3]&lt;/p&gt;

&lt;p&gt;fmt.Println(parts) // [10 20 30]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
This is the same as writing:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
parts := numbers[0:3]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When the start index is omitted, Go uses &lt;code&gt;0&lt;/code&gt; by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Omitting the End Index
&lt;/h3&gt;

&lt;p&gt;You can also omit the end index to select elements from a particular position through to the end of the slice.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20, 30, 40, 50}&lt;/p&gt;

&lt;p&gt;parts := numbers[2:]&lt;/p&gt;

&lt;p&gt;fmt.Println(parts) // [30 40 50]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is equivalent to:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
parts := numbers[2:len(numbers)]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When the end index is omitted, Go uses the length of the slice.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Omitting Both Indexes
&lt;/h3&gt;

&lt;p&gt;You can omit both indexes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20, 30, 40, 50}&lt;/p&gt;

&lt;p&gt;parts := numbers[:]&lt;/p&gt;

&lt;p&gt;fmt.Println(parts) // [10 20 30 40 50]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This creates another slice describing the same elements as the original slice. It does not create an independent copy. Both slices refer to the same underlying array.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Creating a Slice from Another Slice
&lt;/h3&gt;

&lt;p&gt;You can also create a new slice from an existing slice.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20, 30, 40, 50}&lt;/p&gt;

&lt;p&gt;first := numbers[1:5]&lt;br&gt;
second := first[1:3]&lt;/p&gt;

&lt;p&gt;fmt.Println(first)  // [20 30 40 50]&lt;br&gt;
fmt.Println(second) // [30 40]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;First:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;plaintext&lt;br&gt;
first := numbers[1:5]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This creates a slice containing the elements from index &lt;code&gt;1&lt;/code&gt; up to, but not including, index &lt;code&gt;5&lt;/code&gt; of &lt;code&gt;numbers&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;[20 30 40 50]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
second := first[1:3]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This creates another slice from &lt;code&gt;first&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An important detail is that the indexes in this second slicing expression are relative to &lt;code&gt;first&lt;/code&gt;, not the original &lt;code&gt;numbers&lt;/code&gt; slice.&lt;/p&gt;

&lt;p&gt;Therefore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;first[1]&lt;/code&gt; is &lt;code&gt;30&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;first[2]&lt;/code&gt; is &lt;code&gt;40&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The resulting &lt;code&gt;second&lt;/code&gt; slice is &lt;code&gt;[30 40]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Both slices still refer to portions of the same underlying array.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Valid Slice Indexes
&lt;/h3&gt;

&lt;p&gt;When using a two-index slice expression, the indexes must satisfy certain rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The start index must not be negative.&lt;/li&gt;
&lt;li&gt;The end index must not exceed the length of the collection.&lt;/li&gt;
&lt;li&gt;The start index must not be greater than the end index.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20, 30, 40, 50}&lt;/p&gt;

&lt;p&gt;parts := numbers[1:4] // Valid&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;br&gt;
But these would cause runtime panics:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;python&lt;br&gt;
numbers[-1:3] // Negative start index&lt;br&gt;
numbers[1:6]  // End index exceeds length&lt;br&gt;
numbers[4:2]  // Start index is greater than end index&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remember that the end index is exclusive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing Elements from a Slice
&lt;/h2&gt;

&lt;p&gt;Go does not have a built-in &lt;code&gt;remove()&lt;/code&gt; function for slices.&lt;/p&gt;

&lt;p&gt;However, a common technique is to combine slicing and &lt;code&gt;append()&lt;/code&gt; to remove an element.&lt;/p&gt;

&lt;p&gt;Let's say we have this slice:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
numbers := []int{10, 20, 30, 40, 50}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We want to remove the element at index &lt;code&gt;2&lt;/code&gt;, which is &lt;code&gt;30&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can do this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
i := 2&lt;/p&gt;

&lt;p&gt;numbers = append(numbers[:i], numbers[i+1:]...)&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers) // [10 20 40 50]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's break down what happens.&lt;br&gt;
&lt;strong&gt;Step 1: Declare the index to remove&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;plaintext&lt;br&gt;
i := 2&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We declare &lt;code&gt;i&lt;/code&gt; and assign it the value &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the index of the element we want to remove.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Select the elements before the index&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;python&lt;br&gt;
numbers[:i]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
Since &lt;code&gt;i&lt;/code&gt; is &lt;code&gt;2&lt;/code&gt;, this becomes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;python&lt;br&gt;
numbers[:2]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
It selects the elements from the beginning up to, but not including, index &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;[10 20]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Notice that the element at index &lt;code&gt;2&lt;/code&gt; (&lt;code&gt;30&lt;/code&gt;) is excluded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Select the elements after the index&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;python&lt;br&gt;
numbers[i+1:]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;i&lt;/code&gt; is &lt;code&gt;2&lt;/code&gt;, this becomes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;python&lt;br&gt;
numbers[3:]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It selects the elements starting from index &lt;code&gt;3&lt;/code&gt; through to the end.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;[40 50]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Combine both portions using append()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
numbers = append(numbers[:i], numbers[i+1:]...)&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is where everything comes together.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;numbers[:i]&lt;/code&gt; gives us the elements before the element we want to remove: &lt;code&gt;[10 20]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;numbers[i+1:]&lt;/code&gt; gives us the elements after the element we want to remove: &lt;code&gt;[40 50]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;...&lt;/code&gt; expands the second slice into individual elements.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;append()&lt;/code&gt; adds those elements to the first portion.&lt;/li&gt;
&lt;li&gt;We assign the resulting slice back to &lt;code&gt;numbers&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;code&gt;[10 20 40 50]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The element &lt;code&gt;30&lt;/code&gt; is no longer part of the resulting slice.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Does the &lt;code&gt;...&lt;/code&gt; Operator Do Here?
&lt;/h3&gt;

&lt;p&gt;Remember that &lt;code&gt;append()&lt;/code&gt; can accept multiple individual values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
numbers = append(numbers, 40, 50)&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But when we want to append the elements of another slice, we use &lt;code&gt;...&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
numbers = append(numbers, anotherSlice...)&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;...&lt;/code&gt; operator expands the slice into individual arguments.&lt;/p&gt;

&lt;p&gt;Without it, Go would attempt to append the entire slice as a single value, which would cause a type error when working with a &lt;code&gt;[]int&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript vs Go: Removing Elements
&lt;/h3&gt;

&lt;p&gt;In JavaScript, you can use the &lt;code&gt;.splice()&lt;/code&gt; method to remove elements from an array.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`javascript&lt;br&gt;
const numbers = [10, 20, 30, 40, 50];&lt;/p&gt;

&lt;p&gt;numbers.splice(2, 1);&lt;/p&gt;

&lt;p&gt;console.log(numbers); // [10, 20, 40, 50]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.splice()&lt;/code&gt; method takes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The index where removal should begin.&lt;/li&gt;
&lt;li&gt;The number of elements to remove.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Go, we use slicing and &lt;code&gt;append()&lt;/code&gt;:&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
numbers := []int{10, 20, 30, 40, 50}&lt;/p&gt;

&lt;p&gt;i := 2&lt;br&gt;
numbers = append(numbers[:i], numbers[i+1:]...)&lt;/p&gt;

&lt;p&gt;fmt.Println(numbers) // [10 20 40 50]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The difference is that JavaScript provides &lt;code&gt;.splice()&lt;/code&gt; as a built-in array method, while Go's approach is more explicit.&lt;/p&gt;

&lt;p&gt;In Go, we describe the elements before and after the position we want to remove, then combine them using &lt;code&gt;append()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lesson takeaway:&lt;/strong&gt; &lt;br&gt;
Slicing allows you to select portions of arrays and slices. By combining slicing with &lt;code&gt;append()&lt;/code&gt;, you can also remove elements from a slice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Copying Slices
&lt;/h2&gt;

&lt;p&gt;Before we discuss copying slices, let's revisit something we've already learnt. When you create a slice from an array, the slice provides a view into the array's data. Similarly, assigning one slice variable to another does not copy the underlying elements. It copies the slice value (descriptor).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
first := []int{10, 20, 30}&lt;/p&gt;

&lt;p&gt;second := first&lt;/p&gt;

&lt;p&gt;second[0] = 99&lt;/p&gt;

&lt;p&gt;fmt.Println(first)  // [99 20 30]&lt;br&gt;
fmt.Println(second) // [99 20 30]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;When we write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;pascal&lt;br&gt;
second := first&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
Go copies the slice descriptor, not the underlying elements, Meaning both &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;second&lt;/code&gt; still refer to the same underlying array.&lt;/p&gt;

&lt;p&gt;Therefore, modifying an element through &lt;code&gt;second&lt;/code&gt; also affects what we see through &lt;code&gt;first&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using copy() to Create Independent Slices
&lt;/h3&gt;

&lt;p&gt;If we want two slices whose elements can be modified independently, we can use Go's built-in &lt;code&gt;copy()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;copy()&lt;/code&gt; function copies elements from a source slice into a destination slice.&lt;/p&gt;

&lt;p&gt;However, the destination must have sufficient length if we want to copy all the source elements.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
first := []int{10, 20, 30}&lt;/p&gt;

&lt;p&gt;second := make([]int, len(first))&lt;/p&gt;

&lt;p&gt;copy(second, first)&lt;/p&gt;

&lt;p&gt;second[0] = 99&lt;/p&gt;

&lt;p&gt;fmt.Println(first)  // [10 20 30]&lt;br&gt;
fmt.Println(second) // [99 20 30]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's understand what happens:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create the original slice&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
first := []int{10, 20, 30}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
This creates our source slice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Create a destination slice&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
second := make([]int, len(first))&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We create another slice with the same length as &lt;code&gt;first&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;first&lt;/code&gt; has three elements, &lt;code&gt;second&lt;/code&gt; also has a length of three.&lt;/p&gt;

&lt;p&gt;At this point, &lt;code&gt;second&lt;/code&gt; contains three zero values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[0 0 0]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Copy the elements&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;plaintext&lt;br&gt;
copy(second, first)&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This copies the elements from &lt;code&gt;first&lt;/code&gt; into &lt;code&gt;second&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now both slices contain:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[10 20 30]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However, they have separate underlying arrays.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Modify the destination&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;python&lt;br&gt;
second[0] = 99&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This changes only the first element of &lt;code&gt;second&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When we print both slices:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
fmt.Println(first)  // [10 20 30]&lt;br&gt;
fmt.Println(second) // [99 20 30]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The original slice remains unchanged because &lt;code&gt;second&lt;/code&gt; has its own underlying array.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Many Elements Does copy() Copy?
&lt;/h3&gt;

&lt;p&gt;The number of elements copied depends on the lengths of the source and destination slices.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;copy()&lt;/code&gt; function copies the minimum of the two lengths.&lt;/p&gt;

&lt;p&gt;In other words:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Number of elements copied = min(len(destination), len(source))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
source := []int{10, 20, 30, 40}&lt;/p&gt;

&lt;p&gt;destination := make([]int, 2)&lt;/p&gt;

&lt;p&gt;n := copy(destination, source)&lt;/p&gt;

&lt;p&gt;fmt.Println(destination) // [10 20]&lt;br&gt;
fmt.Println(n)           // 2&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The source has a length of 4.&lt;/li&gt;
&lt;li&gt;The destination has a length of 2.&lt;/li&gt;
&lt;li&gt;Only two elements are copied.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;copy()&lt;/code&gt; returns &lt;code&gt;2&lt;/code&gt;, representing the number of elements copied.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The remaining source elements (&lt;code&gt;30&lt;/code&gt; and &lt;code&gt;40&lt;/code&gt;) are not copied because the destination only has room for two elements within its current length. &lt;code&gt;copy()&lt;/code&gt; does not automatically grow the destination slice. Its length determines how many elements can be copied into it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lesson Takeaway
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Assigning one slice to another copies the slice descriptor, not the underlying elements.&lt;/li&gt;
&lt;li&gt;Two slices assigned this way can share the same underlying array.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;copy()&lt;/code&gt; when you need independent element storage.&lt;/li&gt;
&lt;li&gt;The number of elements copied is the smaller of the source and destination lengths.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;copy()&lt;/code&gt; returns the number of elements successfully copied.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Nil Slices vs Empty Slices
&lt;/h2&gt;

&lt;p&gt;In Go, a nil slice and an empty slice can look similar when printed, but they are not the same. Let's look at both declarations.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Declaring a Nil Slice
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
var first []int&lt;/p&gt;

&lt;p&gt;fmt.Println(first)      // []&lt;br&gt;
fmt.Println(len(first)) // 0&lt;br&gt;
fmt.Println(cap(first)) // 0&lt;br&gt;
fmt.Println(first == nil) // true&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When we declare a slice using &lt;code&gt;var&lt;/code&gt; without initialising it, Go creates a nil slice.&lt;/p&gt;

&lt;p&gt;A nil slice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Has a length of zero.&lt;/li&gt;
&lt;li&gt;Has a capacity of zero.&lt;/li&gt;
&lt;li&gt;Does not currently reference an underlying array.&lt;/li&gt;
&lt;li&gt;Is equal to &lt;code&gt;nil&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Declaring an Empty Slice
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
second := []int{}&lt;/p&gt;

&lt;p&gt;fmt.Println(second)      // []&lt;br&gt;
fmt.Println(len(second)) // 0&lt;br&gt;
fmt.Println(cap(second)) // 0&lt;br&gt;
fmt.Println(second == nil) // false&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, we use an empty slice literal.&lt;/p&gt;

&lt;p&gt;Although the slice contains no elements, it is initialised and is not nil.&lt;/p&gt;

&lt;p&gt;An empty slice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Has a length of zero.&lt;/li&gt;
&lt;li&gt;Has a capacity of zero.&lt;/li&gt;
&lt;li&gt;Is not equal to &lt;code&gt;nil&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Comparing Nil and Empty Slices
&lt;/h3&gt;

&lt;p&gt;Let's put both declarations together:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
var first []int&lt;/p&gt;

&lt;p&gt;second := []int{}&lt;/p&gt;

&lt;p&gt;fmt.Println(first) // []&lt;br&gt;
fmt.Println(second) // []&lt;/p&gt;

&lt;p&gt;fmt.Println(first == nil)  // true&lt;br&gt;
fmt.Println(second == nil) // false&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Both slices print as &lt;code&gt;[]&lt;/code&gt;, and both have a length of zero.&lt;/p&gt;

&lt;p&gt;However, the difference is that the first is nil, while the second is an initialised empty slice.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Can You Append to Both?
&lt;/h3&gt;

&lt;p&gt;Yes!&lt;/p&gt;

&lt;p&gt;Both nil slices and empty slices can be used with &lt;code&gt;append()&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
var first []int&lt;br&gt;
second := []int{}&lt;/p&gt;

&lt;p&gt;first = append(first, 10)&lt;br&gt;
second = append(second, 20)&lt;/p&gt;

&lt;p&gt;fmt.Println(first)  // [10]&lt;br&gt;
fmt.Println(second) // [20]&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Go automatically handles the underlying storage when elements are appended.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Does the Difference Matter?
&lt;/h3&gt;

&lt;p&gt;The distinction between nil and empty slices can matter in a few situations:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checking whether a slice has been initialised&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can compare a slice to &lt;code&gt;nil&lt;/code&gt; to determine whether it is nil.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Returning data from functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A function might return a nil slice when there is no result, while another might return an initialised empty slice. These are different values even though both contain zero elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Encoding data into formats such as JSON&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The distinction can affect the output.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
var first []int&lt;br&gt;
second := []int{}&lt;/p&gt;

&lt;p&gt;firstJSON, _ := json.Marshal(first)&lt;br&gt;
secondJSON, _ := json.Marshal(second)&lt;/p&gt;

&lt;p&gt;fmt.Println(string(firstJSON))  // null&lt;br&gt;
fmt.Println(string(secondJSON)) // []&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A nil slice is encoded as &lt;code&gt;null&lt;/code&gt;, while an empty slice is encoded as an empty array (&lt;code&gt;[]&lt;/code&gt;). This can matter when building APIs because clients may treat &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;[]&lt;/code&gt; differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can You Compare Slices Directly?
&lt;/h3&gt;

&lt;p&gt;Go does not allow you to compare two slices directly using &lt;code&gt;==&lt;/code&gt; to check whether their elements are equal.&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`go&lt;br&gt;
first := []int{10, 20, 30}&lt;br&gt;
second := []int{10, 20, 30}&lt;/p&gt;

&lt;p&gt;// fmt.Println(first == second) // Compile-time error&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can, however, compare a slice to &lt;code&gt;nil&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;go&lt;br&gt;
fmt.Println(first == nil) // false&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you need to check whether two slices contain the same elements, you need to compare their elements or use an appropriate equality function.&lt;/p&gt;




&lt;h2&gt;
  
  
  General Lesson Takeaway: Arrays and Slices
&lt;/h2&gt;

&lt;p&gt;Arrays and slices are both used to store collections of elements of the same data type in Go. However, they behave differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrays
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Arrays have a fixed length.&lt;/li&gt;
&lt;li&gt;Their length is part of their type.&lt;/li&gt;
&lt;li&gt;Arrays store elements of the same data type.&lt;/li&gt;
&lt;li&gt;Arrays are value types, meaning assigning an array to another variable copies its elements.&lt;/li&gt;
&lt;li&gt;Their elements can be accessed and modified using indexes.&lt;/li&gt;
&lt;li&gt;Use arrays when you need a collection with a fixed, known size.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Slices
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Slices are flexible and can grow or shrink.&lt;/li&gt;
&lt;li&gt;Slices are built on top of underlying arrays.&lt;/li&gt;
&lt;li&gt;Multiple slices can share the same underlying array.&lt;/li&gt;
&lt;li&gt;Assigning one slice to another copies the slice descriptor, not the underlying elements.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;append()&lt;/code&gt; to add elements.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;copy()&lt;/code&gt; when you need to copy elements into independent storage.&lt;/li&gt;
&lt;li&gt;Use slicing expressions to select portions of arrays and slices.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;len()&lt;/code&gt; to get the current number of elements.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;cap()&lt;/code&gt; to get the available capacity.&lt;/li&gt;
&lt;li&gt;A nil slice and an empty slice both have a length of zero, but they are not the same.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Important Go Functions and Operations
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Function / Operation&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;len()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns the number of elements in an array or slice.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;cap()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns the capacity of a slice or array.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;append()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Adds elements to a slice and returns the resulting slice.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;copy()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Copies elements from one slice into another.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;make()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates and initialises a slice with a specified length and optional capacity.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;numbers[start:end]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates a slice from a selected range of elements.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Arrays vs Slices: Quick Comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Arrays&lt;/th&gt;
&lt;th&gt;Slices&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;Fixed&lt;/td&gt;
&lt;td&gt;Flexible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Length&lt;/td&gt;
&lt;td&gt;Part of the type&lt;/td&gt;
&lt;td&gt;Can change&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assignment&lt;/td&gt;
&lt;td&gt;Copies elements&lt;/td&gt;
&lt;td&gt;Copies slice descriptor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Underlying array&lt;/td&gt;
&lt;td&gt;The array itself&lt;/td&gt;
&lt;td&gt;Refers to an underlying array&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Adding elements&lt;/td&gt;
&lt;td&gt;Cannot grow&lt;/td&gt;
&lt;td&gt;Uses &lt;code&gt;append()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Length and capacity&lt;/td&gt;
&lt;td&gt;&lt;code&gt;len()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;len()&lt;/code&gt; and &lt;code&gt;cap()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Common use&lt;/td&gt;
&lt;td&gt;Fixed-size collections&lt;/td&gt;
&lt;td&gt;Everyday collections and application data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Think of an array as a fixed size container, A slice is a flexible view into an underlying array. You can create slices from arrays or other slices, append elements, copy elements into independent storage, and select different portions using slicing expressions.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>go</category>
      <category>programming</category>
    </item>
    <item>
      <title>GO: Introduction and core syntax</title>
      <dc:creator>Adetola Esther Aderibigbe</dc:creator>
      <pubDate>Thu, 22 Oct 2026 03:30:51 +0000</pubDate>
      <link>https://dev.to/adetolaesther/go-introduction-and-core-syntax-26ol</link>
      <guid>https://dev.to/adetolaesther/go-introduction-and-core-syntax-26ol</guid>
      <description>&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%2Fyr1dd9679snuuirh710g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyr1dd9679snuuirh710g.png" alt="Golang image" width="594" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Go (Golang)&lt;/strong&gt; is Google's statically typed, compiled, cross-platform, open-source programming language that combines efficiency with ease of use. Its features include built-in concurrency via goroutines and channels. It has simple syntax, fast compilation, and a comprehensive standard library. It's popular for microservices, web servers, and cloud-native development.&lt;/p&gt;

&lt;p&gt;Installation guide: &lt;a href="https://go.dev/doc/tutorial/getting-started#install" rel="noopener noreferrer"&gt;Getting Started with Go — Install Go&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Module One: Go Core Syntax
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Package declaration and Imports&lt;/li&gt;
&lt;li&gt;Variables and Types&lt;/li&gt;
&lt;li&gt;Control Flow and Loops: &lt;code&gt;for&lt;/code&gt; loops, &lt;code&gt;range&lt;/code&gt; loops, and &lt;code&gt;switch&lt;/code&gt; statements&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Package declaration and Imports
&lt;/h2&gt;

&lt;p&gt;A Go program is made of packages. Packages in Go are directories containing source files that work together to organize and reuse code. Every Go file belongs to a package, and that package is declared at the top of the file. A package like &lt;code&gt;fmt&lt;/code&gt; (for formatted I/O) is one you bring in with an import.&lt;/p&gt;

&lt;p&gt;A package can also be defined as a group of files that share one package name, usually in one folder. Every Go file starts with a package line that names it. A Go program starts in a package called main, and its function main is where the program starts running.&lt;/p&gt;

&lt;p&gt;A package works like a folder of recipes in a cookbook section. Every recipe card in the folder says which section it belongs to. The section that starts the meal is the one the cook opens first.&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="c"&gt;// package line&lt;/span&gt;
&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="c"&gt;// import line&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&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;Expected output: Hello, World!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;package main&lt;/code&gt; tells Go which package the file belongs to. The main package is special because it's the entry package for a program intended to run as an executable.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import "fmt"&lt;/code&gt; makes Go's standard library fmt package available to the file. It is used for formatted input and output, including printing text to the terminal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;func main()&lt;/code&gt; declares a function named main. When you run an executable program, execution begins in the main function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fmt.Println("Hello, World!")&lt;/code&gt; calls the Println function from fmt to print text, followed by a new line. The dot accesses an exported name from a package, and the parentheses contain the arguments you're passing to the function.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use Code That Belongs to Another Package (Import Another Package)
&lt;/h2&gt;

&lt;p&gt;Our file often needs code from another package, which is listed in the import line. The import statement makes a standard library package available to the file. After importing, you call its functions using the package name, a dot, and the function name, such as &lt;code&gt;fmt.Println&lt;/code&gt;. &lt;code&gt;fmt&lt;/code&gt; is the package, and &lt;code&gt;Println&lt;/code&gt; is the function inside 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;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="c"&gt;// Makes the standard library package available to the file.&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="c"&gt;// Declares the function where execution begins.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// Creates a variable named name as a string using the short variable declaration.&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;"Ada"&lt;/span&gt;

    &lt;span class="c"&gt;// Calls fmt.Println, which lives in the fmt package.&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;Expected output:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Naming Conventions in Go
&lt;/h2&gt;

&lt;p&gt;As an example, we have &lt;code&gt;fmt.Println("Train Name")&lt;/code&gt;. Under &lt;code&gt;fmt&lt;/code&gt;, we have the package name, and under &lt;code&gt;Println&lt;/code&gt;, we have the function name.&lt;/p&gt;

&lt;p&gt;Note: &lt;code&gt;Println&lt;/code&gt; starts with a capital letter. In Go, names beginning with a capital letter are exported and can be accessed from another package, while names beginning with a lowercase letter are unexported and can only be accessed within their own package. Go decides what other packages can use based on the first letter of a name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unused Imports and Compiler Errors
&lt;/h2&gt;

&lt;p&gt;Go checks imports strictly. If a file imports a package and doesn't use it, the compiler stops with an error. If the file uses a package without importing it, the compiler also stops. The import block is an honest list that shows what a file depends on, and every imported package must be used.&lt;/p&gt;

&lt;p&gt;Never use a package you haven't imported yet. Check whether an imported package is needed before importing it. You don't need to use every function inside an imported package, just one.&lt;/p&gt;

&lt;p&gt;Incorrect:&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="s"&gt;"strings"&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;name&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"ada"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go complains about &lt;code&gt;fmt&lt;/code&gt; and &lt;code&gt;strings&lt;/code&gt; being imported and not used. It also complains that &lt;code&gt;name&lt;/code&gt; is declared and not used, because Go is just as strict about unused variables.&lt;/p&gt;

&lt;p&gt;Correct:&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="s"&gt;"strings"&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;name&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"ada"&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;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToUpper&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;h2&gt;
  
  
  Go vs. JavaScript
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;JavaScript&lt;/th&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;const name = "Ada";&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;name := "Ada"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;console.log("Hello", name);&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;fmt.Println("Hello", name)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Lesson Takeaway
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Every Go file starts with a package line, and &lt;code&gt;package main&lt;/code&gt; with the &lt;code&gt;main&lt;/code&gt; function is where a program starts.&lt;/li&gt;
&lt;li&gt;An import statement brings in another package, and you can call its functions using the package name.&lt;/li&gt;
&lt;li&gt;A name starting with a capital letter is exported and can be used from other packages, while a lowercase name remains unexported.&lt;/li&gt;
&lt;li&gt;Go returns a compiler error for unused imports.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Variables and Types
&lt;/h2&gt;

&lt;p&gt;A variable is a named storage location in memory used to hold a value of a specific type. It is a named box that holds a value.&lt;/p&gt;

&lt;p&gt;In Go, variables are statically typed, meaning their type is known at compile time and cannot change.&lt;/p&gt;

&lt;p&gt;Variables are declared using the &lt;code&gt;var&lt;/code&gt; keyword, followed by the name, the type, and an optional value. The type says what kind of value the box can hold, and Go checks this. Any value written with the wrong type will be rejected by Go.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kitchen analogy:&lt;/strong&gt; There is a labelled jar in the kitchen. The label is the name, the jar's size and purpose are the type, while the contents are 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;var&lt;/span&gt; &lt;span class="n"&gt;itemCount&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;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; — keyword&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;itemCount&lt;/code&gt; — name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;int&lt;/code&gt; — type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;3&lt;/code&gt; — value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go also infers the type from the value. If you provide a value, you can leave out the type. In that case, &lt;code&gt;3&lt;/code&gt; becomes an integer (&lt;code&gt;int&lt;/code&gt;), &lt;code&gt;2.5&lt;/code&gt; becomes a float64, &lt;code&gt;"C"&lt;/code&gt; becomes a string, and true or false becomes a boolean.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The short assignment operator (:=)&lt;/strong&gt; is used to declare a variable in Go, with the type usually inferred from the assigned value. It can only be used inside a function. Variables declared outside a function must use the &lt;code&gt;var&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;var&lt;/code&gt; when you want to specify a type that cannot be inferred from the value, or when you want a zero value, which is the default value Go assigns to a variable when no value is provided. Inside a function, the short assignment operator (:=) is the usual choice for declaring a new variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go vs. JavaScript
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;JavaScript&lt;/th&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;let item = 3;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;itemCount := 3&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Type Inference in Go
&lt;/h3&gt;

&lt;p&gt;Type inference is Go working out a variable's type from the value you give it. Go determines a variable's type from the initial value you assign to it. You don't write the type; Go infers it from the value, and the variable has a fixed type. So, the variable can't later hold a different kind of value. For example, if &lt;code&gt;name&lt;/code&gt; is declared as a string using the short assignment operator (&lt;code&gt;:=&lt;/code&gt;), assigning &lt;code&gt;5&lt;/code&gt; to &lt;code&gt;name&lt;/code&gt; later would be incorrect because &lt;code&gt;name&lt;/code&gt; has already been defined as a string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kitchen analogy:&lt;/strong&gt; A shelf is labelled by the first item placed on it. If the first item placed on a shelf is a book, then it can't also be used to store a banana.&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="c"&gt;// Type Inference in Go&lt;/span&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="s"&gt;"fmt"&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// var declares itemCount with the type int and the value 3.&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;itemCount&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;3&lt;/span&gt;

    &lt;span class="c"&gt;// price := 29.5 declares a new variable. Go infers float64 from 29.5.&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;29.5&lt;/span&gt;

    &lt;span class="c"&gt;// name := "tea" declares a string. The := creates a new name here.&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;"tea"&lt;/span&gt;

    &lt;span class="c"&gt;// A plain = assigns to an existing variable. It does not declare a new one, so no := is needed.&lt;/span&gt;
    &lt;span class="n"&gt;itemCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;itemCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;

    &lt;span class="c"&gt;// Prints the three values in order: name, itemCount, and price.&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="n"&gt;itemCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&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;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tea 9 29.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How does this compare to TypeScript?
&lt;/h2&gt;

&lt;p&gt;If you're coming from TypeScript, this will feel familiar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tea&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;coffee&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Valid&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Type error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TypeScript infers that &lt;code&gt;name&lt;/code&gt; is a string, much like Go does. The difference is that TypeScript's types disappear once it's compiled to JavaScript, while Go's types are part of the compiled program. Both languages help catch certain mistakes before your program runs. Here's the same thing in Go:&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;"tea"&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;"coffee"&lt;/span&gt; &lt;span class="c"&gt;// Valid&lt;/span&gt;
&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;        &lt;span class="c"&gt;// Compile error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Go Variable Naming Rules
&lt;/h2&gt;

&lt;p&gt;Go variable names can be short, like x and y, or descriptive, like age and price. They must start with a letter or underscore, cannot start with a digit, and can contain only letters, digits, and underscores. Variable names are case-sensitive, cannot contain spaces or be Go keywords, and have no length limit. Multi-word variable names can use camel case (myVariableName), Pascal case (MyVariableName), or snake case (my_variable_name).&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson Takeaway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The var keyword is used to declare a variable in Go.&lt;/li&gt;
&lt;li&gt;The short variable declaration operator (:=) declares a variable with its type inferred from the assigned value.&lt;/li&gt;
&lt;li&gt;The short variable declaration operator (:=) can only be used inside a function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Control Flow and Loops
&lt;/h2&gt;

&lt;p&gt;A quick note before we start: the snippets in this section leave out &lt;code&gt;package main&lt;/code&gt; and &lt;code&gt;import "fmt"&lt;/code&gt; to keep things short. If you want to run one, add those two lines at the top of the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  for Loops
&lt;/h2&gt;

&lt;p&gt;Many languages have a separate keyword for each loop type, but Go doesn't. It has only &lt;code&gt;for&lt;/code&gt;, and what follows it decides the loop shape. The header sits between &lt;code&gt;for&lt;/code&gt; and the opening brace, and the body is the code that repeats.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kitchen analogy:&lt;/strong&gt; A kitchen timer can count down a set number of minutes. When it reaches zero, it stops and is ready to be turned off. One tool, 3 uses.&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="c"&gt;// i := 1 happens once, before any check.&lt;/span&gt;
    &lt;span class="c"&gt;// i &amp;lt;= 3 is checked before every pass, and the body only runs while it's true.&lt;/span&gt;
    &lt;span class="c"&gt;// i++ adds 1 after each pass.&lt;/span&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;1&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;3&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;for&lt;/code&gt; — keyword&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;i := 1&lt;/code&gt; — initialises i to 1&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;i &amp;lt;= 3&lt;/code&gt; — condition&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;i++&lt;/code&gt; — increments i by one&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;fmt.Println(i)&lt;/code&gt; — body, the code that repeats&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A typical &lt;code&gt;for&lt;/code&gt; loop has three parts, separated by semicolons.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Initial statement:&lt;/strong&gt; It runs once before the loop starts and usually creates a counter.
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Condition statement:&lt;/strong&gt; It is checked before each pass through the loop.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&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;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Post statement:&lt;/strong&gt; It runs after each pass and usually changes the counter.
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;p&gt;So, &lt;code&gt;i := 1&lt;/code&gt; starts the counter at one, the loop runs while &lt;code&gt;i&lt;/code&gt; is less than or equal to three, and &lt;code&gt;i++&lt;/code&gt; adds one after each pass.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of For Loops in Go
&lt;/h2&gt;

&lt;p&gt;There are three major types of loops in Go:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Classic loop&lt;/li&gt;
&lt;li&gt;Condition only loop&lt;/li&gt;
&lt;li&gt;Infinite loop&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Classic Loop
&lt;/h2&gt;

&lt;p&gt;We use a classic loop when we want to repeat something a known number of times or track a counter.&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;5&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;A classic loop has three parts: the initial statement, condition statement, and post statement. It runs five times in this example, printing 0 to 4.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Condition Only Loop
&lt;/h2&gt;

&lt;p&gt;This works like a while loop in JavaScript or TypeScript.&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;count&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;5&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;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;count&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;Note: There's no semicolon in the loop header. It has only a condition and runs while the condition is true.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Infinite Loop
&lt;/h2&gt;

&lt;p&gt;You use an infinite loop when you omit the condition. The loop continues indefinitely unless something inside stops it, usually with the keyword &lt;code&gt;break&lt;/code&gt; or &lt;code&gt;return&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="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;"Still running"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: &lt;code&gt;break&lt;/code&gt; stops the loop after the first print. An infinite loop has no condition and runs indefinitely until it is stopped.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go vs. JavaScript/TypeScript
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;th&gt;JavaScript&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Counter loop&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for i := 0; i &amp;lt; 3; i++ { }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for (let i = 0; i &amp;lt; 3; i++) { }&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Condition loop&lt;/td&gt;
&lt;td&gt;&lt;code&gt;for count &amp;lt; 3 { }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;while (count &amp;lt; 3) { }&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;while (true) { }&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Main difference: Go does not have a separate while keyword. It uses &lt;code&gt;for&lt;/code&gt; for all loop types, and what follows &lt;code&gt;for&lt;/code&gt; determines how the loop behaves.&lt;/p&gt;

&lt;h2&gt;
  
  
  Range Loop
&lt;/h2&gt;

&lt;p&gt;Writing &lt;code&gt;i := 0; i &amp;lt; 5; i++&lt;/code&gt; can honestly be a bit long sometimes. So Go gives us a shorter way to write it using &lt;code&gt;range&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="k"&gt;range&lt;/span&gt; &lt;span class="m"&gt;5&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 gives &lt;code&gt;i&lt;/code&gt; values from 0 to 4, printing each value on a new line.&lt;/p&gt;

&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
1
2
3
4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: &lt;code&gt;range 5&lt;/code&gt; iterates five times, producing values from 0 up to, but not including, 5. Ranging over a number like this needs Go 1.22 or newer, so if it doesn't compile, run &lt;code&gt;go version&lt;/code&gt; to check what you have installed.&lt;/p&gt;

&lt;p&gt;In a loop, &lt;code&gt;continue&lt;/code&gt; skips the rest of the current iteration and moves to the next one, while &lt;code&gt;break&lt;/code&gt; stops the whole loop.&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="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="k"&gt;range&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;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="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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
1
3
4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Lesson Takeaway
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Go has only one looping keyword, &lt;code&gt;for&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A classic loop has three parts, separated by semicolons: an initialisation statement that starts the counter, a condition statement that is checked before every pass, and a post statement that runs after every pass.&lt;/li&gt;
&lt;li&gt;A condition-only loop runs as long as the condition is true.&lt;/li&gt;
&lt;li&gt;An infinite loop runs indefinitely unless it is stopped using &lt;code&gt;break&lt;/code&gt; or &lt;code&gt;return&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A range loop provides a shorter way to iterate over values, such as &lt;code&gt;for i := range 5&lt;/code&gt;, which produces values from 0 to 4.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;continue&lt;/code&gt; skips the rest of the current iteration and moves to the next one, while &lt;code&gt;break&lt;/code&gt; stops the loop.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Route Logic Using Typed Switch Statements
&lt;/h2&gt;

&lt;p&gt;In JavaScript or TypeScript, there is a switch statement or an if-else statement. Go has both too. A switch statement in Go selects and executes one block of code from multiple possibilities based on the value or type of an expression. A switch statement provides a cleaner alternative to multiple if statements. It also supports both value-based and type-based branching.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;A switch statement tells Go which values to examine. Then, each case defines a possible value. Go runs the code under the matching case. If no case matches, it runs &lt;code&gt;default&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;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="s"&gt;"fmt"&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;day&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Monday"&lt;/span&gt;

    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;day&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;"Monday"&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;"Start of the week"&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;"Friday"&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;"Almost the weekend"&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;"Another day"&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;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start of the week
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Go supports two types of switch statements
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Expression switch&lt;/li&gt;
&lt;li&gt;Type switch&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. An Expression Switch
&lt;/h2&gt;

&lt;p&gt;An expression switch evaluates an expression and executes the matching case block. This is useful when you have several possible options.&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="c"&gt;// Expression switch: evaluate the value, find a match,&lt;/span&gt;
&lt;span class="c"&gt;// execute the matching case, and exit the switch.&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;route&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"/profile"&lt;/span&gt;

    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;route&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;"/"&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;"Home page"&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;"/dashboard"&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;"Dashboard page"&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;"/profile"&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;"Profile page"&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;"Page not found"&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;An expression switch can also check more than one value in a single case. Just separate the values with a comma.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;

    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&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;"Finally an adult"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="c"&gt;// two values, one case&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;"How's life?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="m"&gt;35&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;"Oldie"&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;"Another age"&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;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How's life?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value in the switch and the values in the case expressions must be comparable under Go's type rules. E.g., an integer variable cannot be compared directly against a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;

    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;age&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;"25"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="c"&gt;// Error: cannot compare int with string&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;"Twenty-five"&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;This code produces a compile-time error because &lt;code&gt;age&lt;/code&gt; is an integer (&lt;code&gt;int&lt;/code&gt;), while &lt;code&gt;"25"&lt;/code&gt; is a string. Go does not allow these incompatible types to be compared directly in a switch statement. The fix is to make both sides the same type, like &lt;code&gt;case 25:&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Go also allows you to omit the value after &lt;code&gt;switch&lt;/code&gt;. Each case can then contain a Boolean condition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;75&lt;/span&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;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;80&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;"Excellent"&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;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;60&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;"Passed"&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;"Try again"&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;Go evaluates the cases from top to bottom and executes the first case whose condition is true.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Type Switch
&lt;/h2&gt;

&lt;p&gt;A switch checks the value, while a type switch checks what type a value is. This is useful when a variable has an interface type, such as &lt;code&gt;any&lt;/code&gt;, and the value inside can be any data type. In Go, &lt;code&gt;any&lt;/code&gt; is an alias for the empty interface, and it can hold a value of any 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;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="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;

    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;:=&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="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kt"&gt;string&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 a string:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kt"&gt;int&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 an integer:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kt"&gt;bool&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 a Boolean:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&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 type"&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;ul&gt;
&lt;li&gt;
&lt;code&gt;value&lt;/code&gt; — a variable of type any.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.(type)&lt;/code&gt; — the syntax used to inspect the value's type in a type switch.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;v&lt;/code&gt; — a variable declared in the switch that refers to the value inside each case, with its specific type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;case string&lt;/code&gt; — when the value is a string, v is a string.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;case int&lt;/code&gt; — when the value is an integer, v is an integer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Expression Switch vs. Type Switch
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Expression switch&lt;/th&gt;
&lt;th&gt;Type switch&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What it checks&lt;/td&gt;
&lt;td&gt;A value or Boolean condition&lt;/td&gt;
&lt;td&gt;The type of a value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typical use&lt;/td&gt;
&lt;td&gt;Routes, selectors, and codes&lt;/td&gt;
&lt;td&gt;Values whose types may differ&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Example&lt;/td&gt;
&lt;td&gt;&lt;code&gt;switch route&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;switch v := value.(type)&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cases&lt;/td&gt;
&lt;td&gt;Values or Boolean conditions&lt;/td&gt;
&lt;td&gt;Types such as string, int, etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Matching&lt;/td&gt;
&lt;td&gt;Executes the matching case&lt;/td&gt;
&lt;td&gt;Executes the case matching the value's type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Type handling&lt;/td&gt;
&lt;td&gt;Cases must be compatible with the switch expression's type&lt;/td&gt;
&lt;td&gt;Go identifies the value's type in each case&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Go vs. JavaScript
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Go&lt;/th&gt;
&lt;th&gt;JavaScript&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Syntax&lt;/td&gt;
&lt;td&gt;&lt;code&gt;switch expression { }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;switch (expression) { }&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Case behaviour&lt;/td&gt;
&lt;td&gt;Exits the switch automatically after a matching case (use &lt;code&gt;fallthrough&lt;/code&gt; if you want it to continue)&lt;/td&gt;
&lt;td&gt;Requires &lt;code&gt;break&lt;/code&gt; to prevent fall-through&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Lesson Takeaway
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Use an expression switch when one value can have several possible outcomes.&lt;/li&gt;
&lt;li&gt;Use a type switch when you need to identify a value's type.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;case&lt;/code&gt; to define options and &lt;code&gt;default&lt;/code&gt; to handle anything unmatched.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Final Practice Exercise
&lt;/h2&gt;

&lt;p&gt;Create a small program that combines what you've learned.&lt;/p&gt;

&lt;p&gt;Your program should:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declare a variable called &lt;code&gt;status&lt;/code&gt; and assign it &lt;code&gt;"pending"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use an expression switch to print a message for &lt;code&gt;"pending"&lt;/code&gt;, &lt;code&gt;"approved"&lt;/code&gt;, or &lt;code&gt;"rejected"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Print &lt;code&gt;"Unknown status"&lt;/code&gt; for any other value.&lt;/li&gt;
&lt;li&gt;Use a &lt;code&gt;for&lt;/code&gt; loop with &lt;code&gt;range 5&lt;/code&gt; to print the numbers zero through four, but skip the number two using &lt;code&gt;continue&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Next Module: Data Structures&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>A Beginner’s Guide to ThreeJs</title>
      <dc:creator>Adetola Esther Aderibigbe</dc:creator>
      <pubDate>Fri, 13 Feb 2026 11:10:13 +0000</pubDate>
      <link>https://dev.to/adetolaesther/a-beginners-guide-to-threejs-ob5</link>
      <guid>https://dev.to/adetolaesther/a-beginners-guide-to-threejs-ob5</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qdmnqxhtflnlep6qjr3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qdmnqxhtflnlep6qjr3.png" alt=" " width="310" height="162"&gt;&lt;/a&gt;Sometime ago,I was told me that no matter what you’re learning in tech whether it’s a new method, a new language, a new framework, or a new tool you can always approach it by asking three questions: What does it take? What does it return? And what does it do?&lt;/p&gt;

&lt;p&gt;Unfortunately, with Three.js, this is the first time that approach has completely failed me, After three trails however, I'm writing this as an intro to threejs for those that are struggling with it just like me.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is ThreeJs
&lt;/h2&gt;

&lt;p&gt;Three.js is a high-level JavaScript library for creating 3D graphics in web browsers. It simply simplifies 3D creation and animation without any WebGL code knowledge.&lt;/p&gt;

&lt;p&gt;To render 3D graphics in a browser, we need the help of the GPU(Graphic processing unit)the part of your computer that’s really good at drawing graphics.&lt;br&gt;
WebGL is the tool that lets JavaScript talk to the GPU. It acts like a translator, helping the browser tell the GPU what to render on the screen. &lt;br&gt;
Threejs is built on top of webgl, it abstracts the complicated webGL language into an easy to use javascript Api so we reallyy don't have to bother again that.&lt;br&gt;
To understand or use Three.js, it’s important to know that everything is built around three core objects. These three objects form the foundation of the Three.js workflow:&lt;br&gt;
    1.  Scene&lt;br&gt;
    2.  Camera&lt;br&gt;
    3.  Renderer&lt;br&gt;
In this tutorial, we’ll explore these three core concepts while building something small along the way. This hands on approach will help make each concept easier to understand as we go but before we begin, the first step is installation by running this in our terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# three.js
npm install --save three

# vite
npm install --save-dev vite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;A scene in Three.js can be thought of as the world where everything exists. It contains everything you want to see(objects, lights, and anything else that should appear on the screen).&lt;br&gt;
A good way to think about a scene is like a movie set. The set holds all the props, actors, and lights. Without the set, there’s nothing to film.&lt;br&gt;
In the same way, before we can display anything with Three.js, we must first create a scene. Every visible object in Three.js lives inside a scene&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as THREE from 'three';

const scene = new THREE.Scene();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it.&lt;br&gt;
At this point, we've created an empty world. Nothing will show on the screen yet because the scene has no objects,no camera and nothing is being rendered So we need the camera.&lt;/p&gt;
&lt;h2&gt;
  
  
  2.Camera
&lt;/h2&gt;

&lt;p&gt;The camera is the viewer's eye. It is used to look at everything inside the scene. on our movie set, the camera is literally the cameraman. And there are multiple camera types. Three.js provides several types of cameras, including:&lt;br&gt;
    • PerspectiveCamera&lt;br&gt;
    • OrthographicCamera&lt;br&gt;
    • CubeCamera&lt;br&gt;
    • ArrayCamera&lt;br&gt;
    • StereoCamera&lt;/p&gt;

&lt;p&gt;However, the most common is the PerspectiveCamera which is designed to mimic how the human eye sees the world, where objects farther away appear smaller and objects closer appear larger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;a PerspectiveCamera takes 3 arguments, each controlling how the scene is viewed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Field of View (FOV)&lt;br&gt;
This controls how wide the camera can see.It is measured in degrees and usually represents the vertical viewing angle.&lt;br&gt;
A larger value means you see more of the scene, while a smaller value zooms in closer.(ours is 75)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Aspect Ratio&lt;br&gt;
This is the shape of the camera’s view and is usually based on the browser window.&lt;br&gt;
It is calculated by dividing the window’s width by its height:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.innerWidth / window.innerHeight

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

&lt;/div&gt;



&lt;p&gt;The last two are &lt;br&gt;
a. Near Clipping Plane&lt;br&gt;
This defines how close an object can be to the camera before it is no longer visible.&lt;/p&gt;

&lt;p&gt;b. Far Clipping Plane&lt;br&gt;
This defines how far an object can be from the camera before it disappears.&lt;/p&gt;

&lt;p&gt;Together, the near and far values create the view frustum, which determines what the camera can and cannot see.&lt;/p&gt;

&lt;p&gt;For example, using  0.1 and 1000 means everything is seen frim the camera lens.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Renderer
&lt;/h2&gt;

&lt;p&gt;The renderer is what actually draws everything on the screen. You can think of it like a painter, it takes all the objects in the scene, looks through the camera, and paints the final image onto your browser. In Three.js, the most commonly used renderer is the WebGLRenderer. When creating it, you need to tell it where to display the scene in the browser. Usually, this is done by attaching it to a DOM element, like the  or a specific &lt;/p&gt;.&lt;br&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as THREE from 'three';

// Create the renderer
const renderer = new THREE.WebGLRenderer();

// Set the size of the renderer (usually the window size)
renderer.setSize(window.innerWidth, window.innerHeight);

// Add the renderer to the HTML document
document.body.appendChild(renderer.domElement);
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Now that we have a scene, a camera, and a renderer, let’s add a 3D object. In Three.js, creating an object happens in three main steps&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Create the geometry
&lt;/h2&gt;

&lt;p&gt;The geometry defines the shape of our object. It’s basically the XYZ points that make up the object in 3D space.&lt;/p&gt;

&lt;p&gt;Three.js has a bunch of built-in geometries, like cubes, spheres, cones, and planes that can be seen in the docs. For this tutorial, we’ll use a sphere:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const geometry = new THREE.SphereGeometry(15, 32, 16);

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



&lt;p&gt;15 is the radius of the sphere&lt;/p&gt;

&lt;p&gt;32 is the number of horizontal segments&lt;/p&gt;

&lt;p&gt;16 is the number of vertical segments&lt;/p&gt;

&lt;p&gt;This gives us a smooth-looking sphere.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2. Create the material

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



&lt;p&gt;The material defines how the geometry looks. we can think of it as the wrapping paper for your 3D shape.&lt;/p&gt;

&lt;p&gt;Three.js has many materials that can aslo been seen in their docs. Some respond to light (like MeshStandardMaterial), while others don’t. For simplicity, we’ll use MeshBasicMaterial, which does not rely on lights:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const material = new THREE.MeshBasicMaterial({
  color: 0xf9a8d4,
  wireframe: true,
});

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



&lt;p&gt;color: sets the color of the sphere&lt;/p&gt;

&lt;p&gt;wireframe: true makes the sphere display as a wireframe, so you can see its structure&lt;/p&gt;

&lt;p&gt;3.Creating a mesh&lt;br&gt;
A mesh is created by combining geometry and material. The mesh is the actual object that can be added to the scene.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sphere = new THREE.Mesh(geometry, material);
sphere.position.set(8, -30, 0)
scene.add(sphere);
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Now the sphere exists in the scene, ready to be rendered.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Animate the sphere &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We can make the sphere spin to make the scene more dynamic:&lt;/p&gt;

&lt;p&gt;const animate = () =&amp;gt; {&lt;br&gt;
    requestAnimationFrame(animate);&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sphere.rotation.x += 0.01;
sphere.rotation.y += 0.005;
sphere.rotation.z += 0.01;

renderer.render(scene, camera);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;animate();&lt;/p&gt;

&lt;p&gt;At this point, you have a 3D animated sphere rotating on your screen! Of course, there’s much more to Three.js than just this. It’s used in interactive websites, games, simulations, and a whole lot more but if you’ve struggled with Three.js like I have, congratulations, you’ve just created your first 3D object. From here, you can, Add lights to your scene, Experiment with materials, colors, and geometries and Play around with what you’ve built and see how changes affect your scene.&lt;/p&gt;

&lt;p&gt;Also, check out the examples in the official &lt;a href="https://threejs.org/docs/" rel="noopener noreferrer"&gt;Three.js documentation &lt;/a&gt;they’re a great source of inspiration and learning and maybe once I’ve mastered Three.js, I’ll come back with a more detailed tutorialand this time, we might build something even more exciting.&lt;/p&gt;

</description>
      <category>threejs</category>
      <category>animation</category>
    </item>
    <item>
      <title>Some cool Web APIs</title>
      <dc:creator>Adetola Esther Aderibigbe</dc:creator>
      <pubDate>Tue, 28 Oct 2025 07:48:34 +0000</pubDate>
      <link>https://dev.to/adetolaesther/some-cool-web-apis-20m2</link>
      <guid>https://dev.to/adetolaesther/some-cool-web-apis-20m2</guid>
      <description>&lt;p&gt;Last week, I decided to switch up how I learn. Instead of just reading tutorials and documentation, I tried something simple, asking three core questions about every new concept I came across, a technique recommended by my mentor.&lt;/p&gt;

&lt;p&gt;Those three questions were:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What does it do? (What’s its purpose or behavior?)&lt;/li&gt;
&lt;li&gt;What does it take? (What inputs, dependencies, or setup does it require?)&lt;/li&gt;
&lt;li&gt;What does it return? (What’s the output or result you can expect?)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This technique forces clarity and Using this approach, here’s what I explored last week.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Interception Observer
&lt;/h2&gt;

&lt;h2&gt;
  
  
  What does it do?
&lt;/h2&gt;

&lt;p&gt;interception Observer is used to detect interaction between the target element and its ancestor element. It is used to detect if some elements are visible at viewports or not. &lt;br&gt;
 It provides an efficient way to track visibility changes without constantly listening to scroll or resize events.&lt;/p&gt;
&lt;h2&gt;
  
  
  What does it take?
&lt;/h2&gt;

&lt;p&gt;An Intersection Observer takes two main inputs:&lt;/p&gt;

&lt;p&gt;1) A callback function &lt;br&gt;
 This function is triggered whenever the visibility of the observed element changes. It receives a list of IntersectionObserverEntry objects, which contain information about each observed target.&lt;br&gt;
 If an entry’s isIntersecting property is true, it means the target element is visible within the viewport or within the specified root element.&lt;/p&gt;

&lt;p&gt;2) An options object &lt;br&gt;
This object can include the following properties:&lt;br&gt;
 a) root: Defines the element that is used as the viewport for checking the visibility of the target. It must be an ancestor of the target element. If not specified, the browser’s viewport is used by default.&lt;br&gt;
 b) threshold (sometimes misheard or mistyped as choiceOde): Can be a single number or an array of numbers between 0 and 1. It specifies how much of the target element must be visible before the callback is triggered.&lt;br&gt;
Example: 0.5 means the callback fires when half of the element is visible; 1 means the callback fires only when the entire element is visible. The default value is 0.&lt;br&gt;
c) rootMargin: Works similarly to CSS margins. It accepts one to four values (margin-top, margin-right, margin-bottom, margin-left) and can be used to expand or shrink the root’s bounding box. For example, "0px 0px -50px 0px" can trigger earlier or later than the actual visibility point.&lt;/p&gt;
&lt;h2&gt;
  
  
  What does it return?
&lt;/h2&gt;

&lt;p&gt;It returns an observer object that provides methods such as  .observe() and .unobserve(), allowing you to start or stop observing specific target elements for visibility changes.&lt;/p&gt;
&lt;h2&gt;
  
  
  When to use Intersection Observer
&lt;/h2&gt;

&lt;p&gt;Intersection Observers are useful in many real world web interactions, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lazy loading images – Only load images when they’re about to appear on the screen.&lt;/li&gt;
&lt;li&gt;Detecting visibility – Check whether a specific element is within the viewport.&lt;/li&gt;
&lt;li&gt;Autoplay or pause media – Automatically play a video when visible and pause when it scrolls out of view.&lt;/li&gt;
&lt;li&gt;Infinite scrolling – Load more content when the user reaches the end of a page or list.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2.createElement()
&lt;/h2&gt;
&lt;h2&gt;
  
  
  What does it do?
&lt;/h2&gt;

&lt;p&gt;In an HTML document, the &lt;code&gt;document.createElement()&lt;/code&gt; method is used to dynamically create new HTML elements using JavaScript. Instead of hardcoding everything inside your HTML file, this method lets you generate elements on the fly and attach them to the DOM. JavaScript dynamically builds and updates the interface by creating, modifying, and removing elements without reloading the page.&lt;br&gt;
 This makes the web app more efficient and interactive, as it allows constructing UI components directly with JavaScript rather than writing repetitive static HTML.&lt;/p&gt;
&lt;h2&gt;
  
  
  What does it take?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;createElement()&lt;/code&gt; method takes one argument, a string representing the tag name of the element you want to create.&lt;br&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;const button = document.createElement('button');

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

&lt;/div&gt;



&lt;p&gt;Here, a  element is created in memory but not yet added to the page.&lt;/p&gt;


&lt;h2&gt;
  
  
  What does it return?
&lt;/h2&gt;

&lt;p&gt;the new element.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Canva API
&lt;/h2&gt;

&lt;p&gt;The Canvas API provides a means for drawing graphics via JavaScript and the HTML &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; element. Among other things, it can be used for animation, game graphics, data visualization, photo manipulation, and real-time video processing.&lt;/p&gt;

&lt;p&gt;The Canvas API largely focuses on 2D graphics allowing developers to create visual elements dynamically including animations, charts, game graphics, image effects, and even real-time video processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does it take?
&lt;/h2&gt;

&lt;p&gt;To use the Canvas API, you typically need:&lt;br&gt;
a) An HTML  element: This defines the drawing area in your document. For example:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;canvas id="myCanvas" width="400" height="400"&amp;gt;&amp;lt;/canvas&amp;gt;

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



&lt;p&gt;b) A rendering context &lt;br&gt;
In JavaScript, you access the drawing functions by retrieving the context&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const canvas = document.getElementById('myCanvas');&lt;br&gt;
const ctx = canvas.getContext('2d');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The getContext('2d') method gives you the 2D drawing context, which contains all the methods needed to draw shapes, text, images, and more.&lt;/p&gt;

&lt;p&gt;c) Drawing instructions &lt;br&gt;
Once you have the context, you can call methods like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ctx.fillRect(x, y, width, height) – Draws a filled rectangle.&lt;/li&gt;
&lt;li&gt;ctx.beginPath() and ctx.lineTo() – Used to create complex paths and shapes.&lt;/li&gt;
&lt;li&gt;ctx.drawImage() – Renders images or videos onto the canvas.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What does it return?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Channel Messaging API&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  What does it do?
&lt;/h2&gt;

&lt;p&gt;The Channel Messaging API allows two separate scripts running in different browsing contexts (such as two iframes or an iframe and its parent document) to communicate directly with each other.&lt;br&gt;
It creates a two-way communication channel, where each side can send and receive messages asynchronously through dedicated message ports. &lt;br&gt;
 This makes it useful for scenarios where web components, embedded widgets, or isolated scripts need to share data safely and efficiently without relying on global variables or storage.&lt;/p&gt;
&lt;h2&gt;
  
  
  What does it take?
&lt;/h2&gt;

&lt;p&gt;A message channel is created using the &lt;code&gt;MessageChannel()&lt;/code&gt; constructor. Once created, the two ends of the channel can be accessed through: &lt;code&gt;MessageChannel.port1&lt;/code&gt; and &lt;code&gt;MessageChannel.port2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The context that creates the channel uses port1, while the second context (such as an iframe) uses port2.&lt;/p&gt;

&lt;p&gt;You can send messages through &lt;code&gt;port2.postMessage()&lt;/code&gt; and transfer the port itself to another browsing context using window.postMessage() along with two arguments(the message to send, the object to transfer ownership of)&lt;br&gt;
The receiving context listens for messages using &lt;code&gt;onmessage&lt;/code&gt;, and grabs the message content from event's data.&lt;br&gt;
To reply, it can use its own &lt;code&gt;MessagePort.postMessage()&lt;/code&gt; method.&lt;br&gt;
When communication is no longer needed, &lt;code&gt;MessagePort.close()&lt;/code&gt; can be called to close the connection on both ends.&lt;/p&gt;
&lt;h2&gt;
  
  
  What does it return?
&lt;/h2&gt;

&lt;p&gt;a communication bridge&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Geolocation API&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  What does it do?
&lt;/h2&gt;

&lt;p&gt;The Geolocation API allows users to share their physical location with web applications if they choose to.&lt;br&gt;
For privacy and security reasons, the browser always asks for the user’s permission before sharing any location data. Once granted, the API enables a web app to access the device’s geographic position, using the best available method (such as GPS, Wi-Fi, or network information).&lt;/p&gt;

&lt;p&gt;For browser extensions, any feature that accesses geolocation data must explicitly declare the "geolocation" permission in the manifest file.&lt;/p&gt;
&lt;h2&gt;
  
  
  What does it take?
&lt;/h2&gt;

&lt;p&gt;The Geolocation API takes:&lt;/p&gt;

&lt;p&gt;1 User permission &lt;br&gt;
 The browser automatically prompts the user to grant or deny location access before the API can return any data.&lt;br&gt;
2 Callback functions &lt;br&gt;
 The API works asynchronously and requires callback functions to handle success or error responses.&lt;/p&gt;

&lt;p&gt;The Geolocation API is accessed via a call to navigator.geolocation; this will cause the user's browser to ask them for permission to access their location data. If they accept, then the browser will use the best available functionality on the device to access this information. Developer can now access this location information in a couple of different ways:&lt;/p&gt;

&lt;p&gt;a) &lt;code&gt;Geolocation.getCurrentPosition()&lt;/code&gt;: Retrieves the device's current location.&lt;br&gt;
b) &lt;code&gt;Geolocation.watchPosition()&lt;/code&gt;: Registers a handler function that will be called automatically each time the position of the device changes, returning the updated location.&lt;/p&gt;

&lt;p&gt;Each of these methods can take up to three arguments:&lt;br&gt;
1 A success callback (required) executed when location retrieval succeeds. It receives a &lt;code&gt;GeolocationPosition&lt;/code&gt; object containing data such as latitude, longitude, and accuracy.&lt;br&gt;
2 An error callback (optional)  executed if retrieval fails. It receives a &lt;code&gt;GeolocationPositionError&lt;/code&gt; object explaining what went wrong (e.g., permission denied, timeout, or unavailable data).&lt;br&gt;
3 An options object (optional) that allows developers to configure retrieval settings such as accuracy, timeout, and caching behavior.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;navigator.geolocation.getCurrentPosition(
  (position) =&amp;gt; {
    console.log(position.coords.latitude, position.coords.longitude);
  },
  (error) =&amp;gt; {
    console.error(error);
  },
  { enableHighAccuracy: true, timeout: 5000 }
);

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



&lt;h2&gt;
  
  
  What does it return?
&lt;/h2&gt;

&lt;p&gt;The Geolocation API returns a Geolocation object, which provides access to the methods and properties needed to retrieve and monitor location data.&lt;br&gt;
When successfully executed, it provides a GeolocationPosition object containing detailed information such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;coords.latitude&lt;/li&gt;
&lt;li&gt;coords.longitude&lt;/li&gt;
&lt;li&gt;coords.accuracy&lt;/li&gt;
&lt;li&gt;timestamp&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  When to use Geolocation API
&lt;/h2&gt;

&lt;p&gt;Geolocation API can be useful when building&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Weather apps&lt;/li&gt;
&lt;li&gt;Maps and navigation&lt;/li&gt;
&lt;li&gt;Ride-hailing or delivery apps&lt;/li&gt;
&lt;li&gt;Logistics and fleet management &lt;/li&gt;
&lt;li&gt;Fitness and outdoor activity apps &lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Storage API&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  What does it do?
&lt;/h2&gt;

&lt;p&gt;The Storage API provides a way for web applications to store data locally on a user’s device directly in the browser so that information can persist between sessions without needing a server connection.&lt;br&gt;
It allows developers to save key value data, user preferences, and small amounts of application state, making websites faster and more interactive even when offline.&lt;br&gt;
There are two main parts of the Storage API:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;localStorage – Stores data permanently (until explicitly cleared by the user or the site).&lt;/li&gt;
&lt;li&gt;sessionStorage – Stores data temporarily for a single session (it’s cleared once the browser tab is closed).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  What does it take?
&lt;/h2&gt;

&lt;p&gt;The Storage API takes: A key and a value. Data is stored in key value pairs, where both are strings.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;localStorage.setItem('username', 'Adetola');&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Here, 'username' is the key, and 'Adetola' is the value being stored.&lt;/p&gt;

&lt;p&gt;A key (for retrieval or removal) – &lt;br&gt;
To read stored data:&lt;br&gt;
&lt;code&gt;const name = localStorage.getItem('username');&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
To remove data:&lt;br&gt;
&lt;code&gt;localStorage.removeItem('username');&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Or to clear everything at once:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;localStorage.clear();&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What does it return?
&lt;/h2&gt;

&lt;p&gt;The Storage API itself returns a Storage object, which provides methods (setItem(), getItem(), removeItem(), clear()) for interacting with stored data.&lt;/p&gt;

&lt;p&gt;When data is retrieved using getItem(), it returns the string value associated with the specified key. If the key doesn’t exist, it returns null.&lt;/p&gt;
&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt; &lt;a href="https://www.geeksforgeeks.org/javascript/introduction-to-intersection-observer/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/javascript/introduction-to-intersection-observer/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/javascript/html-dom-createelement-method/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/javascript/html-dom-createelement-method/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Channel_Messaging_API/Using_channel_messaging" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Channel_Messaging_API/Using_channel_messaging&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


</description>
    </item>
    <item>
      <title>SEO and Next.js: How to Implement and Optimize for Better Results</title>
      <dc:creator>Adetola Esther Aderibigbe</dc:creator>
      <pubDate>Tue, 28 Oct 2025 06:29:28 +0000</pubDate>
      <link>https://dev.to/adetolaesther/seo-and-nextjs-how-to-implement-and-optimize-for-better-results-54pj</link>
      <guid>https://dev.to/adetolaesther/seo-and-nextjs-how-to-implement-and-optimize-for-better-results-54pj</guid>
      <description>&lt;h2&gt;
  
  
  What is SEO?
&lt;/h2&gt;

&lt;p&gt;SEO stands for Search Engine Optimization. It is the process of making a website more visible in search results to increase both the quality and quantity of traffic. Unlike paid ads, SEO focuses on organic traffic, making websites easier for users to find naturally through search engines like Google and Bing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is SEO Important?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Organic search dominates traffic: Around 50% of website traffic comes from search engines. Unlike paid advertising which may be shown to people who aren’t actively looking. organic search happens when a user types a specific query. By ranking for those keywords, your website positions itself as the solution to their problem, leading to higher quality leads and better conversion rates.
2.Search is a daily habit: Over 8.5 billion searches happen on Google every day. Search is deeply integrated into people’s lives. Businesses without a strong organic presence risk being invisible to their audience. Since most searches now happen on mobile, Google prioritizes mobile-friendly websites. This means businesses need fast, responsive, user-friendly websites to perform well in search rankings and build a strong brand perception.&lt;/li&gt;
&lt;li&gt;It’s sustainable: Unlike paid ads that stop driving traffic once the campaign ends, SEO continues to work long term, making it a more sustainable strategy.&lt;/li&gt;
&lt;li&gt;It builds trust: Ranking high on Google is a signal of credibility. Users tend to trust organic results more than ads, so appearing at the top transfers that trust to your brand.&lt;/li&gt;
&lt;li&gt;It outperforms paid ads in the long run While ads can provide a short-term boost, SEO offers long-term visibility and trust that ads cannot match.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Implementing SEO in Next.js
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Metadata Management
The easiest way to start with SEO is by managing your Html metadata using the next/head document. Metadata is like your page title and description, is crucial for SEO. It's the first thing search engines see and what appears in search results. Next.js provides a straightforward way to handle this using the metadata object or the generateMetadata function (for App Router) or the next/head component (for Pages Router).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Static Metadata: For pages with content that doesn't change, you can define a static metadata object. This is ideal for things like your homepage, "about us" page, or a static landing page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import type { Metadata } from 'next'

 export const metadata: Metadata = {
   title: 'My Awesome Website',
   description: 'The best website for all your needs.',
 }

 export default function Page() {
   //...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dynamic Metadata: For dynamic pages, like blog posts or product pages, you'll need to generate metadata based on the content. The generateMetadata function allows you to fetch data and use it to create unique titles and descriptions for each page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import type { Metadata, ResolvingMetadata } from 'next'

type Props = {
  params: { slug: string }
}

export async function generateMetadata(
  { params }: Props,
  parent: ResolvingMetadata
): Promise&amp;lt;Metadata&amp;gt; {
  // Fetch data for the specific blog post
  const post = await fetch(`https://.../posts/${params.slug}`).then((res) =&amp;gt; res.json())

  return {
    title: post.title,
    description: post.description,
  }
}

export default function Page({ params }: Props) {
  //...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Optimizing SEO in Next.js
&lt;/h2&gt;

&lt;p&gt;SEO in Next.js goes beyond metadata—it’s about leveraging the framework’s built-in features for speed, crawlability, and user experience.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rendering Strategies
Choosing the right rendering method is crucial for SEO:&lt;/li&gt;
&lt;li&gt;Static Site Generation (SSG): Pages are pre-rendered at build time into static HTML. This makes them fast-loading and ideal for content that doesn’t change often (e.g., landing pages).&lt;/li&gt;
&lt;li&gt;Server-Side Rendering (SSR): Pages are rendered on the server for each request. This is best for highly dynamic, user-specific content, like a product inventory or a profile page. The advantage is that crawlers still see the complete HTML.&lt;/li&gt;
&lt;li&gt;Incremental Static Regeneration (ISR): A hybrid approach that combines SSG and SSR. Pages are generated statically but can be updated at intervals or when data changes. This offers both speed and freshness.&lt;/li&gt;
&lt;li&gt;Image and Font Optimization
Page speed is a major SEO factor, and Next.js helps here:&lt;/li&gt;
&lt;li&gt;Automatic image optimization (resizing images based on device and lazy-loading them only when they appear in view).&lt;/li&gt;
&lt;li&gt;Built-in font optimization, reducing load time and improving Core Web Vitals.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Server-Side Rendering (SSR)
&lt;/h2&gt;

&lt;p&gt;The delivery of content from a server to a user’s browser is a foundational challenge in web development. The architectural approach chosen directly shapes performance, user experience, and business outcomes.&lt;br&gt;
For a while now, the debate has centered on two core strategies: &lt;strong&gt;Server-Side Rendering (SSR)&lt;/strong&gt; and &lt;strong&gt;Client-Side Rendering (CSR)&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SSR generates a complete HTML page on the server and delivers it ready for immediate display in the browser.&lt;/li&gt;
&lt;li&gt;CSR, by contrast, sends a minimal HTML shell and relies on JavaScript in the browser to construct the page.
At their core, these strategies represent a fundamental trade-off: should the computational workload be centralized on the server or distributed across user devices?
In this section, we’ll focus on SSR.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is SSR?
&lt;/h2&gt;

&lt;p&gt;Server-Side Rendering (SSR) is a technique where a web page’s HTML is generated on the server in response to a user’s request.&lt;br&gt;
When a user navigates to a URL:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The server processes the request.&lt;/li&gt;
&lt;li&gt;It fetches any required data.&lt;/li&gt;
&lt;li&gt;It generates a complete HTML document.&lt;/li&gt;
&lt;li&gt;That document is sent to the browser, which can immediately display the content.
This is different from CSR, where the server sends a lightweight HTML shell and relies on JavaScript running in the browser to fetch data, construct the DOM, and render the final page.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why Do You Need SSR?
&lt;/h2&gt;

&lt;p&gt;SSR solves several common challenges in modern web development:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Faster First Paint / Initial Load Since the browser receives a fully rendered page, users can see meaningful content almost immediately.&lt;/li&gt;
&lt;li&gt;Improved SEO Search engine crawlers can index SSR pages more effectively because the content is present in the initial HTML, not hidden behind JavaScript execution.&lt;/li&gt;
&lt;li&gt;Consistent User Experience Across Devices SSR reduces reliance on client-side processing power. This makes it ideal for users on older devices or slower networks, where heavy JavaScript rendering would otherwise cause delays.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  When to Use SSR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Content-heavy sites that need SEO (e.g., blogs, e-commerce stores, news sites).&lt;/li&gt;
&lt;li&gt;Applications targeting a global audience where device performance and network speed vary.&lt;/li&gt;
&lt;li&gt;Pages requiring fast initial load times to reduce bounce rates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When Not to Use SSR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Highly interactive, dynamic applications (e.g., dashboards, single-page apps) where most of the content changes after the page loads.&lt;/li&gt;
&lt;li&gt;Applications with real-time updates (e.g., stock tickers, chat apps) that rely heavily on client-side rendering anyway.&lt;/li&gt;
&lt;li&gt;Projects with limited server resources, since SSR increases server workload.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pros of SSR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Faster initial page load and first contentful paint&lt;/li&gt;
&lt;li&gt;Better SEO since crawlers see pre-rendered HTML&lt;/li&gt;
&lt;li&gt;Consistent experience across devices&lt;/li&gt;
&lt;li&gt;Good for static or semi-static content with high visibility needs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cons of SSR
&lt;/h2&gt;

&lt;p&gt;Heavier load on the server, which can affect scalability &lt;br&gt;
Slower page transitions (compared to CSR) since each navigation triggers a new server request  &lt;br&gt;
Increased complexity when integrating with client-side interactivity Caching and performance optimization can be more challenging&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>tutorial</category>
      <category>performance</category>
    </item>
    <item>
      <title>Accessibility.</title>
      <dc:creator>Adetola Esther Aderibigbe</dc:creator>
      <pubDate>Mon, 28 Jul 2025 09:22:59 +0000</pubDate>
      <link>https://dev.to/adetolaesther/accessibility-1913</link>
      <guid>https://dev.to/adetolaesther/accessibility-1913</guid>
      <description>&lt;h2&gt;
  
  
  What is accessibility?
&lt;/h2&gt;

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

&lt;p&gt;Accessibility is the practice of making your app or website accessible to everyone regardless of their ability or disability. It is often referred to as A11y(as in, 'a', then 11 characters, and then 'y'). It can be seen as treating everyone the same, and giving them equal opportunities, no matter what their ability or circumstances. &lt;/p&gt;

&lt;h2&gt;
  
  
  What type of disabilities are we looking at?
&lt;/h2&gt;

&lt;p&gt;1) People with visual impairments &lt;br&gt;
  This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blindness&lt;/li&gt;
&lt;li&gt;Low vision&lt;/li&gt;
&lt;li&gt;Color blindness&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many visually impaired users rely on screen readers, which convert digital text into speech, or screen magnifiers. Designers and developers should ensure that all visual elements are also accessible through text, proper HTML semantics, and ARIA labels.&lt;/p&gt;

&lt;p&gt;2) people with hearing impairments &lt;br&gt;
This includes individuals with hearing loss ranging from mild to profound&lt;br&gt;
To make content accessible to them,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Videos should be captioned&lt;/li&gt;
&lt;li&gt;Audio should come with transcripts&lt;/li&gt;
&lt;li&gt;Textual alternatives should be provided for sound-based interactions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3) people with mobility impairments &lt;br&gt;
This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Paralysis or loss of limb function&lt;/li&gt;
&lt;li&gt;Neurological disorders affecting control&lt;/li&gt;
&lt;li&gt;Age-related movement limitations
Such users may not use a mouse. They might navigate entirely with a keyboard, voice control, or adaptive switches. Therefore, all functionality should be operable using a keyboard alone.
The Web Content Accessibility Guidelines (WCAG) were developed by the World Wide Web Consortium (W3C) to standardize accessibility. These guidelines are organized around four core principles, known by the acronym POUR.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Users must be able to perceive the content, regardless of how they interact with it.&lt;br&gt;
Best practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provide text alternatives (alt) for images&lt;/li&gt;
&lt;li&gt;Use captions and transcripts for media&lt;/li&gt;
&lt;li&gt;Ensure sufficient color contrast&lt;/li&gt;
&lt;li&gt;Structure content with proper headings&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Users must be able to interact with and control the interface.&lt;br&gt;
Best practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All functionality should be accessible via keyboard&lt;/li&gt;
&lt;li&gt;Allow users to pause or disable animations&lt;/li&gt;
&lt;li&gt;Avoid flashing content that could trigger seizures&lt;/li&gt;
&lt;li&gt;Provide clear navigation and focus indicators&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Understandable
&lt;/h2&gt;

&lt;p&gt;Content and operation should be easy to comprehend.&lt;br&gt;
Best practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use plain, simple language&lt;/li&gt;
&lt;li&gt;Make navigation consistent across pages&lt;/li&gt;
&lt;li&gt;Label all forms clearly&lt;/li&gt;
&lt;li&gt;Identify input errors and offer suggestions&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Content must be usable with a wide range of technologies, including assistive tools.&lt;/p&gt;

&lt;p&gt;Best practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use semantic HTML elements &lt;code&gt;&amp;lt;button&amp;gt; &amp;lt;nav&amp;gt; &amp;lt;header&amp;gt;&lt;/code&gt; etc.&lt;/li&gt;
&lt;li&gt;Add ARIA roles and states where necessary&lt;/li&gt;
&lt;li&gt;Ensure compatibility with screen readers and assistive browsers&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to Implement Accessibility in Your Projects
&lt;/h2&gt;

&lt;p&gt;Accessibility is the right thing to do as building accessible sites benefits everyone. A common accessibility myth is that accessibility is an expensive "added extra" to implement on a project.These are some practical suggestions you can take to make your website or app more accessible&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Start with Semantic HTML
&lt;/h2&gt;

&lt;p&gt;Use the correct HTML elements for structure and meaning. &lt;br&gt;
For example:&lt;br&gt;
Use &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; for buttons, not &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;&lt;br&gt;
Use &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; elements properly linked to form inputs&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Caption and Transcribe All Media
&lt;/h2&gt;

&lt;p&gt;Don’t rely on auto-generated captions. Always provide human-reviewed captions and transcripts for audio or video content.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Navigation
&lt;/h2&gt;

&lt;p&gt;Presenting menus as structured lists gives assistive technologies the context they need to help users navigate. It also improves responsiveness on various devices and screen sizes. &lt;br&gt;
Practice includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Placing your menu in a consistent location across all pages&lt;/li&gt;
&lt;li&gt;Use HTML or ARIA to highlight the active/selected item&lt;/li&gt;
&lt;li&gt;Label menus clearly for easy understanding&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Order and Grouping
&lt;/h2&gt;

&lt;p&gt;Grouping related items enhances navigation and helps screen readers communicate structure and progress clearly.&lt;br&gt;
Practice includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use unordered lists &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt;for menus or items where order doesn’t matter&lt;/li&gt;
&lt;li&gt;Use ordered lists &lt;code&gt;&amp;lt;ol&amp;gt;&lt;/code&gt; where sequence is important (e.g. steps in a process&lt;/li&gt;
&lt;li&gt;Make sure only &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; elements are children of your lists&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Color Contrast
&lt;/h2&gt;

&lt;p&gt;Color contrast ensures that text and interactive elements are legible, even for users with low vision or color blindness. Text and background contrast should be at least 4.5:1 and Essential UI icons should have a contrast ratio of at least 3:1&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Git as an onion</title>
      <dc:creator>Adetola Esther Aderibigbe</dc:creator>
      <pubDate>Mon, 16 Jun 2025 10:16:10 +0000</pubDate>
      <link>https://dev.to/adetolaesther/git-as-an-onion-279</link>
      <guid>https://dev.to/adetolaesther/git-as-an-onion-279</guid>
      <description>&lt;p&gt;Git is an open-source version control system that allows developers to track and manage their codebase. It allows several developers to be able to work on one particular project simultaneously without interfering with each other's work. We'll be looking at Git as an onion in which we unfold each layer one by one until we get to the core which is understanding what Git really is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Git&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Git helps in tracking changes, making it possible to reverse back to the previous stage in case something goes wrong.&lt;/li&gt;
&lt;li&gt;Git allows for multiple people to be able to work on one single project at once. &lt;/li&gt;
&lt;li&gt;Git acts as storage, allowing people to share their work.
Git heavily relies on the branching system.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Branching&lt;/strong&gt;&lt;br&gt;
One of the layers of our onion is Branching.&lt;br&gt;
 Branching traditionally means diverging from the main development line. It is copying a copy of what already exists.&lt;br&gt;
A branch can also be called a pointer to a commit. The default branch is always the main branch in which other branches are then created from. Branches are mainly created for features, bugs, fixes, experiments, and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Branching Strategy&lt;/strong&gt;&lt;br&gt;
Branching Strategy is basically defining a set of rules to define how to work with branch. It is creating a structured workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Workflow&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Centralized Workflow &lt;br&gt;
In a centralized workflow, work is done usually in the main branch, and it is the most common branch among developers working on personal projects. This is actually not often advised as it is hard to keep track of changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Gitflow Workflow &lt;br&gt;
Gitflow Workflow, unlike centralize workflow, this involves a number of branches. There is usually the main branch, which is the stable branch and then the development branch where all the changes happen. We also have a feature branch, which is created solely for development of a particular feature, a release branch for a release, hotfix branch to fix codes already in production, and so on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Forking Workflow &lt;br&gt;
A fork is a copy of a repo a developer has worked on. A developer can make a copy of a repo(fork a repo)and then make their own changes and then push to their account without affecting the original repo. This workflow is usually used in open source projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;GitHub Workflow &lt;br&gt;
GitHub Workflow compare to other workflows is lightweight and favoured by GitHub. This workflow is also created around branches and revolves around pull requests. The GitHub Workflow is the workflow in which we will be discussing more in depth in this article.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GitHub workflow heavily operates on a pull request system. Once changes are made in a development branch, the changes are then pushed to the main branch and then this is done by creating a pull request. What the pull request does is it announces a push to a branch, allowing other collaborators to discuss, reveal changes, or even add more commits to the pull request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pull Requests&lt;/strong&gt; &lt;br&gt;
A pull request is another layer of the onion. A pull request improves code quality, as it allows a review system which ensures only quality codes are pushed to production. It also allows transparency to know what changes are made and then who made them. It also allows control which makes sure that only what we want to be pushed to production are pushed. And lastly, pull request also encourages collaborations as it allows developers to review each other's codes, approve and leave comments if there is need to.&lt;/p&gt;

&lt;p&gt;A pull request is created using the Git push  command&lt;br&gt;
After a pull request has been approved, it is merged into the destination branch. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Merging&lt;/strong&gt;&lt;br&gt;
After a Pull request has been reviewed and found satisfactory by the reviewer, it is then Merge into the new branch, fully allowing changes made in the feature branch to be visible also in the destination branch. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type of merge&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Merge commit&lt;br&gt;
The default type of merge on GitHub is the merge commit. With this, a new branch is created that brings changes made in the feature branch. This contains histories of both branches.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Squash and merge &lt;br&gt;
Changes made in the feature branch are squashed into a single commit before merging into the destination branch.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rebase and merge&lt;br&gt;
Using this approach, the commits from the feature branch are placed on the destination branch, and then a merge is executed. The history of the destination branch is rewritten with the new commits.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Merging vs Rebasing&lt;/strong&gt;&lt;br&gt;
Git Rebase solves the same problem as Git merge. They were both designed to integrate changes from one branch to another. This is however just done in different ways. Git Merge combines histories and creates a merge commit, while Rebase moves your commit on top of another branch, completely rewriting the history. Git Merge preserves exact commit history, while Git Rebase creates a linear and cleaner history.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Merge?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When collaborating with other developers on shared branch &lt;/li&gt;
&lt;li&gt; To preserve historical paths development took, what branches were combined and so on &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to Rebase?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When working on a solo project &lt;/li&gt;
&lt;li&gt;To avoid unnecessary merge conflicts &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Merge conflicts&lt;/strong&gt;&lt;br&gt;
A Merge Conflict occurs when changes are made on the same line of code by different developers. A Merge Conflict can also happen if the file with the changes made in has been deleted. A Merge Conflict has to be resolved before a Merge can happen. &lt;/p&gt;

&lt;p&gt;Pull requests go through several states. The first one is the open state which indicates the PR is active and under construction ready to be merged. The second stage is the approved stage after which one or more reviewers have gone through the code and are satisfied with the states. This indicates that the PR is ready to be merged. Change Requested states. This indicates that a reviewer requested that changes be made in a particular file before the PR can be merged. Closed PR. A PR that is closed has been merged.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Git Cherry Picking&lt;/strong&gt;&lt;br&gt;
 Git cherry Picking is an advance merging method. Gits Cherry Picking copies specific commits to another branch. It moves specific commits from one branch and then just that commit is moved into another branch. It creates duplicate commits in each branches, which can also cause confusion. &lt;/p&gt;

&lt;p&gt;It is an advanced Gits feature and shouldn't replace a commit.Cherry‑pick does not move the commit. It creates a copy of the commit, but with a new parent in the branch that you're cherry‑picking into. The original commit stays the same. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to cherrypick?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;bug fix that applies to multiple versions of a product &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Merge changes from a branch into another without merging the entire branch.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Git Blame&lt;/strong&gt;&lt;br&gt;
Git Blame is one of the many layers of the onion. It shows the last changes made in the file, when it was made and who made them&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to use Gif Blame&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When debugging, it can be used to trace who walked on the path. &lt;/li&gt;
&lt;li&gt;To preserve history.&lt;/li&gt;
&lt;li&gt;For accountability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding Git is like peeling an onion layer by layer Before you get to the core, which is understanding how Git works, we have to peel through each layers, one after the other. Pool request, branching, etc. There are several other layers that are also not discussed in this article.&lt;/p&gt;

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