Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Variables in Go must be declared before you can use them. Don't worry, there are several convenient ways to do so.
- Must be declared
- Must declare a type for variable
- Can declare multiple variables of same type at once
- Type can be inferred from initial value, if you provide one
- Short variable declarations:
e := 6
package main
import "fmt"
func main() {
var a int
a = 1
var b, c int
b = 2
c = 3
var d = 5
e := 6
fmt.Println()
}
If we run this sample, we'll get a compile error for all of these variables saying that they have been "declared and not used". Just like you're required to use every package you import, you also have to use every variable you declare. An unused variable often indicates a bug, and Go helps your development team spot these bugs quicker by reporting unused variables.
Since this is just a quick example, the way we'll "use" the variables is by updating the last line to print them all out:
fmt.Println(a, b, c, d, e)
Now that we're actually accessing the variable values somewhere, the program will compile and run successfully.
You can assign multiple variables at the same time:
package main
import "fmt"
func main() {
var a, b int
a, b = 1, 2
var c, d = 3, 4
e, f := 5, 6
fmt.Println(a, b, c, d, e, f)
}
- The same rules apply for the names of variables, constants, functions, and custom types.
- Must begin with a letter
- Can be followed by any number of letters and digits
- If the first letter is lower-case, the variable is unexported and can only be used within the current package. (The same is true for constants, functions, etc.)
- If the first letter is upper-case, the variable is exported and can also be used OUTSIDE the current package.
- Those are the only rules enforced by the compiler. But there are a couple conventions that Go programmers follow as well.
package main
func main() {
// OK
word := 1
multipleWords := 2
// Not OK
multiplewords
multiple_words
}
You can only assign values of the declared type to a variable:
var wholeNumber int = 1
var fractionalNumber float64 = 2.75
var wholeNumber2 int = fractionalNumber // ERROR
var fractionalNumber2 float64 = wholeNumber // ERROR
fmt.Println("wholeNumber2:", wholeNumber2)
fmt.Println("fractionalNubmer2:", fractionalNumber2)
Try to run the above, and you'll get compile errors. But you can assign the values if you do a conversion: write the type you want to convert to, followed by the value you want to convert in parentheses.
var wholeNumber int = 1
var fractionalNumber float64 = 2.75
var wholeNumber2 int = int(fractionalNumber)
var fractionalNumber2 float64 = float64(wholeNumber)
fmt.Println("wholeNumber2:", wholeNumber2)
fmt.Println("fractionalNubmer2:", fractionalNumber2)
Must also convert both operands to the same type prior to math operations or comparisons:
var wholeNumber int = 1
var fractionalNumber float64 = 2.75
fmt.Println(float64(wholeNumber) + fractionalNumber)
fmt.Println(float64(wholeNumber) < fractionalNumber)
- A variable's scope is limited to block in which it's defined
- Blocks are a chunk of code surrounded by
{}
braces - They're usually associated with functions,
if
statements, and loops, all of which we'll see later. But it's possible to use blocks by themselves. - Even though it's not surrounded by curly braces, each package is an implicit block. Variables defined at package level are accessible from every block nested within that package. Most importantly, that includes the bodies of functions.
- Blocks are a chunk of code surrounded by
package main
import (
"fmt"
)
var a = "a" // Package-level variable
func main() {
var b = "b"
{
var c = "c"
{
var d = "d"
fmt.Println(a, b, c, d)
}
fmt.Println(a, b, c, d) // ERROR: "d" undefined
}
fmt.Println(a, b, c, d) // ERROR: "c", "d" undefined
}
-
0:00
And go, as in most programming languages.
-
0:03
A variable's a name that refers to a value.
-
0:06
In go, you need to declare a variables before you can use them.
-
0:09
So here's a simple variable declaration here, and
-
0:12
then we assign to it down here on this slide.
-
0:15
Each variable you declare needs to have a type.
-
0:17
So here we say that this variable a will have a type of int, and
-
0:21
then we can only assign integers to that variable.
-
0:24
You can declare multiple variables of the same type at once.
-
0:27
So here, we declare integer variables named b and c, and
-
0:30
here we assign values to both of those variables.
-
0:34
You don't have to declare the type of a variable if it can be inferred
-
0:37
from its initial assignment, however.
-
0:39
So if you assign an integer value 5 to the variable d here on the line
-
0:44
where you declare it, you don't need to declare the type of the d variable.
-
0:48
It will be just inferred that since this is an integer value,
-
0:51
the type odd is going to be int.
-
0:53
Go also offers short variable decorations, and
-
0:56
this will be the most common form of variable decoration you will use.
-
1:01
You use this syntax right here, a : followed by an = sign,
-
1:04
with the initial value that you want to assign to the variable.
-
1:08
That's the same as saying var e int = 6.
-
1:12
Now if we run this sample, We'll get a compile error for
-
1:17
all these samples saying, they've been declared and not used.
-
1:22
Just like your required to use every package you importing go,
-
1:25
you also have to use every variable you declare.
-
1:28
An unused variable often indicates a bug, and go helps your development team spot
-
1:32
these bugs quicker, by reporting unused variables.
-
1:36
Since this is just a quick example, the way we'll use the variables is by updating
-
1:40
the last line to print them all out.
-
1:41
So we'll say format.print line a,
-
1:47
b, c, d, and e.
-
1:49
Save that and rerun it.
-
1:52
And now that we're actually accessing all the variable names somewhere,
-
1:55
the program will compile and run successfully.
-
1:58
It's also possible to assign a multiple variables at the same time using commas.
-
2:02
So here on this line, we declare a and b variables of type int, and here,
-
2:07
we assign values to both a and b at the same time.
-
2:09
1, 2 gets assigned to a, b.
-
2:14
That is a becomes 1, and b becomes 2.
-
2:17
Here, we assign initial values to the variable c and
-
2:20
d right here on the declaration line.
-
2:23
And here, we use a short variable declaration to assign initial values to
-
2:27
the variables e and f, while simultaneously declaring them.
-
2:30
And if we run this program, you see that it prints values for
-
2:33
all the variables out down here.
-
2:34
The same rules apply for the names of variables, constants, functions,
-
2:39
and custom types.
-
2:41
A variable name has to begin with a letter.
-
2:43
So, if it begins with a number as it does down here, that's not valid.
-
2:47
A variable name can be followed by any number of letters or numbers, however.
-
2:51
So as long as a variable name begins with a letter, it can be followed by numbers.
-
2:56
If the first letter is lowercase, the variable is unexported, and
-
3:00
can only be used within the current package.
-
3:02
The same is true for constants, functions, etc.
-
3:06
If the first letter is uppercase, however, the variable is exported, and
-
3:09
can be used outside the current package.
-
3:12
You can only assign values of the declared type to a variable.
-
3:16
So let's say that we have a variable named wholeNumber declared as an int,
-
3:20
and a second variable named fractionalNumber declared as a float64.
-
3:25
If we try to declare a new variable of an int type named wholeNumber2,
-
3:30
and try to assign the fractionalNumber2 to it, or
-
3:33
try to declare a new float64 variable and try to assign the wholeNumber to it,
-
3:39
we'll get compiler errors, saying that we cannot use a type
-
3:44
float64 value as an int value for assignment to a variable.
-
3:48
And we can't use an int type for assignment to a float64 variable.
-
3:53
We can assign the values if we do a conversion, however.
-
3:56
So, if we were to type int and
-
4:00
put fractional number in parentheses here, that would convert the fractional
-
4:05
number value to an int before attempting to assign it to the int variable.
-
4:10
Likewise down here, we can put the type float64 and
-
4:15
in parenthesis put wholeNumber.
-
4:18
That will convert a whole number to a float value before trying to assign
-
4:22
it to a float64 variable.
-
4:25
Let's save that, and try running this again.
-
4:28
And this time, it's successful.
-
4:30
So anytime you need to do a conversion, you write the type that you want to
-
4:34
convert to, followed by the value that you want to convert in parentheses.
-
4:38
You also need to convert both operands to the same type prior to do again
-
4:42
a math operations or comparisons.
-
4:44
So up here, we have an int and a float64 variable.
-
4:48
And we try to add one to the other,
-
4:50
as well as do a less than comparison between one and the other.
-
4:54
And we can see that we get compile errors down here.
-
4:56
If we try to add the fractionalNumber to the wholeNumber,
-
5:00
we get mismatched types int and float64.
-
5:04
And we get the same error, if we try to do a comparison between those two values.
-
5:09
Again, we can get this working by doing a conversion first.
-
5:11
So let's convert both of our int values to float64s.
-
5:16
Float64 wholeNumber and we'll do a conversion here as well,
-
5:22
float64, wholeNumber.
-
5:24
Save that, rerun it, and this time, it works.
-
5:29
A variable scope that is the section of your programming which it's visible,
-
5:33
is limited to the block in which it's defined.
-
5:36
Blocks are a chunk of codes surrounded by curly braces.
-
5:40
They're usually associated with functions, if statements, and
-
5:43
loops, all of which we'll see later.
-
5:45
But it's possible to use blocks all by themselves.
-
5:48
Even though it's not surrounded by curly braces, each package is an implicit block.
-
5:53
Variables defined at package level are accessible from every block
-
5:56
nested within that package.
-
5:58
Most importantly, that includes the bodies of functions.
-
6:01
So if we would define a variable named a appear at package level,
-
6:05
that will be visible everywhere here within mean and
-
6:09
within any other function that we declare within the main package.
-
6:14
Now this b variable, however, is defined within the main function,
-
6:19
which you'll notice, its body is surrounded by a block.
-
6:23
So that b variable is only visible here within the main function, and
-
6:27
within any other blocks that are nested within that main function.
-
6:32
Same thing with this block here.
-
6:33
We've defined the c variable inside it, so
-
6:36
that's only visible within the scope of this block.
-
6:40
And this d variable is visible only within this innermost block.
-
6:44
So you can see that we make a reference to the values or
-
6:49
to the variables a, b, c, and d, here on several lines.
-
6:52
However, the only one that doesn't have any issues is line 13 here.
-
6:57
That's because the c, b, and a variables were all declared before this block,
-
7:02
and are therefore still visible within.
-
7:06
However the d variable was declared inside this block, and
-
7:09
therefore, its scope lasts only as long as this block is still running.
-
7:14
So when we try to reference the d variable down here on this line,
-
7:17
we get an error at compile time saying that the d variable is undefined.
-
7:22
Same thing with line 17, we get an error saying that both the c and
-
7:26
the d variables are undefined down there.
-
7:28
Because the d variable was defined up here in this block, and is therefore,
-
7:33
its scope only last for that block and the c variable was defined in this block,
-
7:38
which is also out of scope by the time we get down to this line.
-
7:42
So, there is a quick overview of declaring and using variables and go.
-
7:46
Don't worry if you don't remember all those details,
-
7:48
we'll be recapping as needed in later videos.
You need to sign up for Treehouse in order to download course files.
Sign up