• Home
  • ES6
  • ADVANCED
    • Javascript Array Methods
    • Javascript String Methods
    • Javascript Regex
    • ES Next
  • JS BOM
  • JS DOM
  • WEB API
  • SNIPPETS
  • TYPESCRIPT
  • Golang
  • Dark Mode Light Mode
  • » golang » Go Arrays
    <h2 id="go-arrays">Go Arrays</h2> <p>Arrays are used to store multiple values of the same type in a single variable, instead of declaring separate variables for each value.</p> <hr> <h2 id="declare-an-array">Declare an Array</h2> <p>In Go, there are two ways to declare an array:</p> <h4 id="1-with-the-var-keyword">1. With the <code>var</code> keyword:</h4> <h3 id="syntax">Syntax</h3> <pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main <span class="hljs-keyword">import</span> (<span class="hljs-string">&quot;fmt&quot;</span>) <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> { <span class="hljs-keyword">var</span> arr1 = [<span class="hljs-number">3</span>]<span class="hljs-type">int</span>{<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>} arr2 := [<span class="hljs-number">5</span>]<span class="hljs-type">int</span>{<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>} fmt.Println(arr1) fmt.Println(arr2) } </code></pre> <pre><code class="hljs language-sh">[1 2 3] [4 5 6 7 8] </code></pre> <h3 id="example">Example</h3> <p>This example declares two arrays (arr1 and arr2) with inferred lengths:</p> <pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main <span class="hljs-keyword">import</span> (<span class="hljs-string">&quot;fmt&quot;</span>) <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> { <span class="hljs-keyword">var</span> arr1 = [...]<span class="hljs-type">int</span>{<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>} arr2 := [...]<span class="hljs-type">int</span>{<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">7</span>,<span class="hljs-number">8</span>} fmt.Println(arr1) fmt.Println(arr2) } </code></pre> <h3 id="example-1">Example</h3> <p>This example declares an array of strings:</p> <pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main <span class="hljs-keyword">import</span> (<span class="hljs-string">&quot;fmt&quot;</span>) <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> { <span class="hljs-keyword">var</span> cars = [<span class="hljs-number">4</span>]<span class="hljs-type">string</span>{<span class="hljs-string">&quot;Volvo&quot;</span>, <span class="hljs-string">&quot;BMW&quot;</span>, <span class="hljs-string">&quot;Ford&quot;</span>, <span class="hljs-string">&quot;Mazda&quot;</span>} fmt.Print(cars) } </code></pre> <h2 id="access-elements-of-an-array">Access Elements of an Array</h2> <p>You can access a specific array element by referring to the index number.</p> <p>In Go, array indexes start at 0. That means that [0] is the first element, [1] is the second element, etc.</p> <h3 id="example-2">Example</h3> <p>This example shows how to access the first and third elements in the prices array:</p> <pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main <span class="hljs-keyword">import</span> (<span class="hljs-string">&quot;fmt&quot;</span>) <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> { prices := [<span class="hljs-number">3</span>]<span class="hljs-type">int</span>{<span class="hljs-number">10</span>,<span class="hljs-number">20</span>,<span class="hljs-number">30</span>} fmt.Println(prices[<span class="hljs-number">0</span>]) fmt.Println(prices[<span class="hljs-number">2</span>]) } </code></pre> <h2 id="change-elements-of-an-array">Change Elements of an Array</h2> <p>You can also change the value of a specific array element by referring to the index number.</p> <h3 id="example-3">Example</h3> <p>This example shows how to change the value of the third element in the prices array: </p> <pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main <span class="hljs-keyword">import</span> (<span class="hljs-string">&quot;fmt&quot;</span>) <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> { prices := [<span class="hljs-number">3</span>]<span class="hljs-type">int</span>{<span class="hljs-number">10</span>,<span class="hljs-number">20</span>,<span class="hljs-number">30</span>} prices[<span class="hljs-number">2</span>] = <span class="hljs-number">50</span> fmt.Println(prices) } </code></pre> <p>Result:</p> <pre><code class="hljs language-sh">[10 20 50] </code></pre> <h2 id="find-the-length-of-an-array">Find the Length of an Array</h2> <p>The <code>len()</code> function is used to find the length of an array:</p> <h3 id="example-4">Example</h3> <pre><code class="hljs language-go"><span class="hljs-keyword">package</span> main <span class="hljs-keyword">import</span> (<span class="hljs-string">&quot;fmt&quot;</span>) <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> { arr1 := [<span class="hljs-number">4</span>]<span class="hljs-type">string</span>{<span class="hljs-string">&quot;Volvo&quot;</span>, <span class="hljs-string">&quot;BMW&quot;</span>, <span class="hljs-string">&quot;Ford&quot;</span>, <span class="hljs-string">&quot;Mazda&quot;</span>} arr2 := [...]<span class="hljs-type">int</span>{<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>} fmt.Println(<span class="hljs-built_in">len</span>(arr1)) fmt.Println(<span class="hljs-built_in">len</span>(arr2)) } </code></pre> <p>Result:</p> <pre><code>46 </code></pre>

    Go(Golang) Tutorial

    Go Introduction

  • What is Go?
  • Go Get Started
  • Go Syntax
  • Go Variables
  • Go Arrays
  • About Me

    • Made by Rasel Mahmud
    • Portfolio

    Top References

    • javascripttutorial.net
    • W3school.com

    Top Tutorials

    • Primitive vs. Reference Values
    • What is JavaScript

    NOTE:

    This website only for Developing purpose not for business. and all content copied from other place like javascripttutorial.net and W3school.com