The var keyword......various questions

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.

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 what would be referred to as “the package”. The whole thing? Or the whole “program” ?
I’ve gone over my allotted time.

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

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 VarSpecs 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)

2 Likes

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

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