Re: [go-nuts] question about switch on (type)

33 views
Skip to first unread message

Dave Cheney

unread,
Sep 14, 2011, 6:50:49 PM9/14/11
to golan...@googlegroups.com
If you reorder the clauses in your switch statement I suspect that the
first, non int clause will always trigger.

The best explanation that I can give is that all the remaining clauses
are actually the same type, interface{}

Cheers

Dave

On Thu, Sep 15, 2011 at 8:41 AM, d_smi...@rocketmail.com
<d_smi...@rocketmail.com> wrote:
> Why does the following program print "integer!"?  I expected it to print
> "object!" or maybe "string!".
> package main
> import "fmt"
> func showObject(o Object) {
> switch o.(type) {
> case int:
> fmt.Println("int!")
> case Integer:
> fmt.Println("integer!")
> case String:
> fmt.Println("string!")
> case Object:
> fmt.Println("object!")
> }
> }
> type Object interface{}
> type String Object
> type Integer Object
> func main() {
> a := String("foo")
> showObject(a)
> }
> (I find this program here https://gist.github.com/1217938)

Reply all
Reply to author
Forward
0 new messages