# The var keyword......various questions

**URL:** <https://forum.golangbridge.org/t/the-var-keyword-various-questions/13588>\
**Category:** Getting Help\
**Created:** [May 3, 2019, 11:32am UTC](https://forum.golangbridge.org/t/the-var-keyword-various-questions/13588 "2019-05-03T11:32:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cherilexvold1974](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/cherilexvold1974/32/4270_2.png) [@cherilexvold1974](https://forum.golangbridge.org/u/cherilexvold1974)\
**Post date:** [May 3, 2019, 11:32am UTC](https://forum.golangbridge.org/t/the-var-keyword-various-questions/13588/1 "2019-05-03T11:32:38Z")

</div>

First of all, the teacher noted that var is a keyword. I’m happy because I finally found long sought for and previously unsuccessful search for information for this keyword under “Variable Declarations” in the Language Specification. This word has been a mystery to me.

VarDecl = “var” ( VarSpec | “(” { VarSpec “;” } “)” ) .  
VarSpec = IdentifierList ( Type [“=” ExpressionList] | “=” ExpressionList ) .

var i int

Previously, I thought that when the Language Specification wrote things like “(”, “;”, “)” that those things were required to be part of the expression. But I see they are not in “var i int”. So what is the purpose of “(”, “;”, “)”

var i int  
var U, V, W float64  
var k = 0  
var x, y float32 = -1, -2  
var (  
i int  
u, v, s = 2.0, 3.0, “bar”  
)  
var re, im = complexSqrt(-1)  
var \_, found = entries[name] // map lookup; only interested in “found”

Are each of these possible uses of the var keyword? Are they all of the possible uses?

Otherwise, each variable is initialized to its [zero value](https://golang.org/ref/spec#The_zero_value).

What is the purpose of the zero value? What is the purpose of including it?

Interesting. The teacher just said something in his video. “If you ever need to declare a variable outside of a function body, you use var.” This is very helpful. Why did I not hear that before? Just thinking “out loud” here. Any comments?

Interesting how learning is, at least for me. The first time I hear something about programming, it sounds like complicated gibberish and eludes me. But with time and struggle and patience, understanding comes.

Also, when I’m listening to a video, I often have to either pause the video and let the information sink in, or I have to go back and repeat it. I think I would be lost in a classroom setting.

The teacher quickly went over information in the Language Specification for "The zero value "  
I have a vague understanding of the information provide here, but I still do not understand the purpose of including the zero value.  
He just quickly mentioned that the var keyword can be used any place within the package.  
In [https://play.golang.org/p/jW-10tKtrrr](https://play.golang.org/p/jW-10tKtrrr) what would be referred to as “the package”. The whole thing? Or the whole “program” ?  
I’ve gone over my allotted time.

---

<div class="post-metadata">

**Author:** ![cherilexvold1974](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/cherilexvold1974/32/4270_2.png) [@cherilexvold1974](https://forum.golangbridge.org/u/cherilexvold1974)\
**Post date:** [May 3, 2019, 11:34am UTC](https://forum.golangbridge.org/t/the-var-keyword-various-questions/13588/2 "2019-05-03T11:34:43Z")

</div>

Sorry for my incorrect use of ```. Don’t know how that happened.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [May 3, 2019, 11:40am UTC](https://forum.golangbridge.org/t/the-var-keyword-various-questions/13588/3 "2019-05-03T11:40:59Z")

</div>

> [@cherilexvold1974](#):
>
> ```auto
> VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
> 
> ```

This means:

A `VarDecl` has to start with the literal token `var`. It has to be followed by **either** a single `VarSpec` **or** a semicolon-separated list of `VarSpec`s which has to be enclosed in paranthesis.

So yes, you are right. `"("` and `")"` require you to write literal parenthesis, but there is also the pipe (`|`), which denotes alternatives (in grammars its sometimes called _or_ or _choice_)

---

<div class="post-metadata">

**Author:** ![cherilexvold1974](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/cherilexvold1974/32/4270_2.png) [@cherilexvold1974](https://forum.golangbridge.org/u/cherilexvold1974)\
**Post date:** [May 5, 2019, 11:12am UTC](https://forum.golangbridge.org/t/the-var-keyword-various-questions/13588/4 "2019-05-05T11:12:09Z")

</div>

Thanks!!!

var i int  
var U, V, W float64  
var k = 0  
var x, y float32 = -1, -2  
var (  
i int  
u, v, s = 2.0, 3.0, “bar”  
)  
var re, im = complexSqrt(-1)  
var \_, found = entries[name] // map lookup; only interested in “found”

Are these examples of how var can be used? Or are they the only ways which var can be used?

What is the purpose of the zero value? What is the purpose of including it, if it just means 0

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [August 3, 2019, 11:17am UTC](https://forum.golangbridge.org/t/the-var-keyword-various-questions/13588/5 "2019-08-03T11:17:39Z")

</div>

Closed on August 3, 2019, 11:17am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
