<?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: Frihk Ian</title>
    <description>The latest articles on DEV Community by Frihk Ian (@frihk_ian).</description>
    <link>https://dev.to/frihk_ian</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%2F3722900%2Fa0066d49-46b8-4c9c-aee1-170915f27351.jpg</url>
      <title>DEV Community: Frihk Ian</title>
      <link>https://dev.to/frihk_ian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/frihk_ian"/>
    <language>en</language>
    <item>
      <title>Vibe Coding Didn't Kill Junior Devs' Careers. It Just Fast-Forwarded Them.</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Wed, 29 Jul 2026 13:07:36 +0000</pubDate>
      <link>https://dev.to/frihk_ian/vibe-coding-didnt-kill-junior-devs-careers-it-just-fast-forwarded-them-229p</link>
      <guid>https://dev.to/frihk_ian/vibe-coding-didnt-kill-junior-devs-careers-it-just-fast-forwarded-them-229p</guid>
      <description>&lt;p&gt;What was originally a joke, a colloquial term for projects that are done on the weekend, in which you tell a model what you want to do, don't read what it tells you, and follow it by feeling. It was completely out of that "hole" by 2026. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;Ninety-two per cent of U.S. developers are using AI coding tools daily, up 340% from the year before&lt;/a&gt;, and a growing number of junior developers built their entire portfolio around it, since it allowed them to deliver something impressive without having to go through college and years of fundamental coding.&lt;br&gt;
That remained for a time. Next, it was time for the bill.&lt;/p&gt;

&lt;h2&gt;
  
  
  The surging demand for hiring that no one anticipated.
&lt;/h2&gt;

&lt;p&gt;Vibe coding isn't only a coding style; it's an economic change. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;25% of startups in Y Combinator's Winter 2025 batch had codebases that are 95% or more AI-generated&lt;/a&gt;. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;The price of developing a viable SaaS product has come down from $200,000 to $5,000, and the time to build is now six weeks versus six months&lt;/a&gt;. In that same context, a junior developer who can truthfully boast, "I built this whole app myself using AI" seems like a brilliant hire, both cheap and fast and able to deliver a complete product with just one developer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;63% of today's coding users describe themselves as non-developers, product managers, marketing directors, founders, designers&lt;/a&gt;. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;Forrester reckons that there are currently more than 16 million active citizen developers in the world&lt;/a&gt;, and &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;Gartner foresees the number of citizen developers will surpass the number of professional engineers by four to one by 2028&lt;/a&gt;. For a hot minute, it was really just "junior developer" or "person who could prompt well", and hiring managers went for whoever could get the demo packaged out the quickest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next, the codebases had to be tested!
&lt;/h2&gt;

&lt;p&gt;All this has a productivity understory that isn't as easy as the pitch makes it sound. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;In a controlled study, METR determined that more experienced developers who worked on large, actual codebases were 19% slower when using AI tools, and also felt they were 20% faster&lt;/a&gt;. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;Almost 40 points between how fast people feel and how fast they actually are&lt;/a&gt;, and that's important in that it really means that the teams that are planning sprints based on their self-reported productivity are doing it quietly on the sidelines.&lt;/p&gt;

&lt;p&gt;The same can be said for quality. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;In one prominent study, the rate of bugs increased by 41% post AI coding tool adoption&lt;/a&gt;, while &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;other research comparing AI-coauthored pull requests with those written by humans revealed 1.7 times more major issues, with logic errors up 75% and readability issues tripling&lt;/a&gt;. However, &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;the less glamorous part of code, that which is refactored and simplified, dropped from a quarter of changed code in 2021 to under ten per cent by 2024&lt;/a&gt;, even as the amount of code being shipped kept climbing. This doesn't appear on day one. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;It emerged by the 12th week, when teams are usually using 20-30% of their sprints to repair bugs they weren't fully aware of when they were first created by AI code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That's the reason why junior developers are the first to take the hit. Still, debugging doesn't involve writing code; it involves understanding code, and that's something that vibe coding allows you to avoid. A developer can develop a working feature and never, ever have a mental model of why it works, and that's okay until it doesn't and there's no foundation under them. Add security to the mix, and you'll find that &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;almost half of all code generated by AI fails basic industry security standards&lt;/a&gt;, and that &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;one study of more than 200 applications coded by vibe reveals that over 90 per cent had one or more vulnerabilities stemming from a lack of awareness of what they don't know&lt;/a&gt;. These are not fancy bugs, but the very basic ones: unvalidated input, lack of access controls, exposure of credentials, all of that sort of thing that an experienced engineer instinctively looks for, and a junior developer who didn't get the rudiments of engineering basics.&lt;/p&gt;

&lt;p&gt;There is no uncertainty about the production failures. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;One vibe-coding tool experienced three separate security incidents which were widely covered, including one that remained open for 48 days&lt;/a&gt;. In another case, &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;an AI-powered social app took just three days to leak 1.5 million authentication tokens and 35,000 emails&lt;/a&gt;. On the broader market, &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;about ten thousand startups have tried to work out a working production application using an AI assistant, and over eight thousand of them need costly rebuilds that are usually $50,000 to $500,000 a pop&lt;/a&gt;, because the demo they would build wasn't going to hold up with actual users, actual load, and an actual security audit.&lt;/p&gt;

&lt;h2&gt;
  
  
  The market took no notice of this,
&lt;/h2&gt;

&lt;p&gt;As developers have incorporated these tools into their workflows, they are also becoming more sceptical about their effectiveness. Developers' own trust in the tools has been shaken as they have used them more. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;Trust has dropped from approximately 40% two years ago to approximately 29% today, and almost half of developers actively distrust the output of AI-generated code&lt;/a&gt;. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;Only a small number of senior engineers say they have high trust, and one in five say they have high distrust among senior engineers specifically&lt;/a&gt;. But &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;less than half of developers claim to always look over AI-generated code before committing it&lt;/a&gt;, meaning that plenty of AI-generated code is flying the flag without a second glance.&lt;/p&gt;

&lt;p&gt;Labour statistics are now catching up with the anecdotes. Overall demand for engineering talent hasn't gone away, but &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;junior developer hiring has dropped by almost 20% among those who would typically begin their careers in this role&lt;/a&gt;. &lt;a href="https://keyholesoftware.com/vibe-coding-trends-2026/" rel="noopener noreferrer"&gt;As much as forty to seventy per cent of the traditional entry-level development has been swallowed up by AI&lt;/a&gt;. Meanwhile, junior developers, who are seeking to enter the field, are doing so in one of the most competitive fields in years, and at the bare entry-level, in a field that increasingly demands the judgment of a senior engineer.&lt;/p&gt;

&lt;p&gt;The irony is sharp. The idea of vibe coding was to make it the leveller, the one thing that anyone could do and be a builder. For a certain group of junior coders, however, it was a way to an appointment and a quick path out of it, since the skills used to land that job didn't necessarily help when it came time to interact with real people, real traffic, real security audits.&lt;/p&gt;

&lt;p&gt;What really makes the difference between the ones who make it and those that don't?&lt;/p&gt;

&lt;p&gt;The organisations and developers that will emerge from this cycle without issues are not ones that eschewed AI but rather embraced it as an aid to real understanding, not a replacement. The pattern that passes this test is more "let me build with AI" than "let AI build it for me," with the caveat that while the AI is allowed to build it, there is a human in charge of checking each step it takes when the code is written into production code, the AI doesn't run the test gates unless there is a deadline pressure that can't be overcome, and the specifications are detailed enough for an agent to know exactly what is supposed to be built, and not just handed an open-ended prompt and encouraged to guess well. Such teams are consistently spared the technical debt and exposure to security issues that plague other teams that scale up their use of AI without first establishing that structure. If teams consider governance as something to do afterwards, they are almost always going to pay for it afterwards at multiple times the cost of doing it from the beginning.&lt;/p&gt;

&lt;p&gt;Vibe coding doesn't need to be abandoned, and it shouldn't. If you're a junior developer trying to land a job right now, the pitch that lands you the job and keeps you on is NOT "I can have AI create things." It's &lt;strong&gt;"I know how it works well enough to fix it when it breaks, secure it before it ships, and explain it to the next person who touches it."&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>vibecoding</category>
      <category>juniordev</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Multiagent orchestration article</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Tue, 28 Jul 2026 08:38:11 +0000</pubDate>
      <link>https://dev.to/frihk_ian/multiagent-orchestration-article-e15</link>
      <guid>https://dev.to/frihk_ian/multiagent-orchestration-article-e15</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%2F9p3h9ltv29kcdhgp372v.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%2F9p3h9ltv29kcdhgp372v.png" alt=" " width="799" height="475"&gt;&lt;/a&gt;Multi-agent orchestration is the hot topic of 2026. Introduced in February, &lt;a href="https://dev.to/jpeggdev/the-ai-revolution-in-2026-top-trends-every-developer-should-know-18eb"&gt;Agent HQ from GitHub allows developers to concurrently use Claude, Codex, and Copilot to solve a problem, with each having a different perspective on trade-offs or details&lt;/a&gt;. &lt;a href="https://dev.to/jpeggdev/the-ai-revolution-in-2026-top-trends-every-developer-should-know-18eb"&gt;Between Q1 2024 and Q2 2025, Gartner experienced a 1,445% increase in inquiries about multi-agent systems&lt;/a&gt;. Steve Yegge, a very experienced engineer at Amazon and Google, has discussed his work on orchestrated agents and &lt;a href="https://mikemason.ca/writing/ai-coding-agents-jan-2026/" rel="noopener noreferrer"&gt;producing 12,000 lines of code per day&lt;/a&gt;. Even more impressively, &lt;a href="https://mikemason.ca/writing/ai-coding-agents-jan-2026/" rel="noopener noreferrer"&gt;Cursor's team programmed an entire browser over a thousand files and a million lines of code in just one week by using hierarchical orchestration rather than having a single agent perform all the tasks sequentially&lt;/a&gt;, though &lt;a href="https://mikemason.ca/writing/ai-coding-agents-jan-2026/" rel="noopener noreferrer"&gt;multiple reviewers reported the code didn't compile at announcement, and Cursor's own CEO described the result as merely "kind of works"&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The part nobody's celebrating&lt;/p&gt;

&lt;p&gt;The only news to this is Google's 2025 DORA report. It determined that &lt;a href="https://mikemason.ca/writing/ai-coding-agents-jan-2026/" rel="noopener noreferrer"&gt;for a 90% increase in AI adoption, bug rates climbed 9%, code review time rose 91%, and average PR size grew 154%&lt;/a&gt;. When read as a single group, that pattern emerges instantly: teams are shipping more code, more quickly, and it's measurably more difficult to review, and measurably more buggy once it is shipped. No, multi-agent setups don't help with this; they exacerbate it. You're not just reading one model's decisions now; you're reading several.&lt;/p&gt;

&lt;p&gt;Engineers who interact with such systems refer to a particular type of failure: &lt;a href="https://mikemason.ca/writing/ai-coding-agents-jan-2026/" rel="noopener noreferrer"&gt;architectural drift, in which each agent takes an action that is locally optimal but inconsistent with the actions taken by the other agents five minutes ago&lt;/a&gt;. One agent selects one pattern to use for errors, another agent selects another pattern for a neighboring module, and nobody realizes until anybody attempts to extend the feature, and the codebase spits. Then there is the issue of &lt;a href="https://mikemason.ca/writing/ai-coding-agents-jan-2026/" rel="noopener noreferrer"&gt;"lost in the middle," as it's called, and how much information that is in the middle of the long context window becomes lost and essentially forgotten by the agent&lt;/a&gt;, so it seems like it doesn't know about it.&lt;/p&gt;

&lt;p&gt;The naive version of this doesn't work very well, as Cursor's own account of constructing that browser admits. &lt;a href="https://mikemason.ca/writing/ai-coding-agents-jan-2026/" rel="noopener noreferrer"&gt;The first tried to treat all agents the same and, to avoid collisions, used file locking, but this didn't work: agents were holding locks too long, and twenty agents were operating at the throughput of two or three. Their second attempt was optimistic concurrency control, not locks, and that didn't work out quite right; the agents became more of a risk taker and began to avoid anything that resembled a hard task&lt;/a&gt;. &lt;a href="https://mikemason.ca/writing/ai-coding-agents-jan-2026/" rel="noopener noreferrer"&gt;What really worked was a hierarchical structure as opposed to agents being free to improvise in parallel: Planner agents that continuously explored the codebase and created tasks, Worker agents that executed assigned tasks without coordinating with each other, and Judge agents that determined whether to continue at each cycle's end&lt;/a&gt;, rather than a bunch of agents improvising freely in parallel with no one peeking at each other's work.&lt;/p&gt;

&lt;p&gt;Why this becomes a review nightmare&lt;/p&gt;

&lt;p&gt;In response, automated PR review tools have scaled, and &lt;a href="https://zylos.ai/research/2026-04-22-autonomous-code-review-multi-agent-pr-analysis/" rel="noopener noreferrer"&gt;teams managing multiple agent review fleets see real improvements: 30–60% faster review cycle times and, in the best cases, 25–35% fewer production defects&lt;/a&gt;. But even the researchers developing these systems are straightforward about the problem: AI reviewers are good at spotting issues in the code, and they're not so good at understanding what you were trying to accomplish, and why you were making the decision. What you get is a ton of automated "this could be improved" comments, yet not much of "this doesn't fit how the rest of the system thinks.&lt;/p&gt;

&lt;p&gt;In a piece about orchestrated coding, Addy Osmani stated it quite simply: &lt;a href="https://addyosmani.com/blog/code-agent-orchestra/" rel="noopener noreferrer"&gt;"With an orchestrated army of agents, there's no natural bottleneck. Small harmless mistakes — a code smell here, a duplication there, an unnecessary abstraction — compound at a rate that's unsustainable."&lt;/a&gt; These are no longer being created by a single human who was used to writing the code manually and would have noticed any errors or bugs; now they are just being created by many humans who are not involved in the code change, so the errors just accumulate. There is no crash reported. It turns up when someone tries to add a feature to it, and finds the architecture won't support it, and the tests that the agents also wrote are no more reliable than the code they were supposed to be checking.&lt;/p&gt;

&lt;p&gt;What this actually means for teams adopting this now&lt;/p&gt;

&lt;p&gt;None of this is a reason to not use multi-agent coding. The productivity narrative is true, and it will keep going. But it is the teams who are designing plan-approval steps, token budgets, verifier agents, human review gates, and so on into their orchestration, rather than eliminating themselves from the equation and hoping that the agents will get it and do this well. These teams will not realise the cost right away if they do not do that. They will see it in 6 months when the two-line feature request becomes a two-week rewrite, and no one, human or agent, understands the system's structure and why.&lt;/p&gt;

&lt;p&gt;There was never a problem with speed. It was inevitable that coordination would be difficult. But it was not until now that we needed to solve it.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Nobody's Reading Tutorials Anymore. They're Just Asking AI.</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Mon, 27 Jul 2026 00:52:47 +0000</pubDate>
      <link>https://dev.to/frihk_ian/nobodys-reading-tutorials-anymore-theyre-just-asking-ai-17nc</link>
      <guid>https://dev.to/frihk_ian/nobodys-reading-tutorials-anymore-theyre-just-asking-ai-17nc</guid>
      <description>&lt;p&gt;Two or three months ago, I noticed that my Go tutorials were not receiving the level of engagement I was expecting, with fewer comments and saved posts. The first thing that popped into my mind was that I was slipping. Then I absorbed the larger data, and it seems this isn't a me problem; this is an industry problem.&lt;/p&gt;

&lt;p&gt;The figures are appalling.&lt;/p&gt;

&lt;p&gt;Begin on Stack Overflow, which was the most comprehensive reference library for technology. During 2014 to 2020 Question volume reached a maximum of over 2,00,000 per month. It ended up below 50,000 already in late 2025, wiping out 15 years of increases. &lt;a href="https://ppc.land/stack-overflow-traffic-collapses-as-ai-tools-reshape-how-developers-code/" rel="noopener noreferrer"&gt;A January 2026 data visualization, built from Stack Overflow's own Data Explorer, showed the platform's monthly question volume had fallen back to roughly what it was in 2008, the year the site launched&lt;/a&gt;. The same 2025 Developer Survey found that &lt;a href="https://ppc.land/stack-overflow-traffic-collapses-as-ai-tools-reshape-how-developers-code/" rel="noopener noreferrer"&gt;while 82% of respondents still visit Stack Overflow at least a few times a month, only 25% do so daily or more often&lt;/a&gt;, a shift from the platform's old status as a daily habit for most programmers.&lt;/p&gt;

&lt;p&gt;It's not just one platform which is in trouble. Invisible, though, is the larger behaviour change, one that goes beyond the visible symptom, namely that developers no longer consult written references first.&lt;/p&gt;

&lt;p&gt;Rather than "Where everybody was," here is "Where everyone went.&lt;/p&gt;

&lt;p&gt;They went to AI. In the past, this was a rarity, no longer the exception. &lt;a href="https://uvik.net/blog/ai-coding-assistant-statistics/" rel="noopener noreferrer"&gt;Approximately 84% (up from 76% a year ago) of all developers use or plan to use AI coding tools, according to an AI Coding Assistant survey Stack Overflow conducted this year&lt;/a&gt;. Developers report being most comfortable turning to AI for tasks like searching for answers, learning new concepts, and writing documentation, and &lt;a href="https://uvik.net/blog/ai-coding-assistant-statistics/" rel="noopener noreferrer"&gt;44% of developers now use AI tools to learn to code, up from 37% the year before&lt;/a&gt;. Interestingly enough, when developers are asked what their favourite tools of the art are best at, "creating documentation" and "explaining any existing code" are near the top of the list, ahead of "writing new code from scratch.&lt;/p&gt;

&lt;p&gt;The final one is worthy of contemplation: developers are increasingly turning to AI to explain a concept rather than write plain, original code for them. That's right, a direct miss on everything that the tutorial does at its core, which is explaining.&lt;/p&gt;

&lt;p&gt;No wonder, it's not that difficult! The tutorial is geared to the "average reader" (as it's called). A question is directly written for you right now with your stack trace, your file, your specific stupid typo. No pages of preamble to scroll through to discover just one line of config that you needed, no "Closed as duplicate" and no ads, just an answer tuned to your exact problem.&lt;/p&gt;

&lt;p&gt;However, this doesn't seem to be a clean sweep for AI.&lt;/p&gt;

&lt;p&gt;And this is where it gets more interesting than 'AI beat tutorials, tutorials are dead!'. Usage is on the increase, but trust is not. &lt;a href="https://theaieconomy.substack.com/p/stack-overflow-2025-ai-trust-gap" rel="noopener noreferrer"&gt;Some 46% lack confidence in the accuracy of AI-generated output, up from 31% a year ago, and nearly two-thirds feel AI frequently generates code that seems correct but isn't necessarily reliable&lt;/a&gt;. In fact, &lt;a href="https://www.secondtalent.com/resources/ai-developer-productivity-tools-2026/" rel="noopener noreferrer"&gt;according to research from METR, seasoned developers actually took 19% longer to accomplish their tasks using AI tools; however, they felt they finished 20% faster after completing the work&lt;/a&gt;. There is definitely an overestimation of how much AI is actually supporting people.&lt;/p&gt;

&lt;p&gt;An even more acute concern is piling up regarding junior developers. They do not develop the predispositions they need when they rely on AI to solve their problems rather than on docs and forums. They can possibly write working code without knowing how it works, which is okay up until the moment the AI goes off-track by making a minor mistake that it finds hard to track in the code; at that point, they don't have a solid base to lean on if it goes astray.&lt;/p&gt;

&lt;p&gt;It is not a matter of "tutorials stink, AI is good. It's that developers have settled on a path that's more time-consuming and difficult, and that offers the chance to develop real comprehension, rather than a quicker way that yields an answer with little assurance it's correct.&lt;/p&gt;

&lt;p&gt;Popular lessons can be seen when you write tutorials. Writing tutorials will show some of the popular lessons.&lt;/p&gt;

&lt;p&gt;This is not that it is time to halt its development; it's just that it's time to modify what you write and how. Generic explanation/memorized text is strong, with the notable exception of gotchas or "why this worked last month, and not now", explaining architectural trade-offs is strong, but nothing concerning a fast-changing library or a particular corner case is strong. Well, that's where a good article still gets its power from. It also helps when you are aware of the fact that unless the AI does an excellent job with their answer, your reader is not coming to your post cold, rather they are here because they want to take a look at it to see if it matches their requirements and/or sanity, so if you write assuming they came to your post because the AI answer did not suffice, it will benefit more than an answer they are reaching for from cold.&lt;/p&gt;

&lt;p&gt;Optimise for skimmability and machine-readability as well, because these days there's a decent portion of traffic that's not coming to you from a human who's reading from top to bottom, but rather there's a machine there reading your page to respond to the query of someone else. Headings, explicit statements of code, and clear explanations benefit both the writer and the reader. Finally, don't forget to be more specific about the "why" rather than the "how" — it's the one thing that thin AI answers so rarely but the most crucial part of what junior developers need and the part that will keep a human reader on your page.&lt;/p&gt;

&lt;p&gt;What direction this is taking&lt;/p&gt;

&lt;p&gt;I don't know why, but tutorials don't just disappear. I don't think the form that dies is a particularly useful way to get X configured, since there's a much simpler prompt that does the job more easily. The things that remain, perhaps even grow, are the pieces of writing that convey judgment, negotiation, nuance, and detail that have not yet been fully captured through modelling.&lt;/p&gt;

&lt;p&gt;So was the love for reading changed. It wasn't until somebody "got it" that the need for someone who actually understood the problem.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Array in Go</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Wed, 24 Jun 2026 09:33:49 +0000</pubDate>
      <link>https://dev.to/frihk_ian/array-in-go-mad</link>
      <guid>https://dev.to/frihk_ian/array-in-go-mad</guid>
      <description>&lt;h2&gt;
  
  
  What is an Array in Go?
&lt;/h2&gt;

&lt;p&gt;In Go, an array is a fixed-size, ordered set of values with the same type. Imagine an array as a 12 egg tray; the minimum number of eggs which can fit into an array is 12; the maximum number of eggs which can fit into an array is 12. That is the exact behaviour of a Go 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="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&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="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;groceries&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="c"&gt;// Step 1: Create an array that holds 3 strings&lt;/span&gt;
&lt;span class="n"&gt;groceries&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="s"&gt;"Bread"&lt;/span&gt;&lt;span class="c"&gt;// Step 2: Put "Bread" in slot 0&lt;/span&gt;
&lt;span class="n"&gt;groceries&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="s"&gt;"Milk"&lt;/span&gt;&lt;span class="c"&gt;// Step 3: Put "Milk" in slot 1&lt;/span&gt;
&lt;span class="n"&gt;groceries&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="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="c"&gt;// Step 4: Put "Eggs" in slot 2&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;groceries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c"&gt;// Step 5: Print the whole array&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;code&gt;[Bread Milk Eggs]&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Zero Values
&lt;/h2&gt;

&lt;p&gt;In Go, you create an array by using the var keyword, and each of the slots receives the safe code Zero value, given that no elements are explicitly set.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft65yo36zfwm91e9b82t7.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%2Ft65yo36zfwm91e9b82t7.png" alt=" " width="504" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means that your array will never be in an unsafe state of being uninitialized. It will be valid on every slot, even before set. The same thing is a safety feature in Go intentionally.&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;prices&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;float64&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;prices&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;//  Output: [0 0 0]  - Go fills all the values with zeros automatically&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Declaring an Array
&lt;/h2&gt;

&lt;p&gt;In Go, you can declare an array in a few different ways, and you will learn about each of them in turn.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using the var Declaration
&lt;/h4&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;groceries&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;groceries&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="s"&gt;"Bread"&lt;/span&gt;
&lt;span class="n"&gt;groceries&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="s"&gt;"Milk"&lt;/span&gt;
&lt;span class="n"&gt;groceries&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="s"&gt;"Eggs"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to initialise an array of a specific size and use it later, then this is the best method to achieve this.&lt;/p&gt;

&lt;h4&gt;
  
  
  Array Literals
&lt;/h4&gt;

&lt;p&gt;If you are already aware of all the values at the time you write the code, then you can use the literal 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="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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&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;groceries&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;code&gt;[Bread Milk Eggs]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The actual syntax is:&lt;br&gt;
&lt;code&gt;[size]type{value1, value2, value3}&lt;/code&gt;&lt;br&gt;
This method is used when we know the values which we want to store in an array.&lt;/p&gt;
&lt;h4&gt;
  
  
  Let the Compiler count for you — The &lt;code&gt;...&lt;/code&gt; trick
&lt;/h4&gt;

&lt;p&gt;When using literal syntax, and you find that you have to manually count yourself, there's a shortcut in Go. Use ... to fill the size so the compiler can calculate the length automatically.&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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Butter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Coffee"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c"&gt;//Three dots  compiler counts for you&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type of groceries is [5]string. The ... instructs the compiler to count for you. This will be helpful if you have a super long list and you can't determine its length.&lt;/p&gt;

&lt;h4&gt;
  
  
  Sparse Initialization
&lt;/h4&gt;

&lt;p&gt;You can also use index: value syntax in the braces: to specify where a value will 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="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;// Put "Bread" in slot 0 and "Coffee" in slot 4; all other slots get ""&lt;/span&gt;
&lt;span class="n"&gt;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Coffee"&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;groceries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&gt;
Expected output:&lt;br&gt;
&lt;code&gt;[Bread Coffee]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is helpful when you have an array of a large number of elements, but just a couple of elements to change.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to access elements and modify an array.
&lt;/h2&gt;

&lt;p&gt;Each element of an array is retrieved by the array name and an index number enclosed in 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="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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&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;For&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;screen&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;groceries&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="c"&gt;// Read "Bread" on the screen&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;groceries&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="c"&gt;// Read slot 2: Eggs&lt;/span&gt;
&lt;span class="n"&gt;groceries&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="s"&gt;"Brown Bread"&lt;/span&gt; &lt;span class="c"&gt;// Change the value of the 0th element of the array.&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;groceries&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="c"&gt;// Read slot 0 again → "Brown Bread"&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;Bread
Eggs
Brown Bread
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  What are the consequences of going beyond the lines?
&lt;/h4&gt;

&lt;p&gt;If you try to access a non-existent part of a structure (for example, groceries[5] for a 3-element array), then Go will immediately exit the program with a runtime panic:&lt;br&gt;
runtime error: index out of range [5] with length 3&lt;br&gt;
Not a silent bug this time. Go lets you know what was amiss. len(array) - 1 is the final valid index in the array.&lt;/p&gt;
&lt;h2&gt;
  
  
  Getting the Length with len()
&lt;/h2&gt;

&lt;p&gt;len() returns the size (number of slots) of 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="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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Butter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Coffee"&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;"Items on list:"&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;groceries&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;code&gt;Items on list: 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If an array is declared with a size, then len() always returns the exact size, irrespective of whether there are values stored in some of the array's slots. An array is always the size that it was created to be.&lt;/p&gt;

&lt;p&gt;Also, there is an operation cap(), which is called "capacity". Arrays cannot be lengthened or shortened, but the two functions, cap() and len(), return the same value. Use len(arr) instead of literals for the size.  If you ever change the array declaration, len() adjusts automatically.&lt;/p&gt;

&lt;p&gt;When the array is of length, len() always returns the size (independent of whether or not there are zeroes in the array). An array is always allocated the memory space it was declared to have.&lt;/p&gt;
&lt;h2&gt;
  
  
  Arrays are valued, not references.
&lt;/h2&gt;

&lt;p&gt;As a result of Go's copy behavior on assigning an array to a new variable, the entire array is copied. There are two independent variables – if one changes, the other one definitely will not change at all. Let's say we think of an 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;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;backup&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;groceries&lt;/span&gt;&lt;span class="c"&gt;// This copies ALL of groceries into backup&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="n"&gt;groceries&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="s"&gt;"Brown Bread"&lt;/span&gt;&lt;span class="c"&gt;// Change the original&lt;/span&gt;
&lt;span class="err"&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;"groceries:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;groceries&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;"backup:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;backup&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&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;groceries: [Brown Bread Milk Eggs]
backup:[Bread Milk Eggs]

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

&lt;/div&gt;



&lt;p&gt;The above shows that despite replacing groceries [0] with ‘Brown Bread’, backup was not affected. This is similar to photocopying your shopping list! After they've made the photocopy, it remains unchanged if they write on the original. They started identical, but are now two completely separate pieces of paper.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sharing With Pointers
&lt;/h2&gt;

&lt;p&gt;If we want, the changes that we make with our new variable to affect the original array, it's using a pointer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;shared&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;groceries&lt;/span&gt;&lt;span class="c"&gt;// &amp;amp; means "give me the address of groceries"&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="n"&gt;groceries&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="s"&gt;"Brown Bread"&lt;/span&gt;
&lt;span class="err"&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;"groceries:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;groceries&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;"shared:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;shared&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c"&gt;// * means "follow the pointer to the value"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&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;groceries: [Brown Bread Milk Eggs]
shared:[Brown Bread Milk Eggs]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;amp;groceries make a pointer. You don't copy the array; you copy a small "address" indicating where the array is stored in memory. That is followed by the word ‘*shared follows that address to get the actual value. When Shared access is needed, Pointers are used. In most situations, you'll opt for the default copy behaviour, which is more secure and easier to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparing Arrays
&lt;/h2&gt;

&lt;p&gt;Go and share how you can use “==” and “!=” to compare two arrays of equal “size” and “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;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&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;list1&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;list2&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;list3&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Coffee"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&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;"Same?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;list2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c"&gt;// true&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;"Same?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;list1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;list3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c"&gt;// false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&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;Same? true
Same? false

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

&lt;/div&gt;



&lt;p&gt;Two arrays are equal if they are of the same type and length and the elements of the arrays in the corresponding positions are all equal. You must have both conditions.&lt;br&gt;
Comparison of differently sized arrays is not possible. The compiler will halt you, since they are of different types.&lt;/p&gt;

&lt;p&gt;Looping Through Arrays&lt;br&gt;
For small arrays, manually writing the below would work: &lt;code&gt;groceries[0], groceries[1], groceries[2]&lt;/code&gt;. Anything larger, you just use a loop to visit each element by itself. Some of the loops you can use to accomplish this are listed here:&lt;/p&gt;
&lt;h4&gt;
  
  
  The for range Loop
&lt;/h4&gt;

&lt;p&gt;The most popular looping over an array in Go is the following for range 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;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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Butter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Coffee"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;For&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;groceries&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="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Index: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" Item: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;item&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;Index: 0 Item: Bread
Index: 1 Item: Milk
Index: 2 Item: Eggs
Index: 3 Item: Butter
Index: 4 Item: Coffee
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The index will contain the number of the slot that is being modified on each iteration through the loop, and the item will contain a copy of the element being modified. The loop stops by itself when it runs out of elements. No more worrying about out of bounds.&lt;/p&gt;

&lt;p&gt;In Go programming, all the variables defined must be used. The for-range loop provides Go with two variables in its "for" clause: the index and element variable. If the index isn’t useful  to you, then you can't just leave it there intact. It will refuse to compile with Go.&lt;/p&gt;

&lt;p&gt;This is where the blank identifier &lt;code&gt;_&lt;/code&gt; is introduced. It is Go's way of letting you say, "I know this value exists, but I am deliberately throwing it away."&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="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Butter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Coffee"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&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;groceries&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;"Item:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&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;Item: Bread
Item: Milk
Item: Eggs
Item: Butter
Item: Coffee
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's also possible to discard the value and keep the index. If so, simply do not use the second variable at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;For&lt;/span&gt; &lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;groceries&lt;/span&gt; &lt;span class="n"&gt;array&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;"Index:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  A classic  for Loop.
&lt;/h4&gt;

&lt;p&gt;In the case where you require more control, such as when going in reverse or skipping an element, the classic counter-based for loop is best:&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="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Butter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Coffee"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&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="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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;groceries&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="s"&gt;"Position"&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="s"&gt;"-&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;groceries&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;​&lt;br&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;Position 0 -&amp;gt; Bread
Position 1 -&amp;gt; Milk
Position 2 -&amp;gt; Eggs
Position 3 -&amp;gt; Butter
Position 4 -&amp;gt; Coffee
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&gt;
Iterating in reverse:&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;)&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;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;--&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;groceries&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="c"&gt;//`Output: Coffee → Butter → Eggs → Milk → Bread`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Printing every other item:&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;groceries&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="m"&gt;2&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;groceries&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="c"&gt;//`Output: Bread → Eggs → Coffee (slots 0, 2, and 4)`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Multi-Dimensional Arrays
&lt;/h2&gt;

&lt;p&gt;So far, all the arrays have been a single row of values (one-dimensional). Multi-dimensional arrays (arrays of arrays) are also supported in Go.&lt;br&gt;
The most common case is a 2D array, which is a grid or a table. For instance, if I go to the shops every week and make a plan for the items I want to buy by each day and area of the shop:&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="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;// A 3-day plan with 2 items per day&lt;/span&gt;
&lt;span class="n"&gt;weeklyPlan&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="m"&gt;2&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="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="c"&gt;// Day 0&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Butter"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="c"&gt;// Day 1&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Coffee"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Fruit"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="c"&gt;// Day 2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;items&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;weeklyPlan&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Day %d: %s and %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;items&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="n"&gt;items&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="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="m"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Bread&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Milk&lt;/span&gt;
&lt;span class="n"&gt;Day&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Eggs&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Butter&lt;/span&gt;
&lt;span class="n"&gt;Day&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Coffee&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="n"&gt;Fruit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;[3][2]string&lt;/code&gt;: An array of 3 elements, each of which is an array of 2 strings. The first index tells you which row to choose, the second index tells you which column to choose:&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;weeklyPlan&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="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="s"&gt;"Bread"&lt;/span&gt;&lt;span class="c"&gt;// Day 0, first item&lt;/span&gt;
&lt;span class="n"&gt;weeklyPlan&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="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="s"&gt;"Milk"&lt;/span&gt;&lt;span class="c"&gt;// Day 0, second item&lt;/span&gt;
&lt;span class="n"&gt;weeklyPlan&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fruit"&lt;/span&gt;&lt;span class="c"&gt;// Day 2, second item&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An index position is needed to reference a particular cell, the first being the row index, and the second, the column index.&lt;br&gt;
passing an Array object to a function.&lt;br&gt;
In Go, an array passed to a function is itself a complete copy of the array; the function does not refer to the original array. Any changes the function makes don't affect the original:&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="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;printAndModify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;list&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="s"&gt;"Changed"&lt;/span&gt;&lt;span class="c"&gt;// Modifies the LOCAL copy only&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Inside:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;printAndModify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Outside:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c"&gt;// Original is unchanged&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&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;Inside: [Changed Milk Eggs]
Outside: [Bread Milk Eggs]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function takes a snapshot of an array that is independent of one another.&lt;br&gt;
If you have to change the original function, provide a pointer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="err"&gt;​&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;updateFirstItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&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;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;list&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="s"&gt;"Brown Bread"&lt;/span&gt;&lt;span class="c"&gt;// This modifies the original&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;updateFirstItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Outside:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c"&gt;// Now the original IS changed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&gt;
Expected output:&lt;br&gt;
&lt;code&gt;Outside: [Brown Bread Milk Eggs]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you have a large array greater than a few hundred elements, always use a pointer instead of copying to avoid the cost of the copy.&lt;/p&gt;
&lt;h2&gt;
  
  
  Limitations of Arrays
&lt;/h2&gt;

&lt;p&gt;Though concise and straightforward, arrays are restricted in their use and come with limitations that you'll hit hard soon.&lt;/p&gt;
&lt;h4&gt;
  
  
  The Size Is Fixed Forever
&lt;/h4&gt;

&lt;p&gt;After defining a [3]string, it will always contain exactly 3 strings, always! You can't add a fourth item. You can't remove one to shrink it. The size is baked in at compile time. If your shopping list grows to 6 items, your [5]string array can't help you.&lt;br&gt;
The array size can not be a variable, must be a literal number or named constant:&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;n&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;groceries&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&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;p&gt;The above is a compiler error because of the occurrence of the variable n.&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;Fix&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;groceries&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="c"&gt;// it Works, n is a constant&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means you can't create an array whose size depends on something you only know at runtime, like how many items the user typed in. For that, you need a slice.&lt;/p&gt;

&lt;h4&gt;
  
  
  Passing Large Arrays Is Expensive
&lt;/h4&gt;

&lt;p&gt;Every function call copies the entire array. For a [10000]string, that is 10,000 strings copied just to call a function.&lt;br&gt;
Arrays of Different Sizes Are Incompatible Types&lt;br&gt;
A function that accepts [3] strings can't be called with a [5]string. You would need two separate functions with different signatures.&lt;br&gt;
&lt;code&gt;append()&lt;/code&gt; Does Not Work on Arrays&lt;br&gt;
&lt;code&gt;append()&lt;/code&gt; — the function that adds elements to a collection — only works on slices, not arrays:&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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;groceries&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;groceries&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Butter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="c"&gt;// Compiler `error`: append expects a slice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;​&lt;br&gt;
These limitations are not bugs. Fixed-size value types are simple, predictable, and give the compiler strong guarantees. But most real-world programming needs something that can grow and shrink at runtime. That something is the slice.&lt;/p&gt;
&lt;h2&gt;
  
  
  Common Mistakes with Arrays
&lt;/h2&gt;
&lt;h4&gt;
  
  
  Mistake 1: Assigning Arrays of Different Sizes
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;list&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&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;biggerList&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;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="c"&gt;//  Compiler `error` — different types&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;​&lt;br&gt;
Fix: make sure both arrays have the same size, or use a slice if you need flexibility.&lt;br&gt;
​&lt;/p&gt;
&lt;h4&gt;
  
  
  Mistake 2: Expecting Assignment to Share Data
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;list&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;backup&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;
&lt;span class="n"&gt;backup&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="s"&gt;"Brown Bread"&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;list&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="c"&gt;// Prints "Bread", NOT "Brown Bread" — list was not changed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Arrays are value types. Assignment copies everything. If you want shared access, use &amp;amp;list to get a pointer, or switch to slices.&lt;/p&gt;

&lt;p&gt;Mistake 3: Off-by-One on the Last Element&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;groceries&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;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Bread"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Milk"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eggs"&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;groceries&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="c"&gt;//  Runtime `panic`: index out of range&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For an array of length 3, the valid indexes are 0, 1, and 2. The last valid index is always len(array) - 1.&lt;/p&gt;

</description>
      <category>go</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Structs in Go</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Thu, 04 Jun 2026 07:24:29 +0000</pubDate>
      <link>https://dev.to/frihk_ian/structs-in-go-1jff</link>
      <guid>https://dev.to/frihk_ian/structs-in-go-1jff</guid>
      <description>&lt;p&gt;Let's explore structs in Go. If you know Go's basic types like string, int, and bool, structs let you handle more complex data.&lt;br&gt;
Say you're building a store app. You need a product's name, price, and in-stock status. Using separate variables quickly becomes messy.&lt;br&gt;
Structs can simplify this. Picture structures are like a blueprint. It makes it possible to package all the related information in one neat and organized bundle.&lt;br&gt;
Now let's dive into creating and using custom data types in Go.&lt;/p&gt;
&lt;h2&gt;
  
  
  Declaring Structs
&lt;/h2&gt;

&lt;p&gt;Before using a struct, we first need to describe what it looks like. The result of defining a struct is not a piece of data, but the blueprint of data we want to store later on.&lt;br&gt;
Let's take a look at the declaration of the Product struct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;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="c"&gt;// We use the 'type' keyword to define a new custom type.&lt;/span&gt;
&lt;span class="c"&gt;// We name our type 'Product' and specify that it is a 'struct'.&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;  &lt;span class="c"&gt;// Field for the product's name&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt;   &lt;span class="kt"&gt;float64&lt;/span&gt; &lt;span class="c"&gt;// Field for the price&lt;/span&gt;
   &lt;span class="n"&gt;InStock&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;    &lt;span class="c"&gt;// Field to check if it's available&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Using the struct we just created, let's create a product. Using the struct blueprint to create a piece of data is referred to as creating an instance.&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;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt;   &lt;span class="kt"&gt;float64&lt;/span&gt;
   &lt;span class="n"&gt;InStock&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="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;// Creating an instance of Product using a "struct literal."&lt;/span&gt;
   &lt;span class="n"&gt;item1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s"&gt;"Wireless Mouse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="m"&gt;25.99&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;InStock&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c"&gt;// Creating an instance but leaving fields blank&lt;/span&gt;
   &lt;span class="n"&gt;item2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mousepad"&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;item1&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;item2&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;{Wireless Mouse 25.99 true}
{Mousepad 0 false}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might be thinking Why does item2 show 0 and false when we didn’t set them?&lt;br&gt;
This is one of Go’s most important behaviours: zero values. In Go, when you declare a variable but don’t give it a value, Go doesn’t leave it empty or undefined (which causes bugs in many other languages). Instead, Go automatically fills it with the zero value for that type.&lt;br&gt;
Since we gave item2 a name, Go automatically sets the price to 0 and in stock to false . This is one of the features in Go that make it exceptional.&lt;br&gt;
Accessing and Modifying Fields&lt;br&gt;
When you want to modify or read information from a struct, you use a dot notation. Consider the dot to be opening up a particular drawer of your filing cabinet to view a folder.&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;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt;   &lt;span class="kt"&gt;float64&lt;/span&gt;
   &lt;span class="n"&gt;InStock&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="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;item&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s"&gt;"Keyboard"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="m"&gt;45.00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;InStock&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c"&gt;// Reading a field&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;"Original Price:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="c"&gt;// Modifying a field&lt;/span&gt;
   &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;39.99&lt;/span&gt;

   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Discounted Price:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&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;Original Price: 45
Discounted Price: 39.99
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Anonymous Structs
&lt;/h2&gt;

&lt;p&gt;Sometimes we want to bundle up some information for one-time use. In such a case, we don’t want to have to go through the trouble of formally defining a whole blueprint. They are particularly useful when holding configuration settings, grouping test inputs, or temporarily organising data for a single function. We use anonymous structs in this way:&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="c"&gt;// Declaring and initializing a struct at the exact same time&lt;/span&gt;
   &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Port&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
       &lt;span class="n"&gt;Env&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="p"&gt;}{&lt;/span&gt;
       &lt;span class="n"&gt;Port&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Env&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"Production"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Running on port:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Port&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;Running on port: 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a little confusing, if you are new to Go, because you create the blueprint, then use it to create an instance. Let's look at the code snippet again.&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;config&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;blueprint&lt;/span&gt; &lt;span class="n"&gt;opening&lt;/span&gt; &lt;span class="n"&gt;brace&lt;/span&gt;
       &lt;span class="n"&gt;Port&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
       &lt;span class="n"&gt;Env&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="p"&gt;}{&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;closing&lt;/span&gt; &lt;span class="n"&gt;brace&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;blueprint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;followed&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;opening&lt;/span&gt; &lt;span class="n"&gt;brace&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
       &lt;span class="n"&gt;Port&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"8080"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Env&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;  &lt;span class="s"&gt;"Production"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;closing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think of this like defining a shape, then filling it. Keep in mind that this is a non-reusable type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested (Embedded) Structs
&lt;/h2&gt;

&lt;p&gt;Unlike other programming languages, Go has no “classes” or “inheritance”. Instead, Go employs a technique of packing smaller structs into larger structs, similar to Russian nesting dolls.&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="c"&gt;// The smaller struct&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;City&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Country&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// The larger struct&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;    &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Contact&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="c"&gt;// Embedding the Address struct here&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;player&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="n"&gt;City&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s"&gt;"Nairobi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="n"&gt;Country&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Kenya"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="p"&gt;},&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="c"&gt;// Accessing the nested data&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;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"lives in"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contact&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;City&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="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;Alice lives in Nairobi.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Struct Comparison
&lt;/h2&gt;

&lt;p&gt;A question might pop into your brain asking, “Are you able to compare two structs to determine whether they are equal or not?” Yes! Go supports the normal &lt;code&gt;==&lt;/code&gt; operator, but only if all the fields within the struct are comparable.&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;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="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;item1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mug"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12.50&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="n"&gt;item2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mug"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12.50&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="n"&gt;item3&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Mug"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15.00&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;"Are item1 and item2 identical?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;item2&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;"Are item1 and item3 identical?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;item3&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;Are item1 and item2 identical? true
Are item1 and item3 identical? false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go looks at every single field. If everything matches perfectly, the structs are considered equal.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Photocopy vs. The Original
&lt;/h2&gt;

&lt;p&gt;This is the most essential concept for beginners in Go. Go’s default behavior is to send a struct by value into a function.&lt;br&gt;
If you were to give a friend a copy of your notes, how would they look? If they add a mustache to the photocopied version, you are fine! This is the default behavior of Go. To change an original struct, the function has to be passed a pointer to it (that is, the original notebook).&lt;br&gt;
Let’s observe both of them together:&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;type&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;Name&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt;
   &lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="kt"&gt;float64&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Pass by Value (The Photocopy)&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;failedDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;5.00&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;// Pass by Pointer (The Original)&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;successfulDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;5.00&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;item&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Book"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20.00&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="n"&gt;failedDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Trying to discount the photocopy&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;"After failed discount:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="n"&gt;successfulDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c"&gt;// Giving the function the original via '&amp;amp;'&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;"After successful discount:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&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;After failed discount: 20
After successful discount: 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices &amp;amp; Common Beginner Mistakes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;In Go, names of fields that begin with a capital letter are “exported” and available to other packages (as opposed to names that start with a lowercase letter, which are “private”). If it begins with a lower-case letter (e.g., name), it belongs only to the package it is in. If in any doubt, make them capitalized so that you can use them freely.&lt;/li&gt;
&lt;li&gt;Pointers for Modification: Keep in mind the photocopy rule. If your function will call to change or update a struct, then you must pass a pointer (*).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Summary &amp;amp; Your Mini-Challenge
&lt;/h2&gt;

&lt;p&gt;You’ve made it! You’ve learned to structure unsorted variables into orderly, sensible structures. You know how to construct blueprints, make instances, navigate nested data, and update nested data correctly with pointers.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Data Types In Go</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Tue, 14 Apr 2026 08:23:50 +0000</pubDate>
      <link>https://dev.to/frihk_ian/data-types-in-go-5fm3</link>
      <guid>https://dev.to/frihk_ian/data-types-in-go-5fm3</guid>
      <description>&lt;p&gt;This is the first in a series of articles exploring data types in Go. The main goal of this series of articles is to provide detailed explanations and practical examples to help you build a clear and strong understanding of the different data types in Go. All the topics will be subdivided into manageable sections, each focusing on a specific aspect of data types, to facilitate gradual learning.&lt;/p&gt;

&lt;p&gt;There are many different data types in Go:&lt;br&gt;
The two main data types include:&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Basic(primitive) Types
&lt;/h2&gt;

&lt;p&gt;This type of data is made up of :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Numbers(int, float64)&lt;/li&gt;
&lt;li&gt;Strings(string)&lt;/li&gt;
&lt;li&gt;Boolean(bool)&lt;/li&gt;
&lt;li&gt;Composite Types&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. Composite Types
&lt;/h2&gt;

&lt;p&gt;These include &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;li&gt;Slices&lt;/li&gt;
&lt;li&gt;Maps&lt;/li&gt;
&lt;li&gt;Structs&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Other Important Types
&lt;/h2&gt;

&lt;p&gt;These are other very powerful and commonly used data types.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function types (functions are treated as values)&lt;/li&gt;
&lt;li&gt;Pointer Types (used to work with memory)&lt;/li&gt;
&lt;li&gt;Interfaces Types(used to define a certain behavior and simplifying flexibility)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, I will focus on basic data types, as they are fundamental to understanding Go and provide a solid foundation for more complex types. While the other types, such as composite, are not necessarily difficult, they involve broader concepts and all depend on the basic types. For a better understanding of each, I cover each in greater depth in separate, dedicated articles.&lt;/p&gt;
&lt;h2&gt;
  
  
  Basic(primitive) Types
&lt;/h2&gt;

&lt;p&gt;Let's delve directly into the details:&lt;/p&gt;
&lt;h3&gt;
  
  
  Numbers.
&lt;/h3&gt;

&lt;p&gt;Numbers are divided into : &lt;/p&gt;
&lt;h4&gt;
  
  
  Integers
&lt;/h4&gt;

&lt;p&gt;These are whole numbers. In  Go, we use them in two main ways :&lt;br&gt;
&lt;strong&gt;Int&lt;/strong&gt; - This is the most commonly used, including both positive and negative whole numbers.&lt;br&gt;
&lt;strong&gt;Uint&lt;/strong&gt; - Used to represent only positive whole numbers.&lt;/p&gt;
&lt;h4&gt;
  
  
  Floating-point
&lt;/h4&gt;

&lt;p&gt;These are real numbers (numbers with decimal points). They include both positive and negative numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; &lt;span class="c"&gt;// 25 is an integer &lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="kt"&gt;float32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;186.8&lt;/span&gt; &lt;span class="c"&gt;// 186.8 is a float &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Go, Floating-point can either be float32 or float64. The choice lies in the level of precision you need.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strings
&lt;/h3&gt;

&lt;p&gt;In Go, strings are immutable sequences of UTF-8 encoded bytes, meaning once created, they cannot be altered, which ensures data integrity and consistency.&lt;/p&gt;

&lt;h4&gt;
  
  
  Immutability
&lt;/h4&gt;

&lt;p&gt;Once a string is created, it cannot be changed. Any attempt to change it creates a new string. &lt;/p&gt;

&lt;h4&gt;
  
  
  Indexing
&lt;/h4&gt;

&lt;p&gt;The individual bytes can be accessed with a given 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;s&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello"&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;s&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="c"&gt;// 72 (byte value of 'H')&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print 72 as the strings store bytes and H is equal to the ASCII (byte) code of 72.&lt;/p&gt;

&lt;h4&gt;
  
  
  Length
&lt;/h4&gt;

&lt;p&gt;The number of bytes, not characters, is returned by the len() 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="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="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Unicode and Runes
&lt;/h4&gt;

&lt;p&gt;As the strings are UTF-8, some characters can occupy more than one byte. to act on characters (runes), use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&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;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;r&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;code&gt;72&lt;br&gt;
101&lt;br&gt;
108&lt;br&gt;
108&lt;br&gt;
111&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  String Concatenation
&lt;/h4&gt;

&lt;p&gt;The + operator can be used to add strings:&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;full&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt;  &lt;span class="c"&gt;// Hello World&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To Byte Slice. &lt;br&gt;
You can convert a string to a slice of bytes:&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;b&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Boolean
&lt;/h3&gt;

&lt;p&gt;In Go, boolean values represent truth values; they can only be written as either true or false. They are used mostly for comparison purposes and control a program flow in if, for, or switch statements. In Go, by default, an uninitialized boolean has the value false, making it predictable and easy to work with in decision-making logic.&lt;/p&gt;

&lt;p&gt;The section has links to the data types used so far. A link to the next article will be added once it’s ready. When a link is not given on a given type of data, simply means that it is still in the process and it will be given shortly.&lt;br&gt;
&lt;a href="https://dev.to/frihk_ian/maps-in-golang-4jif"&gt;Maps&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/frihk_ian/structs-in-go-1jff"&gt;structs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additional learning materials.&lt;br&gt;
&lt;a href="https://go.dev/tour/basics/11" rel="noopener noreferrer"&gt;Go Documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.w3schools.com/go/go_data_types.php" rel="noopener noreferrer"&gt;W3schools&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Maps in Golang</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Tue, 10 Mar 2026 07:23:29 +0000</pubDate>
      <link>https://dev.to/frihk_ian/maps-in-golang-4jif</link>
      <guid>https://dev.to/frihk_ian/maps-in-golang-4jif</guid>
      <description>&lt;p&gt;Maps are one of the most flexible data types in Go. It might even be the most flexible compared to maps from other programming languages. And just as flexible as they are , they are also as simple to initialize.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Map Structure&lt;/strong&gt;&lt;br&gt;
A map in Go is composed of two main parts: the key and the value. The key is defined as a unique label used to store and find a specific &lt;br&gt;
Maps are one of the main data structures in Go. Maps have two main parts : the key and the value. The key is like a word you are looking up in a dictionary. It is a unique label that stores and carries a specific type of data. Whilst the value is like the definition of the word you are looking up in a dictionary, it is the data that is stored in the key value. Unlike keys, which only accept comparable types, the value can be of any legal Go type.&lt;/p&gt;

&lt;p&gt;In Go, there are two main ways to initialize maps. The first uses the Go built-in make function:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;m := make(map[string]int)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The second one uses a map literal:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;m := map[string]int{}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;They initialize an empty map that takes in a string as the key and an integer as the value. The make method is more flexible because it allows you to specify an initial capacity. When the expected size is known, this preallocation can reduce map growth and rehashing during execution.&lt;/p&gt;

&lt;p&gt;Accessing Values and Handling Missing Keys&lt;br&gt;
Reading from a map in Go is quite simple. You access a value by indexing the map with its key, as shown below:&lt;br&gt;
&lt;code&gt;age := m["John"]&lt;/code&gt;&lt;br&gt;
Sometimes you might call a key that is not in the map; this happens as a result of misspelling or during testing. Go does not display any error, nor does it crash. It instead displays the zero value of the map's value 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;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;m&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;value&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"missing_key"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c"&gt;// value is 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;Print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program above displays:&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;The “Comma ok” Idiom&lt;/strong&gt;&lt;br&gt;
This can be misleading; if a key has a value of zero, it becomes difficult to know whether the key has a zero value or its value is a zero. Go handles this by introducing the “ok” to help solve this error. This helps distinguish the erroneous value and the correct value.&lt;/p&gt;

&lt;p&gt;value, ok := m["missing_key"]&lt;br&gt;
if !ok {&lt;br&gt;
    Return “Error” // The key does not exist in the map&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Here, the value is the data in the map value. Whilst the “ok” is a boolean that is true. If the value exist then the ok becomes true, and the program executes without any error. If it does not exist, then the program returns an “error.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inserting and Updating Data&lt;/strong&gt;&lt;br&gt;
In Go, the syntax for inserting, updating, or deleting data in a map is consistent and straightforward. When adding/ updating a key, you simply assign a value; the method is pretty much the same as shown below.&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;m&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="c"&gt;// Insert: Key "Alice" does not exist, so it is created.&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt; 

&lt;span class="c"&gt;// Update: Key "Alice" already exists, so its value is overwritten.&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;26&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Deleting Elements&lt;/strong&gt;&lt;br&gt;
When deleting from a map, we use the Go built-in function delete(). It removes the key and everything associated with it. Below is its syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;m&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;"Alice"&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;"Bob"&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="c"&gt;// Removes "Alice" from the map&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;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The delete function has a safety feature that makes it very safe to work with. In case of deletion of something that is not in the map, it simply does nothing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Iterating Over Maps&lt;/strong&gt;&lt;br&gt;
Now that you know how to add and remove individual items, you’ll likely want to loop through the entire collection. In Go, we use the range keyword to iterate over a map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;m&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="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Alice&lt;/span&gt;&lt;span class="err"&gt;”&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="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Bob&lt;/span&gt;&lt;span class="err"&gt;”&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="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Charlie&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;35&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;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&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;m&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="err"&gt;”&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;value&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;Something to keep in mind when it comes to maps is that they are  “commitment-phobic” when it comes to order. Unlike arrays or slices, they do not have a specified order during iteration. If you run the code above three times, chances are that you might get three different sequences. If you need a consistent order, you’ll have to sort the keys manually in a separate slice first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maps as Reference Types&lt;/strong&gt;&lt;br&gt;
The most significant points to keep in mind are that maps are a type of reference. Passing a map to a function does not give a copy of the data, but the pointer to the underlying data structure.&lt;br&gt;
This implies that when you make changes to a map within a function, the changes will be carried over to the original map.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;updateAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&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="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;Alice&lt;/span&gt;&lt;span class="err"&gt;”&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="c"&gt;// This changes the original map!&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;Map Performance and Concurrency&lt;/strong&gt;&lt;br&gt;
Maps are incredibly fast, offering O(1) average time complexity for searches, additions, and deletions. Maps are extremely quick with an average time complexity of O(1) for searches, add, and delete. There is, however, a kind of safety limit that they have: Maps are not thread-safe.&lt;br&gt;
If two different goroutines try to write to the same map at the same time, your program will crash with a fatal error: concurrent map writes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature 
Behavior in Golang Maps
Search Speed
Extremely fast (Constant time)
Iteration Order
Random/Unordered
Thread Safety
None (Requires sync.Mutex or sync.Map)
Zero Value
nil (A nil map can be read, but writing to it causes a panic)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Maps are the go-to tool in Go when you need a fast, associative data structure. By mastering the make function, the “comma ok” idiom, and understanding the nuances of iteration and reference types, you can handle complex data relationships with very little code.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Go templates</title>
      <dc:creator>Frihk Ian</dc:creator>
      <pubDate>Wed, 11 Feb 2026 12:23:08 +0000</pubDate>
      <link>https://dev.to/frihk_ian/go-templates-51nm</link>
      <guid>https://dev.to/frihk_ian/go-templates-51nm</guid>
      <description>&lt;p&gt;The first thing to handle is answering the question, "what are Go templates?" Go templates are a way to create dynamic content in Go by allowing the user to mix data with plain text or HTML files with data received from the Go code. This makes it possible to change the data in a template very easily, as it can be edited with ease. They are mainly used in web dev to create web pages that rely on user inputs or content from a database.&lt;br&gt;
A template is just a file that has placeholders for data. In Go, placeholders are written using double curly braces {{ }}. These curly braces can hold a user’s data, and this can be shown by {{ .name }}. The dot refers to the data object you refer to from Go. When the template is running, Go replaces the {{ .name }} with the actual user name from the user's input or the database.&lt;br&gt;
To use templates in Go, a data structure that can help you hold the data to show, usually a map or a struct, is required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type PageData struct {
    Title   string
    Message string
}

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

&lt;/div&gt;



&lt;p&gt;Then you parse the template file in your Go code and execute it with your data. The template system will automatically replace all placeholders with the correct values. This keeps your HTML or text separate from your Go code, making your program cleaner and easier to maintain.&lt;br&gt;
A good example of template file is as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;{{ .Title }}&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;{{ .Title }}&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;{{ .Message }}&amp;lt;/p&amp;gt;

    {{ if .IsLogged }}
        &amp;lt;p&amp;gt;Welcome back, {{ .User }}! &amp;lt;/p&amp;gt;
    {{ else }}
        &amp;lt;p&amp;gt;Please login to continue. &amp;lt;/p&amp;gt;
    {{ end }}

    &amp;lt;h2&amp;gt;Your Tasks:&amp;lt;/h2&amp;gt;
    &amp;lt;ul&amp;gt;
        {{ range .Tasks }}
            &amp;lt;li&amp;gt;{{ . }}&amp;lt;/li&amp;gt;
        {{ else }}
            &amp;lt;li&amp;gt;No tasks available.&amp;lt;/li&amp;gt;
        {{ end }}
    &amp;lt;/ul&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go templates also support basic logic. This helps to manipulate the values to be displayed. You can display certain things if a certain condition is met. You can also range over maps, structs, among others. For example, you can display a specific message to only logged-in users, you can list all the content of a check list from a shopping list among many others. You can also use functions like len to make the content more flexible.&lt;br&gt;
Below is an example of Go code that displays its output to the HTML file above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "html/template"
    "net/http"
)

type PageData struct {
    Title    string
    Message  string
    IsLogged bool
    User     string
    Tasks    []string
}

func handler(w http.ResponseWriter, r *http.Request) {
    data := PageData{
        Title:    "Go Templates Advanced Example",
        Message:  "Using conditions and loops in templates",
        IsLogged: true,
        User:     "Ian",
        Tasks:    []string{"Learn Go templates", "Build a web app", "Practice Go daily"},
    }

    tmpl := template.Must(template.ParseFiles("index.html"))
    tmpl.Execute(w, data)
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In summary, to generate a dynamic text or HTML in Go, the best option is the Go template. They help separate the design of your output from your code logic, allow you to reuse templates with different data, and make it easy to build web pages, reports, or any text-based content.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>go</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
