0
0
Goprogramming~15 mins

Why variables are needed in Go - See It in Action

Choose your learning style9 modes available
Why variables are needed
๐Ÿ“– Scenario: Imagine you are baking cookies and need to keep track of how many cookies you have baked so far. You want to remember this number so you can tell your friend later.
๐ŸŽฏ Goal: You will create a simple Go program that uses a variable to store the number of cookies baked and then prints that number. This will show why variables are useful to remember information in a program.
๐Ÿ“‹ What You'll Learn
Create a variable to store the number of cookies baked
Assign a number to that variable
Print the value of the variable
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Variables are like labeled boxes where you keep things you want to remember, such as scores in a game or counts of items.
๐Ÿ’ผ Career
Understanding variables is essential for all programming jobs because they are the basic way to store and manage data in software.
Progress0 / 4 steps
1
Create a variable to store the number of cookies baked
Write a line of Go code to create a variable called cookiesBaked of type int and set it to 12.
Go
Need a hint?

Use the var keyword to create a variable, then give it a name, type, and value.

2
Add a helper variable for the message
Create a variable called message of type string and set it to "Number of cookies baked:".
Go
Need a hint?

Use var message string = "..." to create a text variable.

3
Use variables to prepare the output
Write a line of code that uses fmt.Printf to print the message followed by the cookiesBaked number on the same line. Use the format string "%s %d\n".
Go
Need a hint?

Use fmt.Printf with the format string and variables to print the message and number together.

4
Print the final message
Run the program and print the output showing the message and the number of cookies baked.
Go
Need a hint?

Run the program to see the message and number printed together.