I get more information from http://golang.org/ref/spec#Type_assertions
On Friday, September 27, 2013 1:27:43 PM UTC+8, Steven Blenkinsop wrote:
interface by saying what type it has. If you're wrong, your program fails
at runtime. You can use the comma-ok notation to get a boolean indicating
whether you were right or not rather than failing: `v, ok :=
e.Value.(*list.List)`. A type assertion doesn't let you change the type of
the value in the interface; the type you assert it to be has to be
identical to the type of the value that was put into the interface. The
exception is that you can assert that the value in an interface also
implements another interface, giving you a value of the second interface
type, or failing at runtime if you're wrong (which you can still avoid by
using the comma-ok notation).
Conversions are for when the compiler knows you can turn a value of one
type into a value of another type, so it fails at compile time if you're
wrong, rather than at runtime.
--On Thursday, September 26, 2013, hao5ang wrote:
Thank you very much for solving my problem.
Is this one kind of type casting?
*e.Value.(*list.List)*
That's called a type assertion. It lets you unwrap the value inside anThank you very much for solving my problem.
Is this one kind of type casting?
*e.Value.(*list.List)*
interface by saying what type it has. If you're wrong, your program fails
at runtime. You can use the comma-ok notation to get a boolean indicating
whether you were right or not rather than failing: `v, ok :=
e.Value.(*list.List)`. A type assertion doesn't let you change the type of
the value in the interface; the type you assert it to be has to be
identical to the type of the value that was put into the interface. The
exception is that you can assert that the value in an interface also
implements another interface, giving you a value of the second interface
type, or failing at runtime if you're wrong (which you can still avoid by
using the comma-ok notation).
Conversions are for when the compiler knows you can turn a value of one
type into a value of another type, so it fails at compile time if you're
wrong, rather than at runtime.
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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.