Type conversion VS type assertion in go

Just learned an interesting subtlety about Go. There are two ways to “cast” values. I put cast in quotes because one isn’t really casting but it’s just the general term I always use for saying “change on type to another one”.

So what’s the difference? Well conversion should be used when you are dealing with a type, whether that be a constant or struct or whatever. Assertion is used when when you’re dealing with an interface. Say for example a method returns an interface instead of a struct, there’s still a value associated with the return but it has a generic type. You can use assertion to convert the return value to any type that implements that interface.

Pretty nifty!

I had to wrap the number 5 in an int64 when doing var x interface{} = int64(5). Check out this post to find out why