In Go, data types define the kind of values a variable can hold. Understanding data types helps you write efficient and error-free code.
Data Type | Description | Example |
---|---|---|
int | Integer values (whole numbers) | 42, -7, 0 |
float64 | Floating-point numbers (decimal values) | 3.14, -0.001, 2.0 |
string | Sequence of characters | "Hello, Go!" |
bool | Boolean values (true or false) | true, false |
byte | Alias for uint8, represents raw data | 0xFF, 65 |
You can explicitly declare a variableβs type using the var
keyword, like this:
var age int = 25 var price float64 = 9.99 var name string = "GoLang" var isActive bool = true
Go can automatically detect the type based on the assigned value, so you can use the shorthand :=
for declaration and assignment:
age := 25 // int price := 9.99 // float64 name := "GoLang" // string isActive := true // bool
Below are some variable declarations. Guess the data type before checking the answer!
var score = 100
β var temperature = 36.6
β var greeting = "Hello"
β var isAvailable = false
β :=
when possible.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!