0
0
Goprogramming~10 mins

Basic data types in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Basic data types
Start
Declare variable with type
Assign value matching type
Use variable in code
Program runs with correct types
End
This flow shows how Go variables are declared with specific types, assigned values, and used in the program.
Execution Sample
Go
package main
import "fmt"
func main() {
  var age int
  age = 30
  var name string
  name = "Alice"
  fmt.Println(name, age)
}
This code declares variables of basic types int and string, assigns values, and prints them.
Execution Table
StepActionVariableTypeValueOutput
1Declare variableageint0 (default)
2Assign valueageint30
3Declare variablenamestring"" (default)
4Assign valuenamestring"Alice"
5Print variablesname, agestring, int"Alice", 30Alice 30
6Program ends
💡 Program ends after printing variables with assigned values.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
ageundefined030303030
nameundefinedundefinedundefined"""Alice""Alice"
Key Moments - 3 Insights
Why does 'age' start with 0 before assignment?
In Go, variables declared with a type get a default zero value (0 for int) before explicit assignment, as shown in step 1 of the execution_table.
Can we assign a string value to 'age' variable?
No, because 'age' is declared as int type. Assigning a string would cause a compile error. The execution_table shows 'age' only holds int values.
Why do we need to declare variable types explicitly?
Go is statically typed, so variable types must be declared to ensure type safety. This is shown in the declaration steps in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' after step 4?
A""
B"Alice"
Cundefined
D30
💡 Hint
Check the 'Value' column for 'name' at step 4 in the execution_table.
At which step does 'age' get assigned the value 30?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Value' columns for 'age' in the execution_table.
If we tried to assign a string to 'age', what would happen?
ACompile error due to type mismatch
BProgram prints the string
CVariable 'age' changes type to string
DProgram runs but value ignored
💡 Hint
Recall Go's static typing explained in key_moments and the variable types in execution_table.
Concept Snapshot
Basic data types in Go:
- Declare variables with 'var name type'
- Types include int, string, bool, etc.
- Variables get default zero values
- Assign values matching declared type
- Type safety prevents mismatched assignments
Full Transcript
This example shows how Go handles basic data types. Variables are declared with a type, like int or string. When declared, they get default zero values (0 for int, empty string for string). Then we assign actual values. The program prints these values. Go checks types strictly, so you cannot assign a string to an int variable. This ensures your program runs safely and predictably.