3 ms·
This code doesn't actually compile. You can't assign v to x.(type) like that.
by bunkat 12y ago
This code doesn't actually compile. You can't assign v to x.(type) like that.
- jerf 12y agoAre you sure? http://play.golang.org/p/dJC328-rh8 http://play.golang.org/p/dJC328-rh8 I did have to put "return x" as the last statement, though, not return v. And I'm not sure I'm happy about an "abs" that happily returns a string if fed a string, but, well, that's Go. Relevant bit of the spec: http://golang.org/ref/spec#Type_switches http://golang.org/ref/spec#Type_switches
- Donch 12y agoIt's horrible method with a horrible signature, that I didn't test :-) But the short variable declaration in the type switch does indeed save the casts the OP was complaining about. Even more correct and horrible: http://play.golang.org/p/ZMQKQhuUQT http://play.golang.org/p/ZMQKQhuUQT
- bunkat 12y agoYes, the return statement was what I was referring to. You can't use a type switch to do some sort of an implicit conversion that exists outside of the switch statement.