So far we’ve been declaring variables one by one, each on their own separate line. But Go actually allows us to declare multiple variables on a single line, in fact, there’s a few different syntaxes!
Let’s start with declaring without assigning a value:
var part1, part2 string part1 = "To be..." part2 = "Not to be..."
Above, we declared both part1
and part2
on the same line both with the same type. If we’re using this syntax, both variables must be the same type.
If we already know what values we want to assign our variables we can use :=
like so:
quote, fact := "Bears, Beets, Battlestar Galactica", true fmt.Println(quote) // Prints: Bears, Beets, Battlestar Galactica fmt.Println(fact) // Prints: true
In the example above, we declare both quote
and fact
in the same line with one operator (:=
). These variables are then assigned their respective values based on the ordering of variables and value. Since quote
is the first variable, and the string "Bears, Beets, Battlestar Galactica"
is the first value, quote
has a value of "Bears, Beets, Battlestar Galactica"
. Similarly, fact
then is assigned the value true
.
Instructions
On a single line, declare two int32
variables: one named magicNum
and the other powerLevel
.
Assign magicNum
a value of 2048
and powerLevel
a value of 9001
.
Once the values are assigned uncomment the first print statement.
In a single line, declare and initialize two variables:
amount
with a value of10
.unit
with a value of"doll hairs"
.
Then, uncomment the second print statement.