Data Types
Start with Go Data Types
A data type describes what kind of value a variable stores and which operations are valid. Go offers numeric, boolean, string and composite types.
What you’ll learn
- Identify common built-in types.
- Use %T to inspect types.
- Understand that types constrain allowed operations.
Understand the idea
Integers include int, int8 through int64, and unsigned forms. Floating-point types include float32 and float64. bool stores true or false; string stores a sequence of bytes, usually UTF-8 text.
Go also supports byte as an alias for uint8, rune as an alias for int32, and complex number types. Later modules introduce arrays, slices, maps and structs.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import "fmt"
func main() {
age := 24
distance := 7.5
active := true
city := "Pune"
fmt.Printf("%T: %v\n", age, age)
fmt.Printf("%T: %v\n", distance, distance)
fmt.Printf("%T: %v\n", active, active)
fmt.Printf("%T: %v\n", city, city)
}
Output
int: 24
float64: 7.5
bool: true
string: PuneTrace what the program does
%T prints the type of a value and %v prints its value. From the literals used in these declarations, Go infers int, float64, bool and string.
Avoid common mistakes
- A decimal value is not automatically assignable to an integer variable.
- A
stringis not a single character; later lessons distinguish bytes, runes and strings.
Try it yourself
Declare a float32 measurement explicitly, print its type using %T, then compare that output with a float64 literal.
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Go Data Types in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.