2. Go language has built-in basic types

Boolean type

In Go, the type of Boolean value is bool , the value is true or false, and the default is false .

// 示例代码
var isActive bool //  全局变量声明
var enabled, disabled = true, false //  忽略类型的声明
func test() {
    
    
    var available bool //  一般声明
    valid := false //  简短声明
    available = true //  赋值操作
}

Numeric type

There are two types of integers: unsigned and signed. Go supports both int and uint. Both types have the same length, but the specific length depends on the implementation of different compilers. The current gcc and gccgo compilers use 32 bits for int and uint on both 32-bit and 64-bit platforms, but this may increase to 64-bit on 64-bit platforms in the future. There are also types in Go that directly define the number of digits: rune, int8, int16, int32, int64 and byte, uint8, uint16, uint32, uint64. Among them, rune is an alias for int32, and byte is an alias for uint8 .

One thing to note is that these types of variables are not allowed to assign values ​​to or operate on each other, otherwise the compiler will report an error during compilation.
The following code will generate an error:

var a int8
var b int32
c:=a + b

In addition, although the length of int is 32 bits, int and int32 are not interchangeable.

There are two types of floating point numbers: float32 and float64 (there is no float type), and the default is float64 .

Go also supports plurals. Its default type is complex128 (64-bit real + 64-bit imaginary). If you need something smaller,
there is also complex64 (32-bit real + 32-bit imaginary). The form of a complex number is RE + IMi, where RE is the real part, IM is the imaginary part, and the last i is the imaginary unit. Here is an example using plural numbers:

var c complex64 = 5+5i
//output: (5+5i)
fmt.Printf("Value is: %v", c)

string

Strings in Go are encoded using the UTF-8 character set. A string is defined by a pair of double quotes ("") or backticks ()
, and its type is string.

// 示例代码
var frenchHello string //  声明变量为字符串的一般方法
var emptyString string = "" //  声明了一个字符串变量,初始化为空字符串
func test() {
    
    
    no, yes, maybe := "no", "yes", "maybe" //  简短声明,同时声明多个变量
    japaneseHello := "Ohaiou" //  同上
    frenchHello = "Bonjour" //  常规赋值
}

Strings are immutable in Go. For example, the following code will report an error when compiled:

var s string = "hello"
s[0] = 'c'

But what if you really want to modify it? The following code can achieve this:

s := "hello"
c := []byte(s) //  将字符串 s  转换为 []byte  类型
c[0] = 'c'
s2 := string(c) //  再转换回 string  类型
fmt.Printf("%s\n", s2)

You can use the + operator in Go to concatenate two strings:

s := "hello,"
m := " world"
a := s + m
fmt.Printf("%s\n", a)

Modifying the string can also be written as:

s := "hello"
s = "c" + s[1:] //  字符串虽不能更改,但可进行切片操作
fmt.Printf("%s\n", s)

What if you want to declare a multi-line string? This can be declared using backticks :

m := `hello
	world`

The string enclosed in backticks is a Raw string , that is, the form of the string in the code is the form when printed. It does not have character escapes, and newlines will be output as is.

Error type

Go has a built-in error type, which is specially used to handle error messages. There is also a package errors in Go's package to handle errors:

err := errors.New("emit macho dwarf: elf header corrupted")
if err != nil {
    
    
	fmt.Print(err)
}

Guess you like

Origin blog.csdn.net/u012534326/article/details/119986165