On a first glance when I started working on go, for me it seems very very weird language. I am working on node-js for two and more years and then start working on golang is not so cool for me.
May be most of you can also agree with me on that. So, here’s the series on weird things about golang what i have got so far…
Ready steady GO!
Let’s start with variables of golang, declaring variables is really simple and sarcastic in golang. By the golang docs, variables are explicitly declared and used by the compiler in golang to check type-correctness of function calls.
Let take an example how simple is that,
var message = "hello world"
variable message is a type of string that is storing hello world in it. Let’s take another example,
var johnWickDeathToll int
We can also declare a variable without initialisation as shown above. But don’t worry about initialisation we have got secured. Yes you heard it right we are secured, there is no JAVA error like,
variable blahblah might not have been initialized
and also there is also no NULL values like NODEJS
YES, MR STARK WE WON!
0, empty string, false are default values for int/float, string and boolean variables respectively and by the docs, there are known as zero-valued variables. Yes, but there is still a weirdy nil values present in golang that can crash a program, but we will talk about it later.
Now let’s check how sarcastic in golang declaring variables,
areYouAString := "yes, i am string!!"
Yes you got it right the weird := syntax is known as short-hand syntax for declaring and initialising variables in golang, you don’t have to add var or anything else in front of variable!. But don’t worry about the type of variable again we have got secured by golang superhero that is compiler.
When we use short-hand syntax golang compiler will automatically look right hand side of line and find what variable is it. Is it a string or number(int/float) or a boolean value and use that variable according to that.
So far so good, we have got reference about declaring or initialising variable, there default values and all that. But if you thinking it’s not that weird although it is fun to work with golang, then we are not done here bro. Things are going about to weird now,
Let’s take an example,
Yes there is no weird in above syntax and output, breakingBadRating variable is type of int because we have initialised it as int value 10.
Let have a look at another example,
Yes, again there is no weirdness, breakingBadRating variable is type of floatbecause we have initialised it as 10.0
What weird thing caught me is that, we can make a float variable not declaring it a 10.0 instead of we can write it just as 10., Yes you heard it right look at below snippet,
Yes our variable breakingBadRating is still a type of float64 we have just initialised it with 10. not with a normal float number like 10.0, but yes golang support this. This can be a call for nightmare or may be a call for lot of future bugs.
GoLang is not so ready steady GO!
May be in future we can have a warning like make 10.0 not just 10. to make it a floating number, but till then make sure to caught it.
Here’s the end of first part of weirdness of golang, but still we have cover many things, so stay tuned!!
Thanks!
Previously published at https://medium.com/@adityasonel/weird-things-about-golang-part-1-4139e97f2bd4