Does go internally makes use of these different types based on specific situations and considerations, and if so what are those considerations and where are different types used. Is the internal-usage done based on the magnitude of numbers for instance (smaller numbers with int8, bigger numbers with int64 for instance?)
Also, is it possible to make Go decide about the concrete type of an interface{} value. My actual scenario is: I have created a parser which reads a number-representation (a string, e.g. “20_000_000”), I then use strconv.ParseInt to parse the string, and save the value into a struct field which has type any. Now when I want to read back the field, is it possible to let Go decide what numeric type would best fit the saved numeric-value, rather than aggressively converting it into some numeric-type I think would be good (like using a type-switch).
As shown in the docs, smaller types such as int8 and uint8 are used for smaller numberswhile larger types(int64 etc) are used for large numbers. Go also infers the type depending on what is being assigned.
Hello there. Short answer is No. Go uses static typing system and can guess the type without specification only when you create a variable e.g.
a := 5 // Type is int.
b := "test" // Type is string.
But under the hood go does not change for the lower type. For numbers, it’s gonna be regular int type, which refers to max system integer and can result in int32 or int64 respectfully. You need to specify exact types on your own. In addition, any is a type by itself. Golang doesn’t know what is inside the container and if you want to get a number out of it, you’ll need to do type assertion to a specific type of your choosing.