<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ustra Júnior on Medium]]></title>
        <description><![CDATA[Stories by Ustra Júnior on Medium]]></description>
        <link>https://medium.com/@ustrajunior?source=rss-ca9637471183------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*kb3lMJuSUVxhoc4D.jpg</url>
            <title>Stories by Ustra Júnior on Medium</title>
            <link>https://medium.com/@ustrajunior?source=rss-ca9637471183------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 06:36:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ustrajunior/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Variadic functions in Go]]></title>
            <link>https://medium.com/@ustrajunior/variadic-functions-in-go-6ecb5814f604?source=rss-ca9637471183------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ecb5814f604</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[variadic-functions]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ustra Júnior]]></dc:creator>
            <pubDate>Wed, 02 Nov 2016 22:02:44 GMT</pubDate>
            <atom:updated>2016-11-02T22:02:44.576Z</atom:updated>
            <content:encoded><![CDATA[<p>Variadic functions in Go</p><p>Let’s create a function that we can pass 3 numbers as arguments and them it returns the average of this numbers.</p><pre><strong>func</strong> <strong>average</strong>(num1, num2, num3 <strong>float64</strong>) <strong>float64</strong> {<br>	avg := (num1 + num2 + num3) / 3</pre><pre>	<strong>return</strong> avg<br>}</pre><p>and then, you call it like this</p><pre><strong>func</strong> <strong>main</strong>() {<br>	fmt.Println(average(2, 4, 6))<br>}</pre><p>Le’ts run to see if works</p><pre>$ <strong>go</strong> run average.<strong>go</strong><br>4</pre><p>It works. Super easy, but not so useful. Normally, when you want to calculate the average, you don’t know how many arguments you will need to accept. How can we handle this?</p><p>When you have a function that you can’t determinate the number of arguments you are going to pass then you need a Variadic function.</p><p>So, to do this, we just need use ‘…’ before the parameter type on the function.</p><pre><strong>func</strong> <strong>average</strong>(numbers ...<strong>float64</strong>) <strong>float64</strong> {<br> // ...<br>}</pre><p>But now, we don’t have separated values anymore, the <em>…float64</em> will be transformed in a slice of float64 and we need to iterate over the items to calculate the average.</p><pre><strong>func</strong> <strong>average</strong>(numbers ...<strong>float64</strong>) <strong>float64</strong> {<br>	<strong>var</strong> total <strong>float64</strong><br>	<strong>for</strong> _, num := <strong>range</strong> numbers {<br>		total += num<br>	}</pre><pre>	<strong>return</strong> total / <strong>float64</strong>(len(numbers))<br>}</pre><p>This is a Variadic function in Go and we can pass as many arguments we want.</p><pre><strong>func</strong> <strong>main</strong>() {<br>	fmt.Println(average(2, 4, 6, 9, 5, 3, 12))<br>}</pre><p><em>Originally published at </em><a href="https://ustrajunior.com/blog/variadic-functions-in-go/"><em>ustrajunior.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ecb5814f604" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>