This is the 10th day of my participation in Gwen Challenge

How does Go’s constant work?

While Go is strict between variables of different numeric types, constants are more flexible. Literal constants such as 23,3.14159, and math. PI take up a certain amount of space, they have arbitrary precision, and there is no overflow or underflow. For example, the value of math.pi is specified as 63 locations in the source code, and constant expressions involving this value will be kept exactly where float64 can fit. Only when a constant or constant expression is assigned to a variable in a program does it become a “computer number” with the usual floating-point properties and precision.

In addition, because they are just numbers rather than values containing types, they can be used more freely than variables, softening some of the awkwardness of strict conversion rules. For example, the following expression:

sqrt2 := math.Sqrt(2)
Copy the code

This will compile because number 2 can be safely and accurately converted to float64 in SQRT ().

PS: A numeric variable cannot be converted at will, for example:

var a int64
var b int8
a = (int8) b
Copy the code

We don’t need constants. In the example above, math.sqrt (), the argument is float64, but we can pass in constant 2 without doing any type conversion.

Why does Go map not allow Slice as key?

A map lookup requires an “equal” comparison, but slicing is not implemented. It doesn’t implement equal yet because there’s no good definition of what does equal mean on these types? There are shallow versus deep comparisons, Pointers versus values, how to deal with recursive types, and so on. We can revisit this problem, but we won’t implement it for a while without thinking explicitly about what a good slice of equal should mean.

In Go 1, unlike the current version, equal is defined for structures and arrays, so you can use this type as a key key for a map. However, there is still no equal definition of slice.