| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package zeros |
| 7 |
|
| 8 |
func assert(cond bool, msg string) { |
| 9 |
if !cond { |
| 10 |
panic(msg) |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
func tp0[T int | string | float64]() T { return T(0) } |
| 15 |
|
| 16 |
func tpFalse[T ~bool]() T { return T(false) } |
| 17 |
|
| 18 |
func tpEmptyString[T string | []byte]() T { return T("") } |
| 19 |
|
| 20 |
func tpNil[T *int | []byte]() T { return T(nil) } |
| 21 |
|
| 22 |
func main() { |
| 23 |
|
| 24 |
var zi int |
| 25 |
var zf float64 |
| 26 |
var zs string |
| 27 |
|
| 28 |
assert(zi == int(0), "zero value of int is int(0)") |
| 29 |
assert(zf == float64(0), "zero value of float64 is float64(0)") |
| 30 |
assert(zs != string(0), "zero value of string is not string(0)") |
| 31 |
|
| 32 |
assert(zi == tp0[int](), "zero value of int is int(0)") |
| 33 |
assert(zf == tp0[float64](), "zero value of float64 is float64(0)") |
| 34 |
assert(zs != tp0[string](), "zero value of string is not string(0)") |
| 35 |
|
| 36 |
assert(zf == -0.0, "constant -0.0 is converted to 0.0") |
| 37 |
|
| 38 |
assert(!tpFalse[bool](), "zero value of bool is false") |
| 39 |
|
| 40 |
assert(tpEmptyString[string]() == zs, `zero value of string is string("")`) |
| 41 |
assert(len(tpEmptyString[[]byte]()) == 0, `[]byte("") is empty`) |
| 42 |
|
| 43 |
assert(tpNil[*int]() == nil, "nil is nil") |
| 44 |
assert(tpNil[[]byte]() == nil, "nil is nil") |
| 45 |
} |