<?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>Forem: Toluwase Thomas</title>
    <description>The latest articles on Forem by Toluwase Thomas (@toluwasethomas).</description>
    <link>https://forem.com/toluwasethomas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F394349%2Fae2c4d16-8456-4931-ad30-de1bd62fdf2a.jpg</url>
      <title>Forem: Toluwase Thomas</title>
      <link>https://forem.com/toluwasethomas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/toluwasethomas"/>
    <language>en</language>
    <item>
      <title>Simple way to obtain largest Number in an array or slice in golang</title>
      <dc:creator>Toluwase Thomas</dc:creator>
      <pubDate>Thu, 13 Jun 2024 00:09:08 +0000</pubDate>
      <link>https://forem.com/toluwasethomas/simple-way-to-obtain-largest-number-in-an-array-or-slice-in-golang-2o06</link>
      <guid>https://forem.com/toluwasethomas/simple-way-to-obtain-largest-number-in-an-array-or-slice-in-golang-2o06</guid>
      <description>&lt;h1&gt;
  
  
  🚀 Step-by-Step Guide to Find the Largest Number in an Array in Go
&lt;/h1&gt;

&lt;p&gt;Finding the largest number in an array is a common task in programming. Let's walk through a simple yet effective approach to accomplish this in Go.&lt;/p&gt;

&lt;h2&gt;
  
  
  📝 Steps to Follow
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1️⃣ Loop through Each Value
&lt;/h3&gt;

&lt;p&gt;Iterate through each element of the array or slice.&lt;/p&gt;

&lt;h3&gt;
  
  
  2️⃣ Declare the Initial Largest Value
&lt;/h3&gt;

&lt;p&gt;Initialize a variable &lt;code&gt;largest&lt;/code&gt; with the first element of the array:&lt;br&gt;
largest := array[0]&lt;/p&gt;
&lt;h3&gt;
  
  
  3️⃣ Compare the Current Largest Value with Other Numbers
&lt;/h3&gt;

&lt;p&gt;Compare the current &lt;code&gt;largest&lt;/code&gt; value with each element in the array.&lt;/p&gt;
&lt;h3&gt;
  
  
  4️⃣ Update the Largest Value
&lt;/h3&gt;

&lt;p&gt;Whenever you find a number greater than the current &lt;code&gt;largest&lt;/code&gt; value, update &lt;code&gt;largest&lt;/code&gt; to this new number.&lt;/p&gt;
&lt;h3&gt;
  
  
  5️⃣ Return the Largest Value
&lt;/h3&gt;

&lt;p&gt;After completing the loop, return the &lt;code&gt;largest&lt;/code&gt; number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func findLargestNumber(nums []int) int {
    if len(nums) == 0 {
        return 0 // handle empty slice case
    }
    largest := nums[0] // Step 2
    for i := 1; i &amp;lt; len(nums); i++ { // Step 1
        if nums[i] &amp;gt; largest { // Step 3
            largest = nums[i] // Step 4
        }
    }
    return largest // Step 5
}

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

&lt;/div&gt;



&lt;p&gt;Testing the function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func TestFindLargestNumber(t *testing.T) {
    tests := []struct {
        name     string
        numbers  []int
        expected int
    }{
        {
            name:     "Mixed positive numbers",
            numbers:  []int{45, 22, 68, 90, 12},
            expected: 90,
        },
        {
            name:     "All negative numbers",
            numbers:  []int{-5, -23, -1, -55},
            expected: -1,
        },
        {
            name:     "All zeros",
            numbers:  []int{0, 0, 0, 0, 0},
            expected: 0,
        },
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got := findLargestNumber(tt.numbers)
            if got != tt.expected {
                t.Errorf("findLargestNumber(%v) = %v, want %v", tt.numbers, got, tt.expected)
            }
        })
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Main Function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func main() {
    arr := []int{45, 22, 68, 90, 12}
    fmt.Println(findLargestNumber(arr)) // Output: 90
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading.&lt;br&gt;
Kindly like and share other methods you know of in the comments below.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>go</category>
      <category>arrays</category>
    </item>
    <item>
      <title>simple ways to sum an array of numbers in golang</title>
      <dc:creator>Toluwase Thomas</dc:creator>
      <pubDate>Wed, 12 Jun 2024 01:23:53 +0000</pubDate>
      <link>https://forem.com/toluwasethomas/simple-ways-to-sum-an-array-of-numbers-in-golang-1chb</link>
      <guid>https://forem.com/toluwasethomas/simple-ways-to-sum-an-array-of-numbers-in-golang-1chb</guid>
      <description>&lt;p&gt;Step 1: Loop through the Array&lt;br&gt;
Loop through each element of the array to access its values.&lt;/p&gt;

&lt;p&gt;Step 2: Declare the result Variable&lt;br&gt;
Declare a variable result to store the cumulative sum. The type of this variable should match the type of the elements in the array (e.g., int, float64, etc.).&lt;/p&gt;

&lt;p&gt;Step 3: Accumulate the Sum&lt;br&gt;
Use the result variable to cumulatively sum each value in the array. For example: result += array[i].&lt;/p&gt;

&lt;p&gt;Step 4: Return the Result&lt;br&gt;
After the loop completes, return the result variable which now contains the sum of all elements in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func sumArray(numbers []int) int {
    result := 0
    for i := 0; i &amp;lt; len(numbers); i++ {
        result += numbers[i]
    }
    return result
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Testing our function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func TestSumArray(t *testing.T) {
    tests := []struct {
        name     string
        numbers  []int
        expected int
    }{
        {
            name:     "Positive numbers",
            numbers:  []int{1, 2, 3, 4, 5},
            expected: 15,
        },
        {
            name:     "Mixed numbers",
            numbers:  []int{-3, 4, -1, 0, 2},
            expected: 2,
        },
        {
            name:     "Single number",
            numbers:  []int{10},
            expected: 10,
        },
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got := sumArray(tt.numbers)
            if got != tt.expected {
                t.Errorf("sumArray(%v) = %v, want %v", tt.numbers, got, tt.expected)
            }
        })
    }
}

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

&lt;/div&gt;



&lt;p&gt;Run your test with &lt;code&gt;go test ./...&lt;/code&gt; command via your terminal or use the play button via your IDE&lt;/p&gt;

&lt;p&gt;Thanks for reading. Please Like and leave a comment,&lt;/p&gt;

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