How many numerical types are there in Go?

青灯夜游
Release: 2023-01-09 11:08:11
Original
1785 people have browsed it

There are three types of numerical types in Go: 1. Integer types, which can be divided into platform-independent integers and platform-related integers. The difference is whether the length is consistent under different CPU architectures or operating systems. 2. Floating point types can be divided into float32 and float64, which correspond to the single-precision and double-precision floating-point numerical types in IEEE754 respectively. 3. Complex numbers can be divided into two types: complex128 (64-bit real and imaginary numbers) and complex64 (32-bit real and imaginary numbers). Complex128 is the default type of complex numbers.

How many numerical types are there in Go?

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

The basic data types in the Go language system generally include numerical types and string types.

The number value types can be divided into the following types: integers, floating point numbers, complex numbers, each of which contains numerical types of different sizes, for example, signed integers include int8 , int16, int32, int64, etc. Each numerical type determines the corresponding size range and whether it supports positive and negative signs.

Go language integer type (integer type)

In Go language, integers are divided into platform-independent integers and platform-related integers. The main difference lies in whether the lengths of these integer types are consistent under different CPU architectures or operating systems.

Platform-independent integer type

The independent integer type distinguishes signed integer types and unsigned integer types, and the data ranges they represent are different.

How many numerical types are there in Go?

Platform-related integer types

Go natively provides three platform-related integer types, namely int, uint and uintptr.

How many numerical types are there in Go?

Special note: When writing code with portability requirements, never rely heavily on the length of these types.

You can get the length of three integers on the platform through the SizeOf function provided by the unsafe package.

func main() {
	var a, b = int(5), uint(6)
	var p uintptr = 0x12345678
	fmt.Println("signed integer a's length is", unsafe.Sizeof(a))
	fmt.Println("unsigned integer b's length is", unsafe.Sizeof(b))
	fmt.Println("uintptr's length is", unsafe.Sizeof(p))
}

// 打印输出
signed integer a's length is 8
unsigned integer b's length is 8
uintptr's length is 8
Copy after login
Formatted output

go provides the standard library fmt package to format and output different base formats.

func main() {
	var a int8 = 66
	fmt.Printf("%b\n", a) //输出二进制:1000010
	fmt.Printf("%d\n", a) //输出十进制:66
	fmt.Printf("%o\n", a) //输出八进制:102
	fmt.Printf("%O\n", a) //输出八进制(带0o前缀):0o102
	fmt.Printf("%x\n", a) //输出十六进制(小写):42
	fmt.Printf("%X\n", a) //输出十六进制(大写):42
}
Copy after login

Go language floating point type (decimal type)

Go language provides float32 and float64. Point types, which respectively correspond to the single-precision and double-precision floating-point numerical types in IEEE754, basically correspond to the float and double types in Java.

Go language provides two ways to represent floating point types, decimal type and scientific notation; scientific notation is divided into two representation methods: decimal and hexadecimal.

Decimal notation

3.1415
.15  // 整数部分如果为0,整数部分可以省略不写
81.80
82. // 小数部分如果为0,小数点后的0可以省略不写
Copy after login

Scientific notation

十进制表示法:

6674.28e-2 // 6674.28 * 10^(-2) = 66.742800
.12345E+5  // 0.12345 * 10^5 = 12345.000000

十六进制表示法:

0x2.p10  // 2.0 * 2^10 = 2048.000000
0x1.Fp+0 // 1.9375 * 2^0 = 1.937500
Copy after login

Formatted output

var f float64 = 112.676899
	// 浮点类型第一种:十进制表示法
	fmt.Printf("%f\n", f) // 112.676899
	// 浮点类型第二种:科学计数法--十进制表示法
	fmt.Printf("%e\n", f) // 1.126769e+02
	// 浮点类型第二种:科学计数法--十六进制表示法
	fmt.Printf("%x\n", f) // 0x1.c2b52502eec7dp+06
Copy after login

Go language plural

Go Two complex number types are provided, they are complex64 and complex128. The real and imaginary parts of complex64 are both of float32 type, while the real and imaginary parts of complex128 are both of float64 type. If a complex number is not explicitly typed, its default type is complex128.

Go provides three representation methods:

1. Use a complex literal value to directly initialize a complex type variable

var c = 5 + 7i
var d = 0o123 + .12345E+5i // 83+12345i
Copy after login

2. Use Go to provide The complex function creates a complex128 type value

var c = complex(5, 6) // 5 + 6i
var d = complex(0o123, .12345E+5) // 83+12345i
Copy after login

3. Using the predefined functions real and imag provided by Go, you can obtain the real part and imaginary part of a complex number, and the return value is a floating point type

var c = complex(5, 6) // 5 + 6i
r := real(c) // 5.000000
i := imag(c) // 6.000000
Copy after login

Extended knowledge: Custom numerical types

can be generated based on native numerical types through the type keyword provided by the Go language Declare a new type.

For example,

type NewInt int32
Copy after login

NewInt is our newly defined type. The underlying type is int32, but it is two different types from the native type int32. It cannot be assigned directly and needs to be displayed and converted. .

type NewInt int32

	var m int = 5
	var n int32 = 6
	var a NewInt = NewInt(m) // ok
	var b NewInt = NewInt(n) // ok
Copy after login

Of course, if you want to be able to directly assign and use native types, Go provides type alias (Type Alias) syntax to customize numerical types.

type NewInt = int32

	var m int32 = 5
	var n NewInt = m
Copy after login

Summary

There are three types of native numerical types in Go: integer, floating point and complex. From the above brief introduction, we can know that Go is basically different from most mainstream languages. Of course, Go is also different, such as its native support for plural types.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of How many numerical types are there in Go?. 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