Type detection: Difference between revisions
Content added Content deleted
(Added Java) |
(Added Go) |
||
Line 160: | Line 160: | ||
I'm a fixnum: 123 |
I'm a fixnum: 123 |
||
I don't know how to print this. |
I don't know how to print this. |
||
</pre> |
|||
=={{header|Go}}== |
|||
Note that Go doesn't really have a character type. A single quoted character (such as 'd') is by default a ''rune'' (or 32 bit integer) literal representing its Unicode code-point. |
|||
<lang go>package main |
|||
import "fmt" |
|||
type any = interface{} |
|||
func showType(a any) { |
|||
switch a.(type) { |
|||
case rune: |
|||
fmt.Printf("The type of '%c' is %T\n", a, a) |
|||
default: |
|||
fmt.Printf("The type of '%v' is %T\n", a, a) |
|||
} |
|||
} |
|||
func main() { |
|||
values := []any{5, 7.5, 2 + 3i, 'd', true, "Rosetta"} |
|||
for _, value := range values { |
|||
showType(value) |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
The type of '5' is int |
|||
The type of '7.5' is float64 |
|||
The type of '(2+3i)' is complex128 |
|||
The type of 'd' is int32 |
|||
The type of 'true' is bool |
|||
The type of 'Rosetta' is string |
|||
</pre> |
</pre> |
||