The four fundamental types
Go’s basic value types form the foundation of all data in your programs:- Strings: Text data like
"hello" - Integers: Whole numbers like
10 - Floats: Decimal numbers like
10.5 - Booleans: True or false values like
true
Working with values
Let’s see these types in action:Integer arithmetic
Integers represent whole numbers without decimal points. You can perform standard mathematical operations:When you divide two integers, Go performs integer division and discards any remainder. For example,
7 / 2 equals 3, not 3.5. To get decimal results, use float division.String values
Strings represent text and must be enclosed in double quotes:Go distinguishes between double quotes
" for strings and single quotes ' for runes (individual characters). Always use double quotes for text strings.Boolean values
Booleans represent truth values - eithertrue or false:
if statements and loops.
Float arithmetic
Floats (floating-point numbers) represent decimal values. Go provides rich support for decimal arithmetic:Float + Float
Float + Integer
Float division
Type matters
Go is a statically typed language, which means every value has a specific type that’s known at compile time:Integer types in depth
Go actually has several integer types with different sizes:int: Platform-dependent size (32 or 64 bits)int8: 8-bit signed integer (-128 to 127)int16: 16-bit signed integerint32: 32-bit signed integerint64: 64-bit signed integeruint: Unsigned integer (positive only)
int - Go will choose the appropriate size for your platform.
Float types in depth
Go provides two float types:float32: 32-bit floating pointfloat64: 64-bit floating point (double precision)
10.5, Go defaults to float64 for maximum precision.
Comparing values
You can compare values using comparison operators, which return boolean results:==equal to!=not equal to>greater than<less than>=greater than or equal to<=less than or equal to
Experiment
Try these exercises to deepen your understanding:-
Calculate your age in days: Multiply your age by 365
-
Try different divisions: See the difference between integer and float division
-
Combine operations: Use multiple operators in one expression
Key takeaways
- Go has four fundamental types: integers, floats, strings, and booleans
- Each type has specific operations you can perform on it
- Integer division truncates decimals, float division preserves them
- Go is statically typed - every value has a specific type
- Mixed arithmetic with floats and integers produces float results
- Comparison operations return boolean values