The .(type) assumption check is very useful but yet too early to stumble upon when you just started imo. :D
What you will also see alot is giving back a result + err from a function. result, err := function(x,y) and in addition to that the often used err != nil check
if err != nil {
fmt.Println("oops something went wrong")
}
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.
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.
The
.(type)
assumption check is very useful but yet too early to stumble upon when you just started imo. :DWhat you will also see alot is giving back a result + err from a function.
result, err := function(x,y)
and in addition to that the often usederr != nil
checkI would take a look on go project structure or code style guide best practises.
Welcome to GO :)
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 😁
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.Ahhh interesting, I see this is quite helpful. I'll need to use this in a little toy app to really cement the concept