Hi everybody,
one question about converting something to a "string"
Problem:
The pair "panic" and "recover" give you the possibility to
"throw" an arbitrary object as error.
example: panic("my error")
"recover" returns an "interface{}" or nil. ...
my problem is that the only way to convert an "interface{}",
using a "unknown" type, into an "string" is (as I know)
fmt.Sprintf("%s", ifc)
Is this right, or can I test for an "string" like interface?
what is the standard in GO if something support a serialization into a
"string" object
I already know the "os.Error" Interface, which require a "String" method.
mfg, Andreas Otto
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJM67YAAAoJEGTcPijNG3/ADaUIAIQ4E+RtVqBANtUbfHQTE5TI
8FhqxIfsJkQSZUO4uCBRczM3Fp/CL9j74AA3CL1Rb9HTX8ThdCZ6kwushrQ+zOAL
qo5psfZKJYqDlSPOJQ1V/nWROihgVuIGLbxgGCUAEFPXhHHvQShPH7Q8leOUIYpc
ml+Pl3gPrHApbx+TGa8tv2xI4ATolxRw987Qcs9XBdK97iQcdTgCAz13d7E4+d67
21XcjfYkC2DnbjIu+c0pm2iqnu3540CyXDRAcy0Zi/jcRL82GL1A1rdroP2YjlTc
Qkz2nr/TRX200idVW5xDfEq880obkMmM4xjh0g7SSRU8ESnkdZ2NPOG4kqFOQbE=
=Gucw
-----END PGP SIGNATURE-----
that's exactly it.
it's conventional to panic with an object that has a String method;
then you can do:
defer func() {
switch x := recover().type {
case os.Error:
log.Printf("panic with error %s", x.String())
case nil:
...
...
> my problem is that the only way to convert an "interface{}",
> using a "unknown" type, into an "string" is (as I know)
>
> fmt.Sprintf("%s", ifc)
>
> Is this right, or can I test for an "string" like interface?
You can use a type test. If X is your interface{} variable and T is
the type you want to test for -- be it an interface type or a concrete
type -- then
aT, isT := X.(T)
will set isT to true iff X has underlying type T and aT to the corresponding
T value (the zero value if isT = false).
> what is the standard in GO if something support a serialization into a
> "string" object
fmt.Stringer looks like the plausible candidate.
Are you really sure you want to be doing this test? (You can test an
interface{} for equality against a string anyway ..)
Chris
--
Chris "allusive" Dollin
hi,friend!can you relove this problem?I have encountered this problem.
hi,friend!
can you relove this problem?I have encountered this problem.
On Tuesday, November 23, 2010 8:55:32 PM UTC+8, chris dollin wrote: