Type is introduced

Numeric types

Numeric types include

  • The integer
  • Floating point Numbers
  • The plural

The integer

Each type contains the type of its corresponding size. For example, the integer type is int8, int16, INT32, int64, etc

Int8 – “unit8” int16- “unit16” int32- “unit32” int64- “unit64”

How many bits are 8, 16, 32, and 64?

There is also an unsigned integer type uintptr that does not specify a specific bit size but is large enough to hold Pointers. The UintPtr type is only needed for low-level programming, especially where the Go language interacts with C libraries or OS interfaces.

Floating point Numbers

Float32, float64

The Printf function allows you to use “%f” to control how many decimal places to keep when printing floating-point numbers

FMT. Printf (" % 2 f \ n ", math.h Pi) 3.14Copy the code

The plural

Complex128 (64-bit real and imaginary) and Complex64 (32-bit real and imaginary)

A complex number is represented by two floating point numbers

var name complex128 = complex(x, y)
var x complex128 = complex(1.2) // 1+2i
Copy the code

The first represents a real number, the second an imaginary number

You can also use == and! = two complex numbers are equal only if their real and imaginary parts are equal.

Boolean type

There are only true and false types of results

Take the reverse use! Unary operator

&& and | | usage consistent with Java

String type

A string is a fixed-length array of bytes

Escape character

  • \n: newline character
  • \r: carriage return character
  • \ t: the TAB key
  • \u or \u: Unicode character
  • \ : backslash itself

The general comparison operators (==,! =, <, <=, >=, >) implements string comparison by comparing bytes in memory

methods

The length of a string in bytes can be obtained by the function len(), such as len(STR).

The contents of a string (pure bytes) can be obtained by using the standard index method. The index is written inside the square brackets []. The index counts from 0: STR [0] STR [i-1] STR [i-1] last byte: STR [len(STR)-1]

Concatenation of strings using +

Multiline string

Use ‘includes living

const str = 'First line second line third line \r\n'
Copy the code

Other String Methods

Learn about web sites: String methods

Character types

There are two types of characters in Go: the Uint8 type, or byte, represents one character of the ASCII code. The other is the RUNe type, which represents a UTF-8 character and is needed when you need to deal with Chinese, Japanese, or other compound characters. The rune type is equivalent to the INT32 type.

Equivalent:var ch byte = 65var ch byte = '\x41'      // (\x always follows a hexadecimal number of length 2)
Copy the code

Type conversion

The GO language must explicitly declare type conversions

valueOfTypeB = typeB(valueOfTypeA)

Such as

a := 5.0
b := int(a)
Copy the code

interface

/* Define an interface */ type interface_name interface {method_name1 [return_type] method_name2 [return_type] method_name3 [return_type] ... Method_namen [return_type]} /* Define struct */ type struct_name struct {/* variables */} /* Implement interface methods */ func (struct_name_variable struct_name) method_name1() [return_type] {/* method implementation */}... Func (struct_name_variable struct_name) method_namen() [return_type] {/* method implementation */}Copy the code
package main
import (
    "fmt"
)
type Phone interface {
    call()
}
type NokiaPhone struct{}func (nokiaPhone NokiaPhone) call(a) {
    fmt.Println("I am Nokia, I can call you!")}type IPhone struct{}func (iPhone IPhone) call(a) {
    fmt.Println("I am iPhone, I can call you!")}func main(a) {
    var phone Phone

    phone = new(NokiaPhone)
    phone.call()

    phone = new(IPhone)
    phone.call()

}
Copy the code

additional

How does UTF-8 relate to Unicode

Unicode is a character set similar to ASCLL

Utf-8 is an encoding rule. Each Chinese character in UTF-8 takes three bytes