|
Drago Ivanov |
at May 9, 2013 at 3:45 pm
|
⇧ |
| |
Here is what i come up:
package main
import(
"fmt"
"strconv"
)
type Float32 float32
type Float64 float64
type TypeConverter interface {
typeConvert(text string)
}
func (f *Float32) typeConvert(text string) {
v, err := strconv.ParseFloat(text, 32)
if err == nil {
*f = Float32(v)
}
}
func convert(val TypeConverter, text string) {
val.typeConvert(text)
}
func main() {
{
var f Float32
convert(&f,"345.344")
fmt.Println(f)
}
}
Changing Float32 for Float64, gives compile time error:
./goplay.go:29: cannot use &f (type *Float64) as type TypeConverter in function argument:
*Float64 does not implement TypeConverter (missing typeConvert method)
09 май 2013, четвъртък, 15:10:57 UTC+3,
[email protected] написа:
Hi, I have the following code fragment:
package main
import(
"fmt"
"strconv"
)
func convert(val interface{}, text string) {
switch t := val.(type) {
case *float32:
v, err := strconv.ParseFloat(text, 32)
if err == nil {
*t = float32(v)
}
default:
panic("Unknown type")
}
}
func main(){
var f float64
convert(&f,"345.344")
fmt.Println(f)
}
I want if I supply wrong type to `convert`, compiler to tell me, instead waiting for runtime panic.
Is there any way?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
[email protected].
For more options, visit
https://groups.google.com/groups/opt_out.