<?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 Bala subramaniyan on Medium]]></title>
        <description><![CDATA[Stories by Bala subramaniyan on Medium]]></description>
        <link>https://medium.com/@balagetme?source=rss-995c94b9b173------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*I5WhVqOM7FGHKKP_eZGuCg.jpeg</url>
            <title>Stories by Bala subramaniyan on Medium</title>
            <link>https://medium.com/@balagetme?source=rss-995c94b9b173------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:50:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@balagetme/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[IOTA — Create Effective Constants in Golang]]></title>
            <link>https://medium.com/swlh/iota-create-effective-constants-in-golang-b399f94aac31?source=rss-995c94b9b173------2</link>
            <guid isPermaLink="false">https://medium.com/p/b399f94aac31</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Bala subramaniyan]]></dc:creator>
            <pubDate>Sun, 06 Sep 2020 04:51:19 GMT</pubDate>
            <atom:updated>2020-09-07T18:13:27.151Z</atom:updated>
            <content:encoded><![CDATA[<h3>iota — Create Effective Constants in Golang</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*48Qax54ZXAYPUPdja7qwgw.png" /></figure><p>Some concepts have names, and sometimes we care about those names, even (or especially) in our code.</p><p>At other times, we only care to distinguish one thing from the other. There are times when there’s no inherently meaningful value for a thing. For example, if we’re storing products in a database table we probably don’t want to store their category as a string. We don’t care how the categories are named, and besides, marketing changes the names all the time.</p><p>We care only that they’re distinct from each other.</p><pre>const (<br>    CategoryBooks    = 0<br>    CategoryHealth   = 1<br>    CategoryClothing = 2<br>)</pre><p>Instead of 0, 1, and 2 we could have chosen 17, 43, and 61. The values are arbitrary.Constants are important but they can be hard to reason about and difficult to maintain.</p><h3>iota</h3><p>iota makes it easy to declare sequentially growing numeric constants in Go:</p><pre>const (<br>	Low = iota<br>	Medium<br>	High<br>)<br>fmt.Printf(&quot;Low: %d\nMedium: %d\nHigh: %d\n&quot;, Low, Medium, High)</pre><p>iota sets the value of Low to 0 and instructs the compiler that the following constants have increasing numeric values.</p><pre>Low: 0<br>Medium: 1<br>High: 2</pre><h3>Operations with iota</h3><p>It’s possible to make operation when using iotas</p><pre>package mainimport &quot;fmt&quot;const (<br>    _         = iota<br>    a         = <strong>iota + 10</strong><br>    b int     = <strong>iota * 10</strong><br>    c float64 = <strong>iota + 5.1</strong><br>)func main() {<br>   fmt.Printf(&quot;%v — %T\n&quot;, a, a)<br>   fmt.Printf(&quot;%v — %T\n&quot;, b, b)<br>   fmt.Printf(&quot;%v — %T\n&quot;, c, c)<br>}</pre><pre><strong>Output=&gt;</strong><br>11 — int<br>20 — int<br>8.1 — float64</pre><h3>Resetting iota increment</h3><p>Every time a keyword Const appears in the code, iota’s increment is reset.</p><pre>package main<br>import &quot;fmt&quot;<br>const (<br>   a = iota<br>   b int = iota<br>)<br><strong>const (<br>   c = iota<br>)<br></strong>func main() {<br>   fmt.Printf(&quot;%v — %T\n&quot;, a, a)<br>   fmt.Printf(&quot;%v — %T\n&quot;, b, b)<br><strong>   fmt.Printf(&quot;%v — %T\n&quot;, c, c)</strong><br>}</pre><pre><strong>Output=&gt;<br></strong>0 — int<br>1 — int<br><strong>0 — int</strong></pre><h3>Creating bitmask values with iota</h3><p>Iota can be very useful when creating a bitmask. For instance, to represent the state of a network connection which may be secure, authenticated, and/or ready, we might create a bitmask like the following:</p><pre>const (<br>	Secure = 1 &lt;&lt; iota <em>// 0b001<br></em>	Authn              <em>// 0b010<br></em>	Ready              <em>// 0b100<br></em>)</pre><pre><em>// 0b011: Connection is secure and authenticated, but not yet Ready</em><br>ConnState := Secure | Authn <em><br></em><br>fmt.Printf(`Secure:    0x%x (0b%03b)<br>Authn:     0x%x (0b%03b)<br>ConnState: 0x%x (0b%03b)<br>`, Secure, Secure, Authn, Authn, ConnState, ConnState)</pre><pre><strong>Output =&gt;</strong><br>Secure:    0x1 (0b001)<br>Authn:     0x2 (0b010)<br>ConnState: 0x3 (0b011)</pre><h3>Skipping values</h3><p>The value of iota is still incremented for every entry in a constant list even if iota is not used:</p><pre>const ( <em>// iota is reset to 0<br></em>	a = 1 &lt;&lt; iota <em>// a == 1<br></em>	b = 1 &lt;&lt; iota <em>// b == 2<br></em>	c = 3         <em>// c == 3  (iota is not used but still  <br>                                  incremented)<br></em>	d = 1 &lt;&lt; iota <em>// d == 8<br></em>)<br>fmt.Printf(&quot;a: %d, b: %d, c: %d, d: %d\n&quot;, a, b, c, d)</pre><pre><strong>Output=&gt;</strong> <br>a: 1, b: 2, c: 3, d: 8</pre><p>It will also be incremented even if no constant is created at all, meaning the empty identifier can be used to skip values entirely:</p><pre>const (<br>	a = iota <em>// a = 0<br></em>	_        <em>// iota is incremented  to 1<br></em>	b        <em>// b = 2<br></em>)<br>fmt.Printf(&quot;a: %d, b: %d\n&quot;, a, b)</pre><pre><strong>Output=&gt;<br></strong>a: 0, b: 2</pre><h3>Using iota in an expression list</h3><p>Because iota is incremented after each <a href="https://golang.org/ref/spec#ConstSpec">ConstSpec</a>, values within the same expression list will have the same value for iota:</p><pre>const (<br>	bit0, mask0 = 1 &lt;&lt; iota, 1&lt;&lt;iota - 1 <em>//bit0 == 1, mask0 == 0<br></em>	bit1, mask1                          <em>//bit1 == 2, mask1 == 1<br></em>	_, _                                 <em>//skips iota == 2<br></em>	bit3, mask3                          <em>//bit3 == 8, mask3 == 7<br></em>)<br>fmt.Printf(&quot;bit0: %d, mask0: 0x%x\n&quot;, bit0, mask0)<br>fmt.Printf(&quot;bit3: %d, mask3: 0x%x\n&quot;, bit3, mask3)</pre><pre><strong>Output=&gt;<br></strong>bit0: 1, mask0: 0x0<br>bit3: 8, mask3: 0x7</pre><h3>Using iota in an expression</h3><p>iota can be used in expressions, so it can also be used to assign values other than simple incrementing integers starting from zero. To create constants for SI units, use this example from <a href="https://golang.org/doc/effective_go.html#initialization">Effective Go</a>:</p><pre>type ByteSize int<br><br>const (<br>	_           = iota <em>//ignore first value by assigning to <br>                           //blank identifier<br></em>	KB ByteSize = 1 &lt;&lt; (10 * iota)<br>	MB<br>	GB<br>	TB<br>	PB<br>)<br>fmt.Printf(&quot;KB: %d\n&quot;, KB)</pre><pre><strong>Output=&gt;<br></strong>KB: 1024</pre><h3>Emulating enums</h3><p>Go doesn’t have a syntax for enumerations, but you can emulate it with constants and a naming scheme.</p><p>Consider a C++ enum:</p><pre>enum {<br>    tagHtml,<br>    tagBody,<br>    taDiv<br>};</pre><p>In Go you can do it as:</p><pre>const (<br>    tagBody = iota<br>    tagDiv<br>    tagHr<br>)</pre><p>This declares 3 untyped, numeric constants. iota starts numbering of constants. tagBody has value of 0, tagDiv a value of 1 etc. This is convenient way to assign unique numeric values.</p><p>If the values matter, you can assign them explicitly:</p><pre>const (<br>	one   = 1<br>	five  = 5<br>	seven = 7<br>)</pre><h3>Adding type safety</h3><p>In the above example tagBody etc. is an untyped constant so it can be assigned to any numeric type.</p><p>We can define a unique type for our enum:</p><pre>type HTMLTag int</pre><pre>const (<br>    tagBody HTMLTag = iota<br>    tagDiv<br>    tagHr<br>)</pre><h3>Conclusion</h3><p>I am leaving you with this, to build upon and get hands on experience.</p><p>That’s it! I hope this short post about Go iota was. useful! Of course, all comments are welcome.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b399f94aac31" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/iota-create-effective-constants-in-golang-b399f94aac31">IOTA — Create Effective Constants in Golang</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Big Int in Go : Handling large numbers is easy]]></title>
            <link>https://blog.devgenius.io/big-int-in-go-handling-large-numbers-is-easy-157cb272dd4f?source=rss-995c94b9b173------2</link>
            <guid isPermaLink="false">https://medium.com/p/157cb272dd4f</guid>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[bigint]]></category>
            <dc:creator><![CDATA[Bala subramaniyan]]></dc:creator>
            <pubDate>Sun, 28 Jun 2020 14:27:52 GMT</pubDate>
            <atom:updated>2020-09-07T17:45:23.209Z</atom:updated>
            <content:encoded><![CDATA[<h3>Big Int in Go : Handling large numbers is easy</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*2_Xc-fL_3zWlq21zmhK9rg.jpeg" /></figure><p>At times we would want to do mathematical operations which involves very big integer calculations that are outside the limit of all available primitive data types. For example factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. Golang doesn’t check for overflows implicitly and so this may lead to unexpected results when a number larger than 64 bits are stored in a int64.</p><p>To solve this problem, Go provides the Package “big” which implements arbitrary-precision arithmetic (big numbers).</p><h3>Description</h3><p>The following numeric types are supported:</p><pre>Int    signed integers<br>Rat    rational numbers<br>Float  floating-point numbers</pre><p>The zero value for an Int, Rat, or Float correspond to 0. Thus, new values can be declared in the usual ways and denote 0 without further initialization:</p><pre>var x Int        // &amp;x is an *Int of value 0<br>var r = &amp;Rat{}   // r is a *Rat of value 0<br>y := new(Float)  // y is a *Float of value 0</pre><h4>Factory Functions</h4><p>Alternatively, new values can be allocated and initialized with factory functions of the form:</p><pre>func NewT(v V) *T</pre><p>For instance, NewInt(x) returns an *Int set to the value of the int64 argument x, NewRat(a, b) returns a *Rat set to the fraction a/b where a and b are int64 values, and NewFloat(f) returns a *Float initialized to the float64 argument f. More flexibility is provided with explicit setters, for instance:</p><pre>var z1 Int<br>z1.SetUint64(123)                 // z1 := 123<br>z2 := new(Rat).SetFloat64(1.25)   // z2 := 5/4<br>z3 := new(Float).SetInt(z1)       // z3 := 123.0</pre><h3>Usage: Fibonacci number</h3><p>Now lets dive in with an example</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8617088c987cc4b82b8fafee9048c889/href">https://medium.com/media/8617088c987cc4b82b8fafee9048c889/href</a></iframe><p>This example demonstrates how to use big.Int to compute the smallest Fibonacci number with 100 decimal digits and to test whether it is prime. This gives an overview on the usage of big package.</p><h3>Converting to and from other types</h3><p>The other difficulty I face while working with the big package is the type conversions. Type conversion is something that we do regularly when we write code to develop applications.</p><p>The data available for us to use as an input to the big package is often the primitive data types. The most straightforward method is transferring this data in string format, usually as properties of some JSON object. Here are some examples for converting values to and from big.Int.</p><h4>Converting from int</h4><p>Converting an int to big.Int is simple, but has to pass through int64, like so:</p><pre>newBigInt := big.newInt(int64(someInt))</pre><h4>Converting to int</h4><p>To convert a big integer to uint64, run this code:</p><pre>var smallnum, _ = new(big.Int).SetString(&quot;2188824200011112223&quot;, 10)<br>num := smallnum.Uint64()</pre><p>Note that Go does not perform bounds checking — if the value of the big integer does not fit inside a uint64 you will not get any warning and <strong>num</strong> will overflow and hold an incorrect value.</p><p>To convert to a signed 64-bit integer use Int64()instead of Uint64().</p><h4>Converting from a string</h4><p>Assuming you hold a big integer as a string, you create a big.Int out of it like so:</p><pre><strong>var </strong>bignum, ok = new(big.Int).SetString(&quot;218882428714186575617&quot;, 0)</pre><p>If the string input tosetString() starts with “0x”, base 16 (hexadecimal) will be used. If the string starts with “0” base 8 (octal) will be used. Otherwise it will use base 10 (decimal). You can also specify the base manually (up to 62). The returned value is of type *big.Int. If Go fails to create the big.Int, <strong>ok</strong> is set to <em>false</em>.</p><h4>Converting to a string</h4><p>Converting to a string lets you serialize as JSON, print to console, and more, so it is very useful.</p><p>To convert a big.Int to a string in hex notation, use:</p><pre>str1 := prime1.Text(16) // or: str1 := fmt.Sprintf(&quot;0x%x&quot;, bigInt)</pre><p>To convert to a decimal notation, use:</p><pre>str1 := prime1.Text(10) // fmt.Sprintf(&quot;%v&quot;, bigInt)</pre><h3>Conclusion</h3><p>I am leaving you with this, to build upon and get hands on experience.</p><p>That’s it! I hope this short post about Go Big Integers was. useful! Of course, all comments are welcome.</p><h3>Reference</h3><p><a href="https://golang.org/pkg/math/big/">big - The Go Programming Language</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=157cb272dd4f" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/big-int-in-go-handling-large-numbers-is-easy-157cb272dd4f">Big Int in Go : Handling large numbers is easy</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>