pixabay.com |
Golang is the functional programming that really have concern about performance, giving limitation to prevent overuse memory is the big deal. for example, golang has a variative type of data such as int, string, boolean, float, array etc. I wonder that you are thinking about "what kind of strictness that is? its totally similar other language", YES. but golang have specification in numeric type of data such as int have int8, int16, int32, int64. golang also have uint8, uint8, uint32 and uint64 which is its really strict you know. the following number after type of data is stand for the length. Obviously you could use the largest type of data for all the variables, but you know it could overwhelming if you put int64 in "is_valid" variable right? joking :D. golang really care about performance, because each length of variable is matters for memory.
yes! golang its totally directing you to put type of data according to the length. the more you strict about the type of data, the more your apps got faster.
but in the other side, golang also have flexible type of data, like Any in Swift. you could put a lot of values in variable, its called null interface data type. I know its weird, I totally agree with you, but you know thank god for this type, because we need flexibility also in some cases. such as dealing with JSON or Dictionary values, you can't use other data type instead of interface, maybe you can use array or slice but you need put the seem data type for each key n values. for example .
in above example, you put all the data type as string, it's totally works but "age" key is belongs to numeric data type right? and you unable to do that if you are only using array or slice, that's why we need interface this time. for example
in above example, you could see the example of using null interface data type, we finally enable to put the right data type for "age" key. see the output below.
you could use interface for any kind of cases but it's totally not recommended, because you could ruin the performance. I recommend you only using interface if dealing with Dictionary or JSON cases.
Conclusion
Golang is totally strict language, but it's not a drawback cause the main purposes is to keep the best performance for your apps and not overusing memory. but golang also have flexible data type called null interface, but you are not recommending to put it in all variable you define, it's only use for Dictionary or JSON values.