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

https://twitter.com/egonelbre

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!

https://twitter.com/egonelbre

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

https://twitter.com/egonelbre

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!

https://twitter.com/egonelbre

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

https://twitter.com/egonelbre

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!

https://twitter.com/egonelbre

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

https://twitter.com/egonelbre

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?

https://twitter.com/egonelbre

Coding Time!

https://twitter.com/egonelbre

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
}

Source File πŸ“„

The Source File

Test File πŸ“

The Test File