Home > Backend Development > Golang > How many types of constants are there in Go language?

How many types of constants are there in Go language?

青灯夜游
Release: 2023-01-09 10:39:54
Original
1793 people have browsed it

There are 5 types of Go language constants: Boolean constants, integer constants, floating point constants, complex number constants and string constants. Constants in the Go language are defined using the keyword const, which is used to store data that will not change. Constants are created at compile time, even if they are defined inside a function, and can only be boolean, numeric (integer, integer, floating point and complex numbers) and string types.

How many types of constants are there in Go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Constant refers to a fixed value that may not change during the execution of the program. These fixed values ​​are also called literals.

Constant is the most basic element in the program and cannot be reassigned after being defined.

Constants in the Go language are defined using the keyword const, which is used to store data that will not change. Constants are created at compile time, even if they are defined inside a function, and can only be of Boolean type. Numeric types (integers, floats, and complex numbers) and string types. Due to compile-time restrictions, the expression defining a constant must be a constant expression that can be evaluated by the compiler.

Boolean constants

Boolean constants have only two values, true (true) or false (false).

The code is as follows:

const x = true
fmt.Print(x) //输出true
Copy after login

Integer constant

Integer constant is actually a positive number

The code is as follows:

const x = 20
fmt.Print(x) //输出20
Copy after login

Floating point number constant

Floating point number constant is actually a decimal number

The code is as follows:

cOnstx= 0.618
fmt.Print(x) //输出%f0.618
Copy after login

Character/string constant

The code is as follows:

const x = 'a'
fmt.Print(x) //输出97

const x = "a"
fmt.Print(x) //输出a
Copy after login

Complex constant

The code is as follows:

const x = 3 + 2i
fmt.Print(x) //输出%v(3+2i)
Copy after login

If you look carefully at children's boots, you will find 'a The output values ​​​​of ' and "a" are different. 'a' in single quotes represents characters, and "a" in double quotes represents strings. In the Go language, '1', "1", and 1 are different values, which is the same as in the C language. Children's boots who are interested can try the output themselves.

Extended knowledge: untyped constants

There is something unusual about the constants in the Go language. Although a constant can have any definite underlying type, such as int or float64, or a base type like time.Duration, many constants do not have an explicit underlying type.

The compiler provides higher-precision arithmetic operations for these numeric constants that do not have an explicit underlying type than the underlying type. It can be considered that there is at least 256 bit operation precision. There are six untyped constant types, namely untyped boolean, untyped integer, untyped character, untyped floating point number, untyped complex number, and untyped string.

By deferring the specific type of a constant, it not only provides higher operation accuracy, but also can be directly used in more expressions without explicit type conversion.

[Example 1] math.Pi is an untyped floating-point constant that can be directly used wherever floating-point numbers or complex numbers are required:

var x float32 = math.Pi
var y float64 = math.Pi
var z complex128 = math.Pi
Copy after login

If math.Pi is determined to be of a specific type, For example, if float64 is used, the accuracy of the result may be different. At the same time, where a float32 or complex128 type value is required, a clear forced type conversion is required:

const Pi64 float64 = math.Pi
var x float32 = float32(Pi64)
var y float64 = Pi64
var z complex128 = complex128(Pi64)
Copy after login

[Related recommendations: Go video tutorialprogramming teaching

The above is the detailed content of How many types of constants are there in Go language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template