Junior — Middle
What built-in data types in Go allow comparison operations?
sobes.tech AI
Answer from AI
Comparison operations in Go are supported for the following built-in data types:
- Boolean values (
bool): can be compared for equality and inequality. - Numeric types (
int,float32,float64,uint, etc.): support all standard comparison operations (==,!=,<,>,<=,>=). - Strings (
string): compared lexicographically using==,!=,<,>,<=,>=. - Pointers: compared for equality and inequality (whether they point to the same object or
nil). - Interfaces: compared for equality and inequality if their dynamic types and values are comparable.
- Arrays: compared element-wise if the elements are comparable.
Types that cannot be compared directly, such as slices (slice), maps (map), functions (func), do not support comparison operations except for nil checks.
Example of comparing strings and numbers:
package main
import "fmt"
func main() {
a := 10
b := 20
fmt.Println(a < b) // true
s1 := "apple"
s2 := "banana"
fmt.Println(s1 == s2) // false
}