This is a creation in Article, where the information may have evolved or changed. Go by Example:constants
Go supports constants of character, String, Boolean, and numeric values.
Const declares a constant value.
A Const statement can appear anywhere a var statement can.
Constant expressions perform arithmetic with arbitrary precision.
A numeric constant has no type until it's given one, such as by an explicit cast.
A number can be given a type by using it in a context the requires one, such as a variable assignment or function call. For example, here Math. Sin expects a float64.
Package Mainimport "FMT" import "math" Const s string = "constant" Func main () { fmt. Println (s) const n = 500000000 Const D = 3e20/n fmt. Println (d) FMT. PRINTLN (Int64 (d)) FMT. Println (Math. Sin (n))}
Translated:
The Go language supports constant types with Characer string character Boolean numeric numeric value
Declare a constant value by using the Const keyword
A constant declaration can show that any VAR declaration can be
Constant expressions can show arbitrary precision arithmetic operations
An arithmetic expression can have no type until he is given a value, for example by displaying a transform
A number can be used when it is necessary to use a given type, such as the assignment of a variable or a call to a function.
For example, here's math. Sin is expected to be a float64 type.
Package Mainimport "FMT" import "math" Const s string = "constant" Func main () { fmt. Println (s) const n = 500000000//Define a constant note here the constant type is not allowed to change the const D = 3e20/n //The FMT is computed according to the science and technology method . Println (d) FMT. PRINTLN (Int64 (d)) FMT. Println (Math. Sin (n))}
Run results
$ go Run constant.go
constant
6e+11
600000000000
-0.28470407323754404