Basic data types help us store different kinds of information like numbers, text, and true/false values in a program.
Basic data types in Go
var variableName dataType // Examples: var age int var name string var isActive bool
Use var keyword to declare a variable with a type.
Common basic types are int for whole numbers, float64 for decimal numbers, string for text, and bool for true/false.
age that holds a whole number 30.var age int = 30
price that holds a decimal number 19.99.var price float64 = 19.99name that holds the text "Alice".var name string = "Alice"isOpen that holds the value true.var isOpen bool = trueThis program declares variables of different basic types and prints their values.
package main import "fmt" func main() { var age int = 25 var price float64 = 9.99 var name string = "Bob" var isMember bool = false fmt.Println("Age:", age) fmt.Println("Price:", price) fmt.Println("Name:", name) fmt.Println("Is member:", isMember) }
Go requires you to specify the type of variable when you declare it.
You can also let Go guess the type by using := like age := 25.
Basic types are the building blocks for storing data in your program.
Basic data types store simple values like numbers, text, and true/false.
Use int for whole numbers, float64 for decimals, string for text, and bool for true/false.
Declare variables with var and specify the type to hold data.
Practice
Solution
Step 1: Understand the data types
In Go,boolis used for true/false values,intfor whole numbers,stringfor text, andfloat64for decimals.Step 2: Match the type to true/false
Since true/false are boolean values, the correct type isbool.Final Answer:
bool -> Option DQuick Check:
Boolean type = bool [OK]
- Choosing int for true/false
- Using string for boolean values
- Confusing float64 with bool
age in Go?Solution
Step 1: Recall Go variable declaration syntax
In Go, variables are declared withvarfollowed by the name and then the type.Step 2: Check each option
var age int matches this syntax:var age int. Others do not follow Go syntax.Final Answer:
var age int -> Option BQuick Check:
Variable declaration = var name type [OK]
- Writing type before variable name
- Using := without initialization
- Mixing order of var and type
package main
import "fmt"
var x int = 10
var y float64 = 5.5
var z string = "Go"
func main() {
fmt.Println(x, y, z)
}Solution
Step 1: Understand variable values
Variablexis 10 (int),yis 5.5 (float64), andzis "Go" (string).Step 2: Check fmt.Println output
fmt.Printlnprints values separated by spaces without quotes for strings.Final Answer:
10 5.5 Go -> Option AQuick Check:
Print values as is, strings without quotes [OK]
- Expecting quotes around strings
- Confusing float64 with int
- Thinking fmt.Println causes error
var name string = 123 fmt.Println(name)
Solution
Step 1: Check variable assignment types
The variablenameis declared asstringbut assigned the integer 123, which is a type mismatch.Step 2: Verify other syntax
Semicolons are optional in Go,fmt.Printlnis correctly used, andnameis not a reserved keyword.Final Answer:
Cannot assign int to string variable -> Option CQuick Check:
Type mismatch error = assigning int to string [OK]
- Assigning wrong type value
- Thinking semicolon is mandatory
- Confusing reserved keywords
Solution
Step 1: Match data types to real-world data
Age is a whole number, soint. Height can have decimals, sofloat64. Name is text, sostring. Student status is true/false, sobool.Step 2: Check each option
var age int var height float64 var name string var isStudent bool matches these types correctly. Others assign wrong types to variables.Final Answer:
var age int var height float64 var name string var isStudent bool -> Option AQuick Check:
Correct types for data = int, float64, string, bool [OK]
- Mixing int and float64
- Using bool for text
- Assigning string to boolean
Ready to prove it?
Pass the test and this pattern moves unknown
