A Variable is something that can inherently change π§ β‘οΈ π§ To be variable means to be adaptable or changeable π© π π¨ You may think of it as varied or versatile; saying π¨οΈ I’m adaptable is the same as sayingπ¨οΈ I’m variable
We can take the types we learned about in the previous lesson and put them into something we name and can change the value of. A variable! So in programming, a variable is a type value with a name that can change it’s value.
Setup
Let’s make our directory variable
and the files we want inside of
that directory example_test.go
variable.go
mkdir variable
touch variable/example_test.go variable/variable.go
Now let’s open up variable.go
and for the very first line we’ll add
package variable
Next for example_test.go
for the very first line we’ll add
package variable_test
We can import basics/variable
into cmd/main.go
and run functions
from there with go run cmd/main.go
and also to run our example_test.go
π
we use go test variable/example_test.go
in the commandline.
Explicit Declaration and Zero Values
In Go, everything has a type
and the compiler (the thing that
makes your program into something your computer can use) needs to know what
type
something is. This is so it can make optimizations, help us out
when we make errors, and other things not important right now.
When we create a variable with the var
keyword we need to give it a name and
a type
. When we do this it can be said that the variable has been declared
(now exists) and has been initialized (has a value). So if you don’t
assign a value for a type, Go will automatically assign it the zero value for
that type (we can think of it as the default value). What are those zero
values? Glad you asked! π
Coding Time!
variable.go
// Zero shows off all of the zero values or what might also be
// thought of as Zero values for each type when they are not assigned
// anything.
func Zero() {
var name string
var x int
var f float32
var isHard bool
var r rune
var slice []int
var map_ map[int]int
fmt.Printf(`
string zero value is: %q
int zero value is: %d
float32 zero value is: %f
bool zero value is: %t
rune zero value is: %d or %q
slice zero value is: %+v
map zero value is: %+v`,
name, x, f, isHard, r, string(r), slice, map_)
}
example_test.go
func ExampleZero() {
variable.Zero()
// Output:
// string zero value is: ""
// int zero value is: 0
// float32 zero value is: 0.000000
// bool zero value is: false
// rune zero value is: 0 or "\x00"
// slice zero value is: []
// map zero value is: map[]
}
Initialization
Initialization is a part of developer jargon. It means to make something. Let’s look at an example, if you here
You need to initialize that before you use it.
You’re being told that the value, or lack their of, won’t work and you need to
turn it into something that will work. This happens with our above code and the
new
keyword that returns a pointer (coming soon). So we should give our
variables values that we want, in other words, we should initialize them.
Coding Time!
variable.go
// Explicit shows that we can explicitly type a variable to a specific
// type if we wanted to.
func Explicit() {
// It works for all types: string, bool, int, float, rune...
var name string = "Jay"
// Emojis are unicode characters, they are supported in Go through runes
var emoji rune = 'π'
// You can do multiple declarations
var min, max int = 0, 1000
var isTrue bool = true
fmt.Printf("My Name's %s! %s,\nFrom %d to %d\nGiven bool value: %t",
name, string(emoji),
min, max,
isTrue)
}
example_test.go
func ExampleExplicit() {
variable.Explicit()
// Output:
// My Name's Jay! π,
// From 0 to 1000
// Given bool value: true
}
Implicit Declaration
Go’s compiler is pretty smart and it knows what type
your variable is
depending on what the right hand side says. We can use this to our advantage!
We don’t have to type out every single type
, because as you get more
experience you’ll see it’s obvious what type
something is.
Coding Time!
variable.go
// Implicit shows that we don't need to declare what the type is as
// long as we initialize it on the right.
func Implicit() {
// It works for all types: string, bool, int, float, rune...
var question = "Does anyone have any room for"
// Emojis are unicode characters, they are supported in Go through runes
var emoji = 'π₯§'
var pi = 3.14159
var isTrue = false
fmt.Printf("%s %s,\nMore Pi %f\nGiven bool value: %t",
question, string(emoji),
pi,
isTrue)
}
example_test.go
func ExampleImplicit() {
variable.Implicit()
// Output:
// Does anyone have any room for π₯§,
// More Pi 3.141590
// Given bool value: false
//
}
Assignment Operator
There is an even faster, easier, and more idomatic way of declaring a variable.
:=
is the assignment operator, which I call the gopher operator because
if this π :) is a smiley face then this π := is two beady eyes and
some buck teeth. Remind you of anything cute, blue, and furry?
Coding Time!
variable.go
// AssignmentOperator shows the preferred and idomatic way of declaring
// variables in Go. With the `:=` operator
func AssignmentOperator() {
// It works for all types: string, bool, int, float, rune...
statement := "Short and Sweet. Very Nice!"
emoji := 'π'
mySpecialNumber := 12648430
isQuick := true
fmt.Printf("%s %s\nI really need %x\nStill works? %t",
statement, string(emoji), mySpecialNumber, isQuick)
}
example_test.go
func ExampleAssignmentOperator() {
variable.AssignmentOperator()
// Output:
// Short and Sweet. Very Nice! π
// I really need c0ffee
// Still works? true
}