DEV Community

Discussion on: 30 Days of Go: Day 1

Collapse
 
kmukabe profile image
Kacha

Thanks for that feedback @supportic, I was having trouble seeing how I'd use the type switch at this point. I'll keep it on the back burner for now, I'm planning to take a look at function syntax and data structures today so thanks for the links.

Thanks for the welcome as well glad to be here 😁

Collapse
 
supportic profile image
Supportic

Pretend using the universal any type interface{} in a function parameter. Say you work with JSON values and you want to get the type of them in order to process them properly.
A type switch may help you because you know the structure/types you get and therefore can safely assume which types you get. (see type switch )
In the example you also see a very handy print function.
fmt.Printf("%v\n", res) eventually you know the printf function from C/C++. However the %v let's you print any type you want. Additionally with classes you might want to use %+v to also see the properties.

Thread Thread
 
kmukabe profile image
Kacha

Ahhh interesting, I see this is quite helpful. I'll need to use this in a little toy app to really cement the concept