hi there serdar gagalov here for infoworld at idg in this episode of smartgo i'm going to
give a quick tour of go's empty interface type and the use of type assertions with empty interfaces
now in an earlier episode of this series i talked about the interface type
interfaces are collections of methods and so a value of some interface type can be any value
that implements those described methods this way you could have a number of different kinds of
types each of which has a common set of behaviors across them a common interface to their methods
go also has what's called the empty interface type this is an interface that doesn't
implement any methods and so it can hold any type it's a kind of a container into which we can put
objects of any type and unpack them later this becomes useful if we want to construct behaviors
where we could receive one of any number of types and sort out what to do with each type as we go
let's start with a simple example of an empty interface variable here i'm creating a string
named title that uses an empty interface i also have an integer and a float value that i am
putting inside interfaces under the date and rating variables now if i want to verify what's
inside the interface if i want to verify in this case that it's actually a string i can do this
by way of what's called a type assertion this is a syntactical construct in go where i take
the name of a variable followed by dot and then the name of a given type in parenthesis
this essentially means if this object is to type in question return the object as
a string and a true value if not return an empty version of that type of object and a false value
so in the first three cases we get back an integer and string and float value along with a true to
indicate that their assertion was successful the last one returns zero the empty value for that
type and a false value because well a date value isn't a 64-bit float in this case it's an integer
now if we have a type assertion that is wrong and is it handled with an error it will cause the go
runtime to panic panics are something i will want to delve into in detail in a future video but they
are basically an error that causes the entire go program to stop in this example i'm asserting
a float64 value from a string but i'm not handling any error value that comes back from it
so if i run this code it will crash however if i swap this code for what that does have an error
handler it won't panic the program will at least continue to run and i will be able to do something
with the resulting value and the resulting error another thing we can do with the interface type
is use it in a function signature we can use it as a type that a function accepts
so a function can theoretically accept multiple types of objects
in this example admittedly not very useful we can supply any variable type to the action function
which is then in turn supplied to the print line function now to do something really useful with
this though we'd want to be able to elegantly make decisions based on the type of an object
now to do that we can use what's called a type switch it's formatted like a regular switch
case statement and go but it uses types instead of values here we have an example of a simple
type switching statement we take in something of type interface derive its underlying object as q
and then use the case statements to take action if the type is one of the ones listed here
if we don't find any matches we can use the default to just say type not recognized
so this when i run it will recognize these integer and string values
but it doesn't recognize a slice of strings and so for that last value we get an error message
but at least the program doesn't bomb out entirely so in sum one of the things the interface type
gives us is flexibility with how to deal with the types of variables that we have we can
wrap a variable of any type in an empty interface pass it around and then unwrap it as needed to do
more type specific work on the underlying object that's it for this video if you liked it please
leave a comment below don't forget to subscribe to the itg techtalk channel on youtube and for
more smart go and more david serdar be sure to follow me on facebook youtube and infoworld.com