[Golang] Understand the basic data types in Go language

Table of contents

plastic surgery

basic integer 

special integer

floating point

Boolean

character type

string escape character 

multiline string

Common operations on strings

plural


No matter what language we are learning, we have to get acquainted with the data types in this language. Of course, learning Go is no exception. We also need to get acquainted with its related data types. Of course, these data types are basically the same. If you learn some previous language learning experience, you will learn more quickly in understanding.

There are abundant data types in the Go language. In addition to basic integers, floating-point types, Boolean types, and strings, there are also arrays, slices, structures, functions, maps, and channels.


plastic surgery

Notes for beginners : Integer data refers to numeric data that does not contain a fractional part . Integer data is only used to represent integers and is stored in binary form.

basic integer 

Integers in Go language are divided into signed and unsigned types:

  • Signed integer types by length: int8, int16, int32, int64
  • Unsigned integer types by length: uint8, uint16, uint32, uint64.

uint8It is the type we are familiar with byte, int16corresponding to the type in C language short, int64corresponding to the type in C language long. 

special integer

The special integer type here refers to two kinds of signed and unsigned integers int and uint that generally correspond to the machine word size of a specific CPU platform; among them, int is the most widely used numeric type. These two types have the same size, 32 or 64bit, but we cannot make any assumptions about this; because different compilers may produce different sizes even on the same hardware platform; and there is also an unsigned The integer type uintptr does not specify a specific bit size but is large enough to hold a pointer. The uintptr type is only needed for low-level programming, especially where Go language interacts with C language function libraries or operating system interfaces

Note : The length returned by the built-in function to get the length of an objectlen()can vary according to the byte length of different platforms. In actual use, the number of elements in slices or maps can intbe used toWhen it comes to the structure description of binary transmission, reading and writing files, in order to keep the structure of the file from being affected by the byte length of different compilation target platforms, do not useintand uint.

package main

import (
	"fmt"
)

func main() {
	var a int
	var b int32
	var c uint = 10

	a = 2147483647
	b = -2147483648

	fmt.Print(a, b, c)
}

floating point

Go language supports two precision floating point numbers, float32 and float64. Their arithmetic specification is defined by the IEEE754 International Standard for Floating-Point Numbers, which is supported by all modern CPUs.

These two floating-point data formats follow IEEE 754the standard:  float32 The maximum range of floating-point numbers is approx.  3.4e38, which can be defined using the constant: math.MaxFloat32float64 The maximum range of floating-point numbers is approx  1.8e308. and can be defined using a constant: math.MaxFloat64. (It should be noted that because float32 has only 23 effective bits, the other bits are used for exponents and symbols; when the integer is greater than the range that can be expressed by 23bit, the representation of float32 will have errors)

The way to print floating point numbers: here we use fmtthe pack verb%f

package main

import (
	"fmt"
	"math"
)

func main() {
	var a float64

	a = 5.923871

	fmt.Print(a)
	fmt.Printf("\n%f\n", math.Pi)
	fmt.Printf("%.5f\n", math.Pi)
}

Boolean

Note for newbies : A Boolean type has only two values: true and false. The condition part of the if and for statements are Boolean values, and comparison operations such as == and < also produce Boolean values. The unary operator!corresponds to the logical NOT operation, so!truevaluefalseis, more wordy(!true==false)==true, although the expression is different, but we generally use concise Boolean expressions, just like xx==true.

boolBoolean data is declared by type in Go language , and Boolean data has true(真)only false(假)two values. 

Boolean values ​​can be combined with && (AND) and || (OR) operators, and have short-circuit behavior: if the value on the left side of the operator can already determine the value of the entire Boolean expression, the value on the right side of the operator will no longer be evaluated , so the following expressions are always safe:

s != "" && s[0] == 'x'

Note: &&the priority ||is higher than )

Boolean values ​​are not implicitly converted to numeric values ​​0 or 1, and vice versa. The conversion must be assisted by an explicit if statement:

i := 0
if b {
    i = 1
}

Notice:

  1. The default value for Boolean variables is false.
  2. Coercion from integer to boolean is not allowed in Go language.
  3. The Boolean type cannot participate in numerical operations, nor can it be converted to other types.

character type

Note for newbies : A string is an immutable sequence of bytes. Strings can contain arbitrary data, including byte value 0, but are usually used to contain human-readable text. Text strings are usually interpreted as sequences of Unicode codepoints (runes) encoded in UTF8

Strings in the Go language appear as native data types, and using strings is like using other native data types (int, bool, float32, float64, etc.). The internal implementation of strings in Go uses UTF-8encodings. The value of the string 双引号(")is the content in , and non-ASCII characters can be directly added to the source code of the Go language.

String values ​​can also be written as string literals by enclosing a sequence of bytes within double quotes:

s1 := "Hello"
s2 := "World"

string escape character 

Common escape characters for strings in the Go language include carriage return, line feed, single and double quotes, tabs, etc., as shown in the following table.

multiline string

When defining a multi-line string in Go language, characters must be used 反引号:

s := `第一行
第二行
第三行
...
第n行
`


fmt.Println(s)

Newlines between backticks will be treated as newlines in the string, but any escape characters will have no effect, and the text will be output as-is.

Common operations on strings

plural

 The Go language provides two types of precision complex numbers: complex64 and complex128, corresponding to float32 and float64 respectively.

The built-in complex function is used to construct complex numbers, and the built-in real and imag functions return the real and imaginary parts of complex numbers, respectively:

var c1 complex64
c1 = 1 + 2i

var c2 complex128
c2 = 2 + 3i

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

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

fmt.Println(x*y)                 
fmt.Println(real(x*y))           
fmt.Println(imag(x*y))           
fmt.Println(c1)
fmt.Println(c2)

Note :

  • Complex numbers have real and imaginary parts, the real and imaginary parts of complex64 are 32 bits, and the real and imaginary parts of complex128 are 64 bits.
  • Complex numbers can also be compared for equality using == and !=. Two complex numbers are only equal if their real and imaginary parts are equal, so care needs to be taken with precision

Guess you like

Origin blog.csdn.net/qq_62464995/article/details/129971023