<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Complete Coding</title>
	
	<link>http://kevinrodrigues.com/blog</link>
	<description>A blog about programming and software development</description>
	<lastBuildDate>Thu, 26 Aug 2010 16:30:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/completecoding1" /><feedburner:info uri="completecoding1" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/2.0/</creativeCommons:license><image><link>http://creativecommons.org/licenses/by-nc-sa/2.0/</link><url>http://creativecommons.org/images/public/somerights20.gif</url><title>Some Rights Reserved</title></image><feedburner:emailServiceId>completecoding1</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Complete Coding has moved. Please update your feeds.</title>
		<link>http://feedproxy.google.com/~r/completecoding1/~3/AdBmqPYa3Yw/</link>
		<comments>http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 16:30:22 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[general]]></category>

		<guid isPermaLink="false">http://kevinrodrigues.com/blog/?p=993</guid>
		<description><![CDATA[Complete Coding has moved to another host. Kindly update your bookmarks to http://rkevin.com/code Also please update the RSS feed to http://feeds.feedburner.com/rkevin/code Related Posts: No Related Posts &#160; Subscribe to the comments for this post? Share this on del.icio.us Share this on Facebook Post on Google Buzz Promote this on Orkut Share this on Reddit Digg [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		var dzone_url = "http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/";
		var dzone_title = "Complete Coding has moved. Please update your feeds.";
		var dzone_style = "1";
		var dzone_blurb = "";
		//-->
		</script>
		<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script></div><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin-left: 5px;">
			<a rel="nofollow" target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F08%2F26%2Fcomplete-coding-has-moved-please-update-your-feeds%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F08%2F26%2Fcomplete-coding-has-moved-please-update-your-feeds%2F&amp;source=rodrigueskevin&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Complete Coding has moved to another host. Kindly update your bookmarks to <a rel="nofollow" target="_blank" title="Complete Coding" href="http://rkevin.com/code" target="_self">http://rkevin.com/code</a></p>
<p>Also please update the RSS feed to <a rel="nofollow" target="_blank" title="Complete Coding RSS" href="http://feeds.feedburner.com/rkevin/code" target="_self">http://feeds.feedburner.com/rkevin/code</a><br />
<h3 class='related_post_title'>Related Posts:</h3>
<ul class='related_post'>
<li>No Related Posts</li>
</ul>
<div style="clear:both;">&nbsp;</div>

<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/&amp;title=Complete+Coding+has+moved.+Please+update+your+feeds." rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/&amp;t=Complete+Coding+has+moved.+Please+update+your+feeds." rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-orkut">
			<a href="http://promote.orkut.com/preview?nt=orkut.com&amp;tt=Complete+Coding+has+moved.+Please+update+your+feeds.&amp;du=http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/&amp;cn=Complete%20Coding%20has%20moved%20to%20another%20host.%20Kindly%20update%20your%20bookmarks%20to%20http%3A%2F%2Frkevin.com%2Fcode%0D%0A%0D%0AAlso%20please%20update%20the%20RSS%20feed%20to%20http%3A%2F%2Ffeeds.feedburner.com%2Frkevin%2Fcode" rel="nofollow" class="external" title="Promote this on Orkut">Promote this on Orkut</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/&amp;title=Complete+Coding+has+moved.+Please+update+your+feeds." rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/&amp;title=Complete+Coding+has+moved.+Please+update+your+feeds." rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/&amp;title=Complete+Coding+has+moved.+Please+update+your+feeds.&amp;description=Complete%20Coding%20has%20moved%20to%20another%20host.%20Kindly%20update%20your%20bookmarks%20to%20http%3A%2F%2Frkevin.com%2Fcode%0D%0A%0D%0AAlso%20please%20update%20the%20RSS%20feed%20to%20http%3A%2F%2Ffeeds.feedburner.com%2Frkevin%2Fcode" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Complete+Coding+has+moved.+Please+update+your+feeds.+-+http://b2l.me/ak74dm&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>


<p><a href="http://feedads.g.doubleclick.net/~a/lgqOvGMRw4hWSpvKz9DEBbLTy6Q/0/da"><img src="http://feedads.g.doubleclick.net/~a/lgqOvGMRw4hWSpvKz9DEBbLTy6Q/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/lgqOvGMRw4hWSpvKz9DEBbLTy6Q/1/da"><img src="http://feedads.g.doubleclick.net/~a/lgqOvGMRw4hWSpvKz9DEBbLTy6Q/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/completecoding1/~4/AdBmqPYa3Yw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kevinrodrigues.com/blog/2010/08/26/complete-coding-has-moved-please-update-your-feeds/</feedburner:origLink></item>
		<item>
		<title>How To Use Arrays and Slices in Google Go</title>
		<link>http://feedproxy.google.com/~r/completecoding1/~3/IHNeaNOIPWo/</link>
		<comments>http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 17:03:50 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Go Programming]]></category>
		<category><![CDATA[google go]]></category>

		<guid isPermaLink="false">http://kevinrodrigues.com/blog/?p=990</guid>
		<description><![CDATA[Array An array is a collection of like objects. In Google Go programming, arrays are declared as, var arrayOfInt [10]int The length is part of the array&#8217;s type and must be a constant expression that evaluates to a non-negative integer value. The length of array a can be discovered using the built-in function len(a). The [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		var dzone_url = "http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/";
		var dzone_title = "How To Use Arrays and Slices in Google Go";
		var dzone_style = "1";
		var dzone_blurb = "";
		//-->
		</script>
		<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script></div><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin-left: 5px;">
			<a rel="nofollow" target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F08%2F05%2Fhow-to-use-arrays-and-slices-in-google-go%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F08%2F05%2Fhow-to-use-arrays-and-slices-in-google-go%2F&amp;source=rodrigueskevin&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>Array</strong><br />
An array is a collection of like objects. In Google Go programming, arrays are declared as,</p>
<pre class="prettyprint">
var arrayOfInt [10]int
</pre>
<p>The length is part of the array&#8217;s type and must be a constant expression that evaluates to a non-negative integer value. The length of array a can be discovered using the built-in function len(a). The elements can be indexed by integer indices 0 through the len(a)-1. If the array is indexed beyond len(a)-1, then we get an out of bounds error.</p>
<p>In C, arrayOfInt would be usable as a pointer to int. In Go, since arrays are values, we have to use a pointer to an array. What this means is that in Google Go,</p>
<pre class="prettyprint">
var pArray *int = &#038;arrayOfInt //pArray is a pointer to an integer.
var pArray *[10]int = &#038;arrayOfInt //pArray is a pointer to an array of 10 integers.
</pre>
<p>In the declaration of the pointer to an array, the size of the array is mandatory. When assigning the address of an array to a pointer variable, the &#038; referencing is mandatory.</p>
<p>Arrays are values. Assigning one array to another copies all the elements. In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it. The value property can be useful but also expensive; if you want C-like behavior and efficiency, you can pass a pointer to the array.</p>
<pre class="prettyprint">
package main 

import "fmt" 

func arrayLen (pArray *[5]int) int{
   return len(pArray)
} 

func main(){
   var arrayOfInt [5]int 

   arrayOfInt[0] = 0
   arrayOfInt[1] = 1
   arrayOfInt[2] = 2
   arrayOfInt[3] = 3
   arrayOfInt[4] = 4 

   fmt.Println("Array Length = ", arrayLen(&#038;arrayOfInt))
}
</pre>
<p>If you are creating a regular array but want the compiler to count the elements for you, use &#8230; as the array size:</p>
<pre class="prettyprint">
s := sum(&#038;[...]int{1,2,3})
</pre>
<p>A type followed by a brace-bounded expression—is a constructor for a value, in this case an array of 3 ints.</p>
<p><strong>Slice</strong><br />
Slices wrap arrays to give a more general, powerful, and convenient interface to sequences of data. Slices can be considered as a sub-section of an array. Slices are reference types, which means that if you assign one slice to another, both refer to the same underlying array. For instance, if a function takes a slice argument, changes it makes to the elements of the slice will be visible to the caller, analogous to passing a pointer to the underlying array. </p>
<p>We can declare a slice variable, by assigning a pointer to any array with the same element type, </p>
<pre class="prettyprint">
var arrayOfInt [5]int
var slice []int = &#038;arrayOfInt
</pre>
<p>or by a slice expression of the form a[low : high], representing the subarray indexed by low through high-1. </p>
<pre class="prettyprint">
var arrayOfInt [5]int
slice = arrayOfInt[1:3]
</pre>
<p>Slices look a lot like arrays but have no explicit size ([] vs. [10]) and they reference a segment of an underlying, often anonymous, regular array. Multiple slices can share data if they represent pieces of the same array; multiple arrays can never share data.</p>
<p>When passing an array to a function, you almost always want to declare the formal parameter to be a slice. When you call the function, take the address of the array and Go will create (efficiently) a slice reference and pass that.</p>
<p>Using slices one can write the earlier program as:</p>
<pre class="prettyprint">
package main

import "fmt"

func arrayLen (slice []int) int{
   return len(slice)
}

func main(){
   var arrayOfInt [5]int

   arrayOfInt[0] = 0
   arrayOfInt[1] = 1
   arrayOfInt[2] = 2
   arrayOfInt[3] = 3
   arrayOfInt[4] = 4

   fmt.Println("Array Length = ", arrayLen(&#038;arrayOfInt))

}
</pre>
<p>We pass the pointer to arrayLen() by (implicitly) promoting it to a slice.</p>
<p>Like arrays, slices are indexable and have a length. The length of a slice s can be discovered by the built-in function len(s); unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array.<br />
<h3 class='related_post_title'>Related Posts:</h3>
<ul class='related_post'>
<li><a href='http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/' title='How To Define Functions in Google Go'>How To Define Functions in Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/' title='How To Use Strings In Google Go'>How To Use Strings In Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/' title='How To Define Constants in Google Go'>How To Define Constants in Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/28/how-to-define-variables-in-google-go/' title='How To Define Variables In Google Go'>How To Define Variables In Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/26/how-to-write-a-hello-world-program-in-google-go/' title='How To Write A Hello World Program In Google Go'>How To Write A Hello World Program In Google Go</a></li>
</ul>
<div style="clear:both;">&nbsp;</div>

<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/&amp;title=How+To+Use+Arrays+and+Slices+in+Google+Go" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/&amp;t=How+To+Use+Arrays+and+Slices+in+Google+Go" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-orkut">
			<a href="http://promote.orkut.com/preview?nt=orkut.com&amp;tt=How+To+Use+Arrays+and+Slices+in+Google+Go&amp;du=http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/&amp;cn=Array%0D%0AAn%20array%20is%20a%20collection%20of%20like%20objects.%20In%20Google%20Go%20programming%2C%20arrays%20are%20declared%20as%2C%0D%0A%0D%0Avar%20arrayOfInt%20%5B10%5Dint%0D%0A%0D%0AThe%20length%20is%20part%20of%20the%20array%27s%20type%20and%20must%20be%20a%20constant%20expression%20that%20evaluates%20to%20a%20non-negative%20integer%20value.%20The%20length%20of%20array%20a%20can%20be%20discovered%20using%20the%20b" rel="nofollow" class="external" title="Promote this on Orkut">Promote this on Orkut</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/&amp;title=How+To+Use+Arrays+and+Slices+in+Google+Go" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/&amp;title=How+To+Use+Arrays+and+Slices+in+Google+Go" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/&amp;title=How+To+Use+Arrays+and+Slices+in+Google+Go&amp;description=Array%0D%0AAn%20array%20is%20a%20collection%20of%20like%20objects.%20In%20Google%20Go%20programming%2C%20arrays%20are%20declared%20as%2C%0D%0A%0D%0Avar%20arrayOfInt%20%5B10%5Dint%0D%0A%0D%0AThe%20length%20is%20part%20of%20the%20array%27s%20type%20and%20must%20be%20a%20constant%20expression%20that%20evaluates%20to%20a%20non-negative%20integer%20value.%20The%20length%20of%20array%20a%20can%20be%20discovered%20using%20the%20b" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=How+To+Use+Arrays+and+Slices+in+Google+Go+-+http://b2l.me/ae84td&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>


<p><a href="http://feedads.g.doubleclick.net/~a/0HIuoB973c399G7m8l5WxJ6Q1ro/0/da"><img src="http://feedads.g.doubleclick.net/~a/0HIuoB973c399G7m8l5WxJ6Q1ro/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/0HIuoB973c399G7m8l5WxJ6Q1ro/1/da"><img src="http://feedads.g.doubleclick.net/~a/0HIuoB973c399G7m8l5WxJ6Q1ro/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/completecoding1/~4/IHNeaNOIPWo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/</feedburner:origLink></item>
		<item>
		<title>How To Define Functions in Google Go</title>
		<link>http://feedproxy.google.com/~r/completecoding1/~3/fAY9OXH2kcc/</link>
		<comments>http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 17:14:51 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Go Programming]]></category>
		<category><![CDATA[google go]]></category>

		<guid isPermaLink="false">http://kevinrodrigues.com/blog/?p=973</guid>
		<description><![CDATA[A program can be broken down into modules that each perform a specific function. These modules interact with each other to perform the entire functionality of the program. Functions are the basis of modular programming in Google Go. In Google Go, a function is declared using the keyword func as shown below, func [function name] [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		var dzone_url = "http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/";
		var dzone_title = "How To Define Functions in Google Go";
		var dzone_style = "1";
		var dzone_blurb = "";
		//-->
		</script>
		<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script></div><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin-left: 5px;">
			<a rel="nofollow" target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F08%2F02%2Fhow-to-define-functions-in-google-go%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F08%2F02%2Fhow-to-define-functions-in-google-go%2F&amp;source=rodrigueskevin&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>A program can be broken down into modules that each perform a specific function. These modules interact with each other to perform the entire functionality of the program. Functions are the basis of modular programming in Google Go.</p>
<p>In Google Go, a function is declared using the keyword <em>func</em> as shown below,</p>
<pre class="prettyprint">func [function name] ([param1 declaration], [param2 declaration],...) (ret1 declaration, ret2 declaration, ...){
     //body of the function
}</pre>
<p>The function arguments declaration (param1) and return values declaration (ret1) is similar to variable declaration except the <em>var</em> keyword is not used. (See <a title="How To Define Variables In Google Go" href="http://kevinrodrigues.com/blog/2010/07/28/how-to-define-variables-in-google-go/" target="_blank">How To Define Variables In Google Go</a>)</p>
<pre class="prettyprint">func min(x int, y int) int {
        if x < y {
             return x
        }
        return y
}</pre>
<p>In Google Go, it is necessary that the opening brace { of the scope should be placed on the same line as the function declaration else you will get a compilation error.</p>
<pre class="prettyprint">func main()
{                                          //Wrong
   fmt.Println("Hello World")
}

func main() {                         //Correct
   fmt.Println("Hello World")
}</pre>
<p>One of Go's unusual features is that functions and methods can return multiple values. Multiple return values are declared after the argument list and separated by a comma as shown below.</p>
<pre class="prettyprint">package main

import "fmt"

func calc(a int, b int) (add int, sub int){
     add = a + b
     sub = a - b
     return
}

func main(){
     a := 100
     b := 50
     add, sub := calc(a, b)
     fmt.Println("Addition = ", add);
     fmt.Println("Subtraction = ", sub);
}</pre>
<p>The return or result "parameters" of a Go function can be given names and used as regular variables, just like the incoming parameters. When named, they are initialized to the zero values for their types when the function begins; if the function executes a return statement with no arguments, the current values of the result parameters are used as the returned values.</p>
<p><strong>Defer</strong><br />
Go's defer statement schedules a function call to be run immediately before the function executing the defer returns. It's an unusual but effective way to deal with situations such as resources that must be released regardless of which path a function takes to return. The canonical examples are unlocking a mutex or closing a file.</p>
<pre class="prettyprint">// Contents returns the file's contents as a string.
func Contents(filename string) (string, os.Error) {
    f, err := os.Open(filename, os.O_RDONLY, 0)
    if err != nil {
        return "", err
    }
    defer f.Close()  // f.Close will run when we're finished.

    var result []byte
    buf := make([]byte, 100)
    for {
        n, err := f.Read(buf[0:])
        result = bytes.Add(result, buf[0:n])
        if err != nil {
            if err == os.EOF {
                break
            }
            return "", err  // f will be closed if we return here.
        }
    }
    return string(result), nil // f will be closed if we return here.
}</pre>
<p>Deferring a function like this has two advantages. First, it guarantees that you will never forget to close the file, a mistake that's easy to make if you later edit the function to add a new return path. Second, it means that the close sits near the open, which is much clearer than placing it at the end of the function.</p>
<p>The arguments to the deferred function are evaluated when the defer executes, not when the call executes. Besides avoiding worries about variables changing values as the function executes, this means that a single deferred call site can defer multiple function executions.</p>
<p>Here's a silly example.</p>
<pre class="prettyprint">for i := 0; i < 5; i++ {
    defer fmt.Printf("%d ", i)
}</pre>
<p>Deferred functions are executed in LIFO order, so this code will cause 4 3 2 1 0 to be printed when the function returns.<br />
<h3 class='related_post_title'>Related Posts:</h3>
<ul class='related_post'>
<li><a href='http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/' title='How To Use Arrays and Slices in Google Go'>How To Use Arrays and Slices in Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/' title='How To Use Strings In Google Go'>How To Use Strings In Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/' title='How To Define Constants in Google Go'>How To Define Constants in Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/28/how-to-define-variables-in-google-go/' title='How To Define Variables In Google Go'>How To Define Variables In Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/26/how-to-write-a-hello-world-program-in-google-go/' title='How To Write A Hello World Program In Google Go'>How To Write A Hello World Program In Google Go</a></li>
</ul>
<div style="clear:both;">&nbsp;</div>

<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/&amp;title=How+To+Define+Functions+in+Google+Go" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/&amp;t=How+To+Define+Functions+in+Google+Go" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-orkut">
			<a href="http://promote.orkut.com/preview?nt=orkut.com&amp;tt=How+To+Define+Functions+in+Google+Go&amp;du=http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/&amp;cn=A%20program%20can%20be%20broken%20down%20into%20modules%20that%20each%20perform%20a%20specific%20function.%20These%20modules%20interact%20with%20each%20other%20to%20perform%20the%20entire%20functionality%20of%20the%20program.%20Functions%20are%20the%20basis%20of%20modular%20programming%20in%20Google%20Go.%0D%0A%0D%0AIn%20Google%20Go%2C%20a%20function%20is%20declared%20using%20the%20keyword%20func%20as%20s" rel="nofollow" class="external" title="Promote this on Orkut">Promote this on Orkut</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/&amp;title=How+To+Define+Functions+in+Google+Go" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/&amp;title=How+To+Define+Functions+in+Google+Go" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/&amp;title=How+To+Define+Functions+in+Google+Go&amp;description=A%20program%20can%20be%20broken%20down%20into%20modules%20that%20each%20perform%20a%20specific%20function.%20These%20modules%20interact%20with%20each%20other%20to%20perform%20the%20entire%20functionality%20of%20the%20program.%20Functions%20are%20the%20basis%20of%20modular%20programming%20in%20Google%20Go.%0D%0A%0D%0AIn%20Google%20Go%2C%20a%20function%20is%20declared%20using%20the%20keyword%20func%20as%20s" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=How+To+Define+Functions+in+Google+Go+-+http://b2l.me/aeg7aj&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>


<p><a href="http://feedads.g.doubleclick.net/~a/PpIYV0IVsJ5mzO648Tp5Xmwt0vI/0/da"><img src="http://feedads.g.doubleclick.net/~a/PpIYV0IVsJ5mzO648Tp5Xmwt0vI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/PpIYV0IVsJ5mzO648Tp5Xmwt0vI/1/da"><img src="http://feedads.g.doubleclick.net/~a/PpIYV0IVsJ5mzO648Tp5Xmwt0vI/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/completecoding1/~4/fAY9OXH2kcc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/</feedburner:origLink></item>
		<item>
		<title>How To Use Strings In Google Go</title>
		<link>http://feedproxy.google.com/~r/completecoding1/~3/Ys4_fnXzjeo/</link>
		<comments>http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 09:48:45 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Go Programming]]></category>
		<category><![CDATA[google go]]></category>

		<guid isPermaLink="false">http://kevinrodrigues.com/blog/?p=967</guid>
		<description><![CDATA[In the earlier tutorials, we have seen how to define variables and how to define constants in Google Go. This tutorial explains how we can use strings in Google Go programming. Strings The predeclared string type is string. Unlike C/C++ programming, Strings are length-delimited not NUL-terminated. Strings behave like arrays of bytes but are immutable: [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		var dzone_url = "http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/";
		var dzone_title = "How To Use Strings In Google Go";
		var dzone_style = "1";
		var dzone_blurb = "";
		//-->
		</script>
		<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script></div><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin-left: 5px;">
			<a rel="nofollow" target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F07%2F31%2Fhow-to-use-strings-in-google-go%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F07%2F31%2Fhow-to-use-strings-in-google-go%2F&amp;source=rodrigueskevin&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In the earlier tutorials, we have seen <a href="http://kevinrodrigues.com/blog/2010/07/28/how-to-define-variables-in-google-go/">how to define variables</a> and <a href="http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/">how to define constants</a> in Google Go. This tutorial explains how we can use strings in Google Go programming.</p>
<p><strong>Strings</strong><br />
The predeclared string type is <em>string</em>. Unlike C/C++ programming, Strings are length-delimited not NUL-terminated. Strings behave like arrays of bytes but are immutable: once created, it is impossible to change the contents of a string. In C++ terms, Go strings are a bit like <em>const</em> strings, while pointers to strings are analogous to <em>const</em> string references.  Once you&#8217;ve built a string value, you can&#8217;t change it, although of course you can change a string variable simply by reassigning it.</p>
<p>This snippet from strings.go is legal code:</p>
<pre class="prettyprint">s := "hello"
if s[1] != 'e' { os.Exit(1) }
s = "good bye"
var p *string = &amp;s
*p = "ciao"</pre>
<p>However the following statements are illegal because they would modify a string value:</p>
<pre class="prettyprint">s[0] = 'x'
(*p)[1] = 'y'</pre>
<p>The elements of strings have type <em>byte</em> and may be accessed using the usual indexing operations. It is illegal to take the address of such an element; if s[i] is theith <em>byte</em> of a string, &amp;s[i] is invalid.</p>
<p><strong>String literals</strong><br />
A string literal represents a string constant obtained from concatenating a sequence of characters. There are two forms: raw string literals and interpreted string literals.</p>
<p>Raw string literals are character sequences between back quotes ` `. Within the quotes, any character is legal except back quote. When using back quotes, backslashes have no special meaning and the string may span multiple lines.</p>
<p>Interpreted string literals are character sequences between double quotes &#8221; &#8220;. The text between the quotes, which may not span multiple lines, forms the value of the literal, with backslash escapes interpreted as they are in character literals.</p>
<p>Below are some examples of both types of string literals. Google Go also supports Unicode strings.</p>
<pre class="prettyprint">`abc`  // same as "abc"
`\n
\n`    // same as "\\n\n\\n"
"\n"
""
"Hello, world!\n"
"日本語"                                 // UTF-8 input text
`日本語`                                 // UTF-8 input text as a raw literal</pre>
<p>Strings can be concatenated using the &#8216;+&#8217; operator. The length of string s can be discovered using the built-in function len(). The length is a compile-time constant if s is a string literal.</p>
<pre class="prettyprint">var string1 = "Hello World "
var string2 = "This is Google Go"
string3 := string1 + string2	//string3 = "Hello World This is Google Go"
len_string3 := len(string3)	//len_string3 = 29</pre>
<p>Google Go provides a “strings” package that consists of several functions to manipulate strings. You can find more information at <a rel="nofollow" target="_blank" href="http://golang.org/pkg/strings/">http://golang.org/pkg/strings/</a>.<br />
<h3 class='related_post_title'>Related Posts:</h3>
<ul class='related_post'>
<li><a href='http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/' title='How To Use Arrays and Slices in Google Go'>How To Use Arrays and Slices in Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/' title='How To Define Functions in Google Go'>How To Define Functions in Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/' title='How To Define Constants in Google Go'>How To Define Constants in Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/28/how-to-define-variables-in-google-go/' title='How To Define Variables In Google Go'>How To Define Variables In Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/26/how-to-write-a-hello-world-program-in-google-go/' title='How To Write A Hello World Program In Google Go'>How To Write A Hello World Program In Google Go</a></li>
</ul>
<div style="clear:both;">&nbsp;</div>

<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/&amp;title=How+To+Use+Strings+In+Google+Go" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/&amp;t=How+To+Use+Strings+In+Google+Go" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-orkut">
			<a href="http://promote.orkut.com/preview?nt=orkut.com&amp;tt=How+To+Use+Strings+In+Google+Go&amp;du=http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/&amp;cn=In%20the%20earlier%20tutorials%2C%20we%20have%20seen%20how%20to%20define%20variables%20and%20how%20to%20define%20constants%20in%20Google%20Go.%20This%20tutorial%20explains%20how%20we%20can%20use%20strings%20in%20Google%20Go%20programming.%0D%0A%0D%0AStrings%0D%0AThe%20predeclared%20string%20type%20is%20string.%20Unlike%20C%2FC%2B%2B%20programming%2C%20Strings%20are%20length-delimited%20not%20NUL-terminate" rel="nofollow" class="external" title="Promote this on Orkut">Promote this on Orkut</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/&amp;title=How+To+Use+Strings+In+Google+Go" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/&amp;title=How+To+Use+Strings+In+Google+Go" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/&amp;title=How+To+Use+Strings+In+Google+Go&amp;description=In%20the%20earlier%20tutorials%2C%20we%20have%20seen%20how%20to%20define%20variables%20and%20how%20to%20define%20constants%20in%20Google%20Go.%20This%20tutorial%20explains%20how%20we%20can%20use%20strings%20in%20Google%20Go%20programming.%0D%0A%0D%0AStrings%0D%0AThe%20predeclared%20string%20type%20is%20string.%20Unlike%20C%2FC%2B%2B%20programming%2C%20Strings%20are%20length-delimited%20not%20NUL-terminate" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=How+To+Use+Strings+In+Google+Go+-+http://b2l.me/adx7bg&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>


<p><a href="http://feedads.g.doubleclick.net/~a/RwcU6A1BMKYUKxybuIATVYS_CTQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/RwcU6A1BMKYUKxybuIATVYS_CTQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/RwcU6A1BMKYUKxybuIATVYS_CTQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/RwcU6A1BMKYUKxybuIATVYS_CTQ/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/completecoding1/~4/Ys4_fnXzjeo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://kevinrodrigues.com/blog/2010/07/31/how-to-use-strings-in-google-go/</feedburner:origLink></item>
		<item>
		<title>How To Define Constants in Google Go</title>
		<link>http://feedproxy.google.com/~r/completecoding1/~3/7NoZJqpDqgk/</link>
		<comments>http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 17:05:35 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[Go Programming]]></category>
		<category><![CDATA[google go]]></category>

		<guid isPermaLink="false">http://kevinrodrigues.com/blog/?p=958</guid>
		<description><![CDATA[In the previous tutorial, we saw how to define variables in Google Go. In today&#8217;s tutorial, we check out how we can declare constants and enumerated constants in Google Go. Constants The types of constants available in Go programming are boolean constants, integer constants, floating-point constants, complex constants, and string constants. Constants in Go are [...]]]></description>
			<content:encoded><![CDATA[<!--S-ButtonZ 1.1.5 Start--><div style="float: right; width: 42px; padding-right: 10px; margin: 0 0 0 10px;">
		<script type="text/javascript">
		<!--
		var dzone_url = "http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/";
		var dzone_title = "How To Define Constants in Google Go";
		var dzone_style = "1";
		var dzone_blurb = "";
		//-->
		</script>
		<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script></div><!--S-ButtonZ 1.1.5 End--><div class="tweetmeme_button" style="float: right; margin-left: 5px;">
			<a rel="nofollow" target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F07%2F29%2Fhow-to-define-constants-in-google-go%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fkevinrodrigues.com%2Fblog%2F2010%2F07%2F29%2Fhow-to-define-constants-in-google-go%2F&amp;source=rodrigueskevin&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In the previous tutorial, we saw <a href="http://kevinrodrigues.com/blog/2010/07/28/how-to-define-variables-in-google-go/">how to define variables in Google Go</a>. In today&#8217;s tutorial, we check out how we can declare constants and enumerated constants in Google Go.</p>
<p><strong>Constants</strong><br />
The types of constants available in Go programming are boolean constants, integer constants, floating-point constants, complex constants, and string constants. Constants in Go are created at compile time, even when defined as locals in functions.</p>
<p>Constants are declared similar to variables except that the <em>const</em> keyword is used. Also we cannot use the idiom (using :=) as done for variable declaration.</p>
<p>Below are examples of declaring different types of constants in Go programming.</p>
<pre class="prettyprint">const Pi float64 = 3.14159265358979323846   //typed floating-point constant
const zero = 0.0             // untyped floating-point constant
const (
        size int64 = 1024   //typed integer constant
        eof = -1             // untyped integer constant
)
const a, b, c = 3, 4, "foo"  // a = 3, b = 4, c = "foo", untyped integer and string constants
const u, v float = 0, 3      // u = 0.0, v = 3.0
const sum = 1 – 0.707i   ///complex constant
const flag bool = true</pre>
<p><strong>Enumerated Constants</strong><br />
In Go, enumerated constants are created using the<em> iota</em> enumerator.  This can be considered similar to enumeration in C. It is reset to 0 whenever the reserved word <em>const</em> appears in the source and increments after each use of <em>iota</em> as shown below.</p>
<pre class="prettyprint">const (  // iota is reset to 0
        c0 = iota  // c0 == 0
        c1 = iota  // c1 == 1
        c2 = iota  // c2 == 2
)

const (
        a = 1 << iota  // a == 1 (iota has been reset)
        b = 1 << iota  // b == 2
        c = 1 << iota  // c == 4
)

const (
        u       = iota * 42  // u == 0     (untyped integer constant)
        v float = iota * 42  // v == 42.0  (float constant)
        w       = iota * 42  // w == 84    (untyped integer constant)
)

const x = iota  // x == 0 (iota has been reset)
const y = iota  // y == 0 (iota has been reset)</pre>
<h3 class='related_post_title'>Related Posts:</h3>
<ul class='related_post'>
<li><a href='http://kevinrodrigues.com/blog/2010/08/05/how-to-use-arrays-and-slices-in-google-go/' title='How To Use Arrays and Slices in Google Go'>How To Use Arrays and Slices in Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/08/02/how-to-define-functions-in-google-go/' title='How To Define Functions in Google Go'>How To Define Functions in Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/28/how-to-define-variables-in-google-go/' title='How To Define Variables In Google Go'>How To Define Variables In Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/26/how-to-write-a-hello-world-program-in-google-go/' title='How To Write A Hello World Program In Google Go'>How To Write A Hello World Program In Google Go</a></li>
<li><a href='http://kevinrodrigues.com/blog/2010/07/24/how-to-install-googles-go-programming-language/' title='How To Install Google&#8217;s Go Programming Language'>How To Install Google&#8217;s Go Programming Language</a></li>
</ul>
<div style="clear:both;">&nbsp;</div>

<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/&amp;title=How+To+Define+Constants+in+Google+Go" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/&amp;t=How+To+Define+Constants+in+Google+Go" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-orkut">
			<a href="http://promote.orkut.com/preview?nt=orkut.com&amp;tt=How+To+Define+Constants+in+Google+Go&amp;du=http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/&amp;cn=In%20the%20previous%20tutorial%2C%20we%20saw%20how%20to%20define%20variables%20in%20Google%20Go.%20In%20today%27s%20tutorial%2C%20we%20check%20out%20how%20we%20can%20declare%20constants%20and%20enumerated%20constants%20in%20Google%20Go.%0D%0A%0D%0AConstants%0D%0AThe%20types%20of%20constants%20available%20in%20Go%20programming%20are%20boolean%20constants%2C%20integer%20constants%2C%20floating-point%20const" rel="nofollow" class="external" title="Promote this on Orkut">Promote this on Orkut</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/&amp;title=How+To+Define+Constants+in+Google+Go" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/&amp;title=How+To+Define+Constants+in+Google+Go" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.dzone.com/links/add.html?url=http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/&amp;title=How+To+Define+Constants+in+Google+Go&amp;description=In%20the%20previous%20tutorial%2C%20we%20saw%20how%20to%20define%20variables%20in%20Google%20Go.%20In%20today%27s%20tutorial%2C%20we%20check%20out%20how%20we%20can%20declare%20constants%20and%20enumerated%20constants%20in%20Google%20Go.%0D%0A%0D%0AConstants%0D%0AThe%20types%20of%20constants%20available%20in%20Go%20programming%20are%20boolean%20constants%2C%20integer%20constants%2C%20floating-point%20const" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=How+To+Define+Constants+in+Google+Go+-+http://b2l.me/adh57j&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>


<p><a href="http://feedads.g.doubleclick.net/~a/4GjqAOFAfHwhMMqK2cOSSDxdfoA/0/da"><img src="http://feedads.g.doubleclick.net/~a/4GjqAOFAfHwhMMqK2cOSSDxdfoA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/4GjqAOFAfHwhMMqK2cOSSDxdfoA/1/da"><img src="http://feedads.g.doubleclick.net/~a/4GjqAOFAfHwhMMqK2cOSSDxdfoA/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/completecoding1/~4/7NoZJqpDqgk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://kevinrodrigues.com/blog/2010/07/29/how-to-define-constants-in-google-go/</feedburner:origLink></item>
	</channel>
</rss>
