Go’s 100 day trip-04 basic data types

Time:2021-12-31

catalogue
  • brief introduction
  • integer
  • Floating point type
  • Boolean
  • complex

brief introduction

In the definition of variables, we said that each variable has a type, and the type is used to restrict the interpretation of data in the computer. Like other computer languages, go language provides a wealth of data types. Let’s see what types there are and compare them with other languages.

integer

Integer is the type used to indicate that the variable is an integer. andCsimilar,GoInteger types are divided into two categories: unsigned and signed. Signed is simply a type that can represent negative numbers. Unsigned is this type, which has no sign and uniformly represents integers. Besides symbols, there is also length, because symbols and length determine the range of integers. Here isGoClassification of integers:

type explain Range
int Integer. The length is related to the byte size of the platform machine, usually 32 or 64 bits Related to bytecode
uint Unsigned integer, the length is related to the byte size of the platform machine, generally 32 or 64 bits Related to bytecode
int8 Signed 8-bit integer -128 ~ 127
uint8 Unsigned 8-bit integer 0 ~ 255
int16 Signed 16 bit integer -32768 ~ 32767
uint16 Unsigned 16 bit integer 0 ~ 65535
int32 Signed 32-bit integer -2147483648 ~ 2147483647
uint32 Unsigned 32-bit integer 0 ~ 4294967295
int64 Signed 64 bit integer -9223372036854775808 ~ 9223372036854775807
uint64 Unsigned 64 bit integer 0 ~ 18446744073709551615

Floating point type

GoThere are two floating point types,float32andfloat64, they all matchIEEE754Definition of specifications. When it comes to floating point types, we have to mention precision,float32Approximately 6-bit accuracy error is provided, andfloat64Provides an accuracy error of about 15 bits.
For example, the following code:

package main

import "fmt"

func main() {
	var i float32 = 10
	var j float32 = 10.0000000001

    var m float64 = 10
	var n float64 = 10.0000000001

	fmt.Println(i == j)
    fmt.Println(m == n)
}

Printed are:

true
false

Therefore, attention should be paid to the problem of accuracy in normal use. In addition to directly using decimal form, scientific counting method can also be used, such as

var i float32 = 10e-2 // 0.01

Boolean

There are two Boolean types:trueandfalse, usually used for logical judgment

complex

The complex number is not often used. In the mathematical definition, the complex number has real part and imaginary part.GoTwo precision complex types are provided,complex64andcomplex128They correspond to each otherfloat32andfloat64Accuracy.

var x complex128 = complex(1, 2) // 1+2i
var y complex128 = complex(3, 4) // 3+4i


i := 1 + 2i
j := 3 + 4i

The above is the writing of plural numbers. Just specify their real and imaginary parts respectively. Plural numbers can also be compared for equality

i := 1 + 2i
j := 1 + 3i
fmt.Println(i == j)

But the real part and imaginary part must be the sametrue

Recommended Today

[go web development] configure database connection pool

In this section, we will explain how connection pooling works behind it and explore how configuring a database can change or optimize its performance. Note: the database series contains quite a few theories, which are interesting but not important to the construction of applications. If you find it too difficult, you can browse it first […]