# Introduction To Numeric Constants In Go

- Published: April 6, 2014
- Author: William Kennedy
- URL: https://www.ardanlabs.com/blog/2014/04/introduction-to-numeric-constants-in-go.html

<b><span style="font-size: large;">Introduction</span></b><br />
One of the more unique features of Go is how the language implements constants. The rules for <a href="http://golang.org/ref/spec#Constants" target="_blank">constants</a> in the language specification are unique to Go. They provide the flexibility Go needs at the compiler level to make the code we write readable and intuitive while still maintaining a type safe language.<br />
<br />
This post will attempt to build a foundation for what numeric constants are, how they behave in their simplest form and how best to talk about them. There are a lot of little nuances, terms and concepts that can trip us up. Because of this, the post is going to take things slowly.<br />
<br />
So if you are ready to peek under the covers just a bit, roll up your sleeve and let’s get started: <br />
<br />
<b><span style="font-size: large;">Untyped and Typed Numeric Constants</span></b><br />
Constants can be declared with or without a type in Go. When we declare literal values in our code, we are actually declaring constants that are both untyped and unnamed.<br />
<br />
The following examples show typed and untyped numeric constants that are both named and unnamed:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>const</b> untypedInteger &nbsp; &nbsp; &nbsp; = <b><span style="color: #38761d;">12345</span></b><br />
<b>const</b> untypedFloatingPoint = <b><span style="color: #38761d;">3.141592</span></b><br />
<br />
<b>const</b> typedInteger       <b><span style="color: #0b5394;">int</span></b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = <span style="color: #38761d;"><b>12345</b></span><br />
<b>const</b> typedFloatingPoint <b><span style="color: #0b5394;">float64</span></b> = <span style="color: #38761d;"><b>3.141592</b></span></div>
<br />
The constants on the left hand side of the declaration are named constants and the literal values on the right hand side are unnamed constants.<br />
<br />
<b><span style="font-size: large;">Kinds of Numeric Constants</span></b><br />
Your first instinct may be to think that typed constants use the same type system as variables, but they don’t. Constants have their own implementation for representing the values that we associate with them. Every Go <a href="http://golang.org/cmd/" target="_blank">compiler</a> has the flexibility to implement constants as they wish, within a set of mandatory <a href="http://golang.org/ref/spec#Constants" target="_blank">requirements</a>.<br />
<br />
When declaring a typed constant, the declared type is used to associate the type’s precision limitations. It does not change how the value is being internally represented. Because the internal representation of constants can be different between the different compilers, it is best to think of constants as having a <i><b>kind</b></i>, not a type.<br />
<br />
Numeric constants can be one of four kinds:  integer, floating-point, complex and rune:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<span style="color: #38761d;"><b>12345</b></span> &nbsp; &nbsp;<span style="color: #783f04;">// kind: integer</span><br />
<span style="color: #38761d;"><b>3.141592</b></span>      <span style="color: #783f04;">// kind: floating-point</span><br />
<span style="color: #38761d;"><b>1E6 &nbsp; &nbsp;&nbsp;</b></span>&nbsp;<span style="color: #783f04;">// kind: floating-point</span></div>
<br />
In the example above, we have declared three numeric constants, one of kind integer and two of kind floating-point. The form of the literal value will determine what kind the constant takes. When the form of the literal value contains a decimal or exponent, the constant is of kind floating-point. When the form does not contain a decimal or exponent, the constant is of kind integer.<br />
<br />
<b><span style="font-size: large;">Constants Are Mathematically Exact</span></b><br />
Regardless of the implementation, constants are always considered to be mathematically exact. This is something that makes constants in Go unique. This is not the case in other languages like C and C++.<br />
<br />
Integers can always be represented precisely when there is enough memory to store their entire value. Since the specification requires integer constants to have at least 256 bits of precision, we are safe in saying integer constants are mathematically exact.<br />
<br />
To have mathematically exact floating-point numbers, there are different strategies and options that the compiler can employ. The specification does not state how the compiler must do this, it just specifies a set of mandatory requirements that need to be met.<br />
<br />
Here are two strategies that the different Go compilers use today to implement mathematically exact floating-point numbers:<br />
<ul>
<li style="color: black;">One strategy is to represent all floating-point numbers as fractions, and use rational arithmetic on those fractions. This is what go/types does today and these floating-point numbers never have any loss of precision.</li>
<li style="color: black;">Another strategy is to use floating-point numbers with so much precision that they appear to be exact for all practical purposes. When we use floating-point numbers with several hundred bits, the difference between exact and approximate becomes virtually non-existent. This is what the gc/gccgo compilers do today.</li>
</ul>
As developers however, it is best to not consider what internal representation is being used by the compiler, it is irrelevant. Just remember that all constants, regardless if they are declared with or without a type, use the same representation to store their values, which is not the same as variables and is mathematically exact.<br />
<br />
<b><span style="font-size: large;">Mathematically Exact Example</span></b><br />
Since constants only exist during compilation, it is hard to provide an example that shows constants are mathematically exact. One way is to show how the compiler will let us declare constants of kind integer with values that are much larger than the largest integer types can support.<br />
<br />
Here is a program that can be compiled because constants of kind integer are mathematically exact:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>package</b> main<br />
<br />
<b>import</b> <span style="color: #990000;">&quot;fmt&quot;</span><br />
<br />
<span style="color: #783f04;">// Much larger value than int64.</span><br />
<b>const</b> myConst = <span style="color: #38761d;"><b>9223372036854775808543522345</b></span><br />
<br />
<b>func</b> main() {<br />
&nbsp; &nbsp; fmt.Println(<span style="color: #990000;">&quot;Will Compile&quot;</span>)<br />
}
</div>
<br />
If we change the constant to be of type <span style="font-family: Courier New, Courier, monospace;">int64</span>, which means the constant is now bound to the precision limitations of a 64 bit integer, the program will no longer compile:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>package</b> main<br />
<br />
<b>import</b> <span style="color: #990000;">&quot;fmt&quot;</span><br />
<br />
<span style="color: #783f04;">// Much larger value than int64.</span><br />
<b>const</b> myConst <b><span style="color: #0b5394;">int64</span></b> = <b><span style="color: #38761d;">9223372036854775808543522345</span></b><br />
<br />
<b>func</b> main() {<br />
&nbsp; &nbsp; fmt.Println(<span style="color: #990000;">&quot;Will NOT Compile&quot;</span>)<br />
}<br />
<br />
<span style="color: #660000;"><b>Compiler Error:</b></span><br />
<span style="color: #660000;">./ideal.go:6: constant 9223372036854775808543522345 overflows int64
</span></div>
<br />
Here we can see that constants of kind integer can represent very large numbers and why we say they are mathematically exact.<br />
<br />
<b><span style="font-size: large;">Numeric Constant Declarations</span></b><br />
When we declare an untyped numeric constant, there are no type constraints that must be met by the constant value:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>const</b> untypedInteger &nbsp; &nbsp; &nbsp; = <span style="color: #38761d;"><b>12345</b></span> &nbsp; &nbsp;<span style="color: #783f04;">// kind: integer</span><br />
<b>const</b> untypedFloatingPoint = <span style="color: #38761d;"><b>3.141592</b></span> <span style="color: #783f04;">// kind: floating-point
</span></div>
<br />
In each case, the untyped constant on the left hand side of the declaration is given the same kind and value as the constant on the right.<br />
<br />
When we declare a typed constant, the constant on the right hand side of the declaration must use a form that is compatible with the declared type on the left:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>const</b> typedInteger       <b><span style="color: #134f5c;">int</span></b> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = <b><span style="color: #38761d;">12345</span></b> &nbsp; &nbsp;// kind: integer<br />
<b>const</b> typedFloatingPoint <b><span style="color: #134f5c;">float64</span></b> = <span style="color: #38761d;"><b>3.141592</b></span> // kind: floating-point
</div>
<br />
The value on the right hand side of the declaration must also fit into the range for the declared type. For instance, this numeric constant declaration is invalid:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>const</b> myUint8&nbsp;<b><span style="color: #134f5c;">uint8</span></b> = <span style="color: #38761d;"><b>1000
</b></span></div>
<br />
<span style="font-family: Courier New, Courier, monospace;">uint8</span> only can represent numbers from 0 to 255. This is what I mean when I said earlier that the declared type is used to associate the type’s precision limitations.<br />
<br />
<b><span style="font-size: large;">Implicit Integer Type Conversions</span></b><br />
In Go there are no implicit type <a href="http://golang.org/ref/spec#Conversions" target="_blank">conversions</a> between variables. However, implicit type conversions between variables and constants can happen regularly by the compiler.<br />
<br />
Let’s start with an implicit integer conversion:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>var</b> myInt <b><span style="color: #134f5c;">int</span></b> = <span style="color: #38761d;"><b>123
</b></span></div>
<br />
In this example we have constant <span style="font-family: Courier New, Courier, monospace;">123</span> of kind integer being implicitly converted to a value of type <span style="font-family: Courier New, Courier, monospace;">int</span>. Since the form of the constant is not using a decimal point or exponent, the constant takes the kind integer. Constants of kind integer can be implicitly converted into signed and unsigned integer variables of any length as long as no truncation needs to take place.<br />
<br />
Constants of kind floating-point can also be implicitly converted into integer variables if the constant uses a form that is compatible with the integer type:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>var</b> myInt <b><span style="color: #134f5c;">int</span></b> = <b><span style="color: #38761d;">123.0
</span></b></div>
<br />
We can also perform implicit type conversion assignments without declaring an explicit type for the variable:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>var</b> myInt = <span style="color: #38761d;"><b>123
</b></span></div>
<br />
In this case, the default type of <span style="font-family: Courier New, Courier, monospace;">int64</span> is used to initialize the variable being assigned with constant <span style="font-family: Courier New, Courier, monospace;">123</span> of kind integer.<br />
<br />
<b><span style="font-size: large;">Implicit Floating-Point Type Conversions</span></b><br />
Next let’s look at an implicit floating-point conversion:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>var</b> myFloat <b><span style="color: #134f5c;">float64</span></b> = <b><span style="color: #38761d;">0.333
</span></b></div>
<br />
This time the compiler is performing an implicit conversion between constant <span style="font-family: Courier New, Courier, monospace;">0.333</span> of kind floating-point to a variable of type <span style="font-family: Courier New, Courier, monospace;">float64</span>. Since the form of the constant is using a decimal point, the constant takes the kind floating-point. The default type for a variable initialized with a constant of kind floating-point is <span style="font-family: Courier New, Courier, monospace;">float64</span>.<br />
<br />
The compiler can also perform implicit conversions between constants of kind integer to variables of type <span style="font-family: Courier New, Courier, monospace;">float64</span>:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>var</b> myFloat <b><span style="color: #134f5c;">float64</span></b> = <b><span style="color: #38761d;">1
</span></b></div>
<br />
In this example, constant <span style="font-family: Courier New, Courier, monospace;">1</span> of kind integer is implicitly converted to a variable of type <span style="font-family: Courier New, Courier, monospace;">float64</span>.<br />
<br />
<b><span style="font-size: large;">Kind Promotion</span></b><br />
Performing constant arithmetic between other constants and variables is something we do quite often in our programs. It follows the rules for <a href="http://golang.org/ref/spec#Operators" target="_blank">binary operators</a> in the specification. The rule states that operand types must be identical unless the operation involves shifts or untyped constants.<br />
<br />
Let’s look at an example of two constants that are multiplied together:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>var</b> answer = <span style="color: #38761d;"><b>3</b></span> <b>&#42;</b> <span style="color: #38761d;"><b>0.333
</b></span></div>
<br />
In this example we perform multiplication between constant <span style="font-family: Courier New, Courier, monospace;">3</span> of kind integer and constant <span style="font-family: Courier New, Courier, monospace;">0.333</span> of kind floating-point.<br />
<br />
There is a rule in the specification about constant expressions that is specific to this operation:<br />
<br />
<i>&quot;Except for shift operation, if the operands of a binary operation are <u>different kinds of untyped constants</u>, ..., the result <u>use the kind that appears later in this list</u>: integer, rune, floating-point, complex.&quot;</i><br />
<br />
Based on this rule, the result of the multiplication between these two constants will be a constant of kind floating-point. Kind floating-point is being promoted ahead of kind integer based on the rule.<br />
<br />
<b><span style="font-size: large;">Numeric Constant Arithmetic</span></b><br />
Let’s continue with our multiplication example:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>var</b> answer = <b><span style="color: #38761d;">3</span></b> <b>&#42;</b> <b><span style="color: #38761d;">0.333
</span></b></div>
<br />
The result of the multiplication will be a new constant of kind floating-point. That constant is then assigned to the variable <span style="font-family: Courier New, Courier, monospace;">answer</span> through an implicit type conversion from kind floating-point to <span style="font-family: Courier New, Courier, monospace;">float64</span>.<br />
<br />
When we divide numeric constants, the kind of the constants determine how the division is performed.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>const</b> third = <b><span style="color: #38761d;">1</span></b> <b>/</b> <b><span style="color: #38761d;">3.0
</span></b></div>
<br />
When one of the two constants are of kind floating-point, the result of the division will also be a constant of kind floating-point. In our example we have used a decimal point to represent the constant in the denominator. This follows the rules for kind promotion that we talked about before.<br />
<br />
Let’s take the same example but use kind integer in the denominator:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>const</b> zero = <b><span style="color: #38761d;">1</span></b> <b>/</b> <b><span style="color: #38761d;">3
</span></b></div>
<br />
This time we are performing division between two constants of kind integer. The result of the division will be a new constant of kind integer. Since dividing 3 into the value of 1 represents a number that is less than 1, the result of this division is constant <span style="font-family: Courier New, Courier, monospace;">0</span> of kind integer.<br />
<br />
Let’s create a typed constant using numeric constant arithmetic:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>type</b> Numbers <b><span style="color: #134f5c;">int8</span></b><br />
<b>const</b> One Numbers = <span style="color: #38761d;"><b>1</b></span><br />
<b>const</b> Two &nbsp; &nbsp; &nbsp; &nbsp; = <span style="color: #38761d;"><b>2</b></span> <b>&#42;</b> One</div>
<br />
Here we declare a new type called <span style="font-family: Courier New, Courier, monospace;">Numbers</span> with a base type of <span style="font-family: Courier New, Courier, monospace;">int8</span>. Then we declare constant <span style="font-family: Courier New, Courier, monospace;">One</span> with type <span style="font-family: Courier New, Courier, monospace;">Numbers</span> and assign constant <span style="font-family: Courier New, Courier, monospace;">1</span> of kind integer. Next we declare constant <span style="font-family: Courier New, Courier, monospace;">Two</span> which is promoted to type <span style="font-family: Courier New, Courier, monospace;">Numbers</span> through the multiplication of constant <span style="font-family: Courier New, Courier, monospace;">2</span> of kind integer and constant <span style="font-family: Courier New, Courier, monospace;">One</span> of type <span style="font-family: Courier New, Courier, monospace;">Numbers</span>.<br />
<br />
The declaration of constant <span style="font-family: Courier New, Courier, monospace;">Two</span> shows an example of a constant getting promoted not just to a user-defined type, but a user-defined type associated with a base type.<br />
<br />
<b><span style="font-size: large;">One Practical Example</span></b><br />
Let’s look at one practical example right from the standard library. The time package declares this type and set of constants:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>type</b> Duration <b><span style="color: #134f5c;">int64</span></b><br />
<br />
<b>const</b> (<br />
&nbsp; &nbsp; Nanosecond  Duration = <span style="color: #38761d;"><b>1</b></span><br />
&nbsp; &nbsp; Microsecond &nbsp; &nbsp; &nbsp; &nbsp; = <b><span style="color: #38761d;">1000</span></b> <b>&#42;</b> Nanosecond<br />
&nbsp; &nbsp; Millisecond &nbsp; &nbsp; &nbsp; &nbsp; = <b><span style="color: #38761d;">1000</span></b> <b>&#42;</b> Microsecond<br />
&nbsp; &nbsp; Second &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= <b><span style="color: #38761d;">1000</span></b> <b>&#42;</b> Millisecond<br />
)</div>
<br />
All of the constants declared above are constants of type <span style="font-family: Courier New, Courier, monospace;">Duration</span> which have a base type of <span style="font-family: Courier New, Courier, monospace;">int64</span>. Here we are declaring typed constants using constant arithmetic between constants that are typed and untyped.<br />
<br />
Since the compiler will perform implicit conversions for constants, we can write code in Go like this:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>package</b> main<br />
<br />
<b>import</b> (<br />
&nbsp; &nbsp; <span style="color: #990000;">&quot;fmt&quot;</span><br />
&nbsp; &nbsp; <span style="color: #990000;">&quot;time&quot;</span><br />
)<br />
<br />
<b>const</b> fiveSeconds = <b><span style="color: #38761d;">5</span></b> <b>&#42;</b> time.Second<br />
<br />
<b>func</b> main() {<br />
&nbsp; &nbsp;&nbsp;now := time.Now()<br />
&nbsp; &nbsp;&nbsp;lessFiveNanoseconds := now.Add(<b><span style="color: #38761d;">-5</span></b>)<br />
&nbsp; &nbsp;&nbsp;lessFiveSeconds := now.Add(-fiveSeconds)<br />
<br />
&nbsp; &nbsp;&nbsp;fmt.Printf(<span style="color: #990000;">&quot;Now &nbsp; &nbsp; : %v\n&quot;</span>, now)<br />
&nbsp; &nbsp;&nbsp;fmt.Printf(<span style="color: #990000;">&quot;Nano &nbsp; &nbsp;: %v\n&quot;</span>, lessFiveNanoseconds)<br />
&nbsp; &nbsp;&nbsp;fmt.Printf(<span style="color: #990000;">&quot;Seconds : %v\n&quot;</span>, lessFiveSeconds)<br />
}<br />
<br />
<span style="color: #4c1130;"><b>Output:</b></span><br />
<span style="color: #4c1130;">Now &nbsp; &nbsp; : 2014-03-27 13:30:49.111038384 -0400 EDT</span><br />
<span style="color: #4c1130;">Nano &nbsp; &nbsp;: 2014-03-27 13:30:49.111038379 -0400 EDT</span><br />
<span style="color: #4c1130;">Seconds : 2014-03-27 13:30:44.111038384 -0400 EDT</span></div>
<br />
The power of constants are exhibited with the method calls to <span style="font-family: Courier New, Courier, monospace;">Add</span>. Let’s look at the definition of the <span style="font-family: Courier New, Courier, monospace;">Add</span> method for the receiver type <span style="font-family: Courier New, Courier, monospace;">Time</span>:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>func</b> (t Time) Add(d Duration) Time
</div>
<br />
The <span style="font-family: Courier New, Courier, monospace;">Add</span> method accepts a single parameter of type <span style="font-family: Courier New, Courier, monospace;">Duration</span>. Let’s look closer at the method calls to <span style="font-family: Courier New, Courier, monospace;">Add</span> from our program:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>var</b> lessFiveNanoseconds = now.Add(<span style="color: #38761d;"><b>-5</b></span>)<br />
<b>var</b> lessFiveMinutes = now.Add(-fiveSeconds)
</div>
<br />
The compiler is implicitly converting constant <span style="font-family: Courier New, Courier, monospace;">-5</span> into a variable of type&nbsp;<span style="font-family: 'Courier New', Courier, monospace;">Duration</span>&nbsp;to allow the method call to&nbsp;happen. Constant <span style="font-family: Courier New, Courier, monospace;">fiveSeconds</span> is already of type&nbsp;<span style="font-family: 'Courier New', Courier, monospace;">Duration</span>&nbsp;thanks to the rules for constant&nbsp;arithmetic:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>const</b> fiveSeconds = <b><span style="color: #38761d;">5</span></b> <b>&#42;</b> time.Second
</div>
<br />
The multiplication between constant <span style="font-family: Courier New, Courier, monospace;">5</span> and <span style="font-family: Courier New, Courier, monospace;">time.Second</span> results in constant <span style="font-family: Courier New, Courier, monospace;">fiveSeconds</span> becoming a constant of type <span style="font-family: Courier New, Courier, monospace;">Duration</span>. This is because constant <span style="font-family: Courier New, Courier, monospace;">time.Second</span> is of type <span style="font-family: Courier New, Courier, monospace;">Duration</span> and this type is promoted when determining the type of the result. To support the function call, the constant still needs to be implicitly converted from a constant of type <span style="font-family: Courier New, Courier, monospace;">Duration</span> to a variable of type <span style="font-family: Courier New, Courier, monospace;">Duration</span>.<br />
<br />
If constants didn't behave the way they do, these kind of assignments and function calls would always require explicit conversions. Look at what happens when we try to use a value of type <span style="font-family: Courier New, Courier, monospace;">int</span> to make the same method call:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<b>var</b> difference <b><span style="color: #134f5c;">int</span></b> = <b><span style="color: #38761d;">-5</span></b><br />
<b>var</b> lessFiveNano = now.Add(difference)<br />
<br />
<span style="color: #990000;"><b>Compiler Error:</b></span><br />
<span style="color: #990000;">./const.go:16: cannot use difference (type int) as type time.Duration in functi</span>on argument
</div>
<br />
Once we use a typed integer value as the parameter for the <span style="font-family: Courier New, Courier, monospace;">Add</span> method call, we received a compiler error. The compiler will not allow implicit type conversions between typed variables. For that code to compile we would need to perform an explicit type conversion:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Add(time.Duration(difference))
</div>
<br />
Constants are the only mechanism we have to write code without the need to use explicit type conversions.<br />
<br />
<b><span style="font-size: large;">Conclusion</span></b><br />
We take the behavior of constants for granted, which is a testament to the language designers and those who have worked hard on this feature. A lot of work and care has gone into making constants work this way and the benefits are hopefully clear.<br />
<br />
So the next time you are working with a constant, remember you are working with something that is unique. A hidden gem buried in the compiler that doesn’t get enough credit or recognition as a unique feature of Go. Constants help make coding in Go fun and the code we write readable and intuitive. While at the same time keeping the code we write type safe.<br />
<br />
<b><span style="font-size: large;">Thanks</span></b><br />
Thanks to&nbsp;
[Nate Finch](/broken-link) and <a href="http://www.westryn.net/resumes/kim.html" target="_blank">Kim Shrier</a>&nbsp;who have provided several reviews of the post that have helped to make sure the content and examples were accurate, flowed well and would be interesting to Go developers. I was ready to give up a few times and Nate’s encouragement kept me going.<br />
<br />
Special thanks to Robert Griesemer and others on the Go dev team for their time and patience in teaching me the subject matter. The Go dev team is filled with an amazing group of people who really care about the community and the people who are a part of it. Thanks!!<br />
<br />
