﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Go Program - The First internet site for Go programming &#187; Declarations</title>
	<atom:link href="http://www.go-program.com/category/basics/declarations/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.go-program.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 19 Nov 2009 11:31:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GO Programming Language Declarations</title>
		<link>http://www.go-program.com/go-language-declarations/</link>
		<comments>http://www.go-program.com/go-language-declarations/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 16:34:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Declarations]]></category>

		<guid isPermaLink="false">http://www.go-program.com/?p=33</guid>
		<description><![CDATA[



Declarations
Declarations are introduced by a keyword
var
const
type
func
and look reversed compared to C:
var i int
const PI = 22./7.
type Point struct { x, y int }
func sum(a, b int) int { return a + b }
Why are they reversed? Earlier example:
var p, q *int
Also functions read better and are consistent with other declarations.
Var
Variable declarations are introduced by var.
They [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-9004887195759386";
/* Go programs text ads1 */
google_ad_slot = "3650915252";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Declarations<br />
Declarations are introduced by a keyword<br />
var<br />
const<br />
type<br />
func</p>
<p>and look reversed compared to C:</p>
<p>var i int<br />
const PI = 22./7.<br />
type Point struct { x, y int }<br />
func sum(a, b int) int { return a + b }</p>
<p>Why are they reversed? Earlier example:<br />
var p, q *int<br />
Also functions read better and are consistent with other declarations.</p>
<p>Var</p>
<p>Variable declarations are introduced by var.<br />
They may have a type or an initialization expression; one or both must be present. Initializers must match variables (and types!).</p>
<p>var i int<br />
var j = 365.245<br />
var k int = 0<br />
var l, m uint64 = 1, 2<br />
var billion int64 = 1e9 // float constant!<br />
var inter, floater, stringer = 1, 2.0, &#8220;hi&#8221;</p>
<p>Distributing var<br />
<script type="text/javascript"><!--
google_ad_client = "pub-9004887195759386";
/* Goprograms1 */
google_ad_slot = "8587802801";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
Annoying to type var all the time. Group with parens! Need semicolons as separators.</p>
<p>var (<br />
i int;<br />
j = 356.245;<br />
k int = 0;<br />
l, m uint64 = 1, 2;<br />
billion int64 = 1e9;<br />
inter, floater, stringer = 1, 2.0, &#8220;hi&#8221;<br />
)</p>
<p>Applies to const, type, var</p>
<p>The := &#8220;short declaration&#8221;</p>
<p>Within functions (only), declarations of the form</p>
<p>var v = value<br />
can be shortened to<br />
v := value</p>
<p>(Another reason for the name/type reversal.)<br />
The type is that of the value (for ideal numbers, get<br />
int or float, accordingly.)</p>
<p>a, b, c, d := 1, 2.0, &#8220;three&#8221;, FOUR</p>
<p>These are used a lot and are available in places such as for loop initializers.</p>
<p>Const</p>
<p>Constant declarations are introduced by const.<br />
They must have a &#8220;constant expression&#8221;, evaluated at compile time, as initializer and may have an optional type specifier.</p>
<p>const Pi = 22./7.<br />
const AccuratePi float64 = 355./113<br />
const beef, two, parsnip = &#8220;meat&#8221;, 2, &#8220;veg&#8221;<br />
const (<br />
Monday, Tuesday, Wednesday = 1, 2, 3;<br />
Thursday, Friday, Saturday = 4, 5, 6;<br />
)</p>
<p>Iota</p>
<p>Constant declarations can use the counter iota, which starts at 0 in each const block and increments at each semicolon.</p>
<p>const (<br />
Monday = iota; // 0<br />
Tuesday = iota; // 1<br />
)</p>
<p>Shorthand: Previous type and expressions repeat.<br />
const (<br />
loc0, bit0 uint32 = iota, 1&amp;lt;<br />
loc1, bit1; // 1, 2<br />
loc2, bit2; // 2, 4<br />
)</p>
<p>Type</p>
<p>Type declarations are introduced by type.<br />
We&#8217;ll learn more about types later but here are some examples:</p>
<p>type Point struct {<br />
x, y, z float;<br />
name string<br />
}</p>
<p>type Operator func(a, b int) int<br />
type ArrayOfIntPointers []*int<br />
We&#8217;ll come back to functions a little later.</p>
<p>New</p>
<p>The new() operator allocates memory. Syntax is like a function call, with type as argument, similar to C++. Returns a pointer to the allocated object.</p>
<p>var p *Point = new(Point)<br />
v := new(int) // v has type *int</p>
<p>Later we&#8217;ll see how to build arrays and such.<br />
There is no delete or free; Go has garbage collection.</p>
<p>Assignment<br />
Assignment is easy and familiar:<br />
a = b<br />
But multiple assignment works too:<br />
x, y, z = f1(), f2(), f3()<br />
a, b = b, a</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.go-program.com/go-language-declarations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
