1. 程式人生 > >Learn Go Constants — A visual guide

Learn Go Constants — A visual guide

You don’t want to type magical values everywhere in your code, so, declare them with constants and reuse them again.

Magical values are not safe, you may type them incorrectly, so, a constant is your safer alternative. Also, it’s more enjoyable to see a constant instead of a magic value; People will understand more easily what’s going on in your code.

We hope that we’ll get some speed benefits of using constants, because, the compiler will be able to do more optimizations, because, it knows that the value of the constant will never change.

My favorite is the untyped constants. They’re really a genius idea. You’ll get flexibility and high-precision calculations when you use untyped constants.