📜  Go 中的数据类型

📅  最后修改于: 2021-10-24 13:30:00             🧑  作者: Mango

数据类型指定有效的 Go 变量可以保存的数据类型。在 Go 语言中,类型分为以下四类:

  1. 基本类型:数字、字符串和布尔值属于此类别。
  2. 聚合类型:数组和结构属于此类别。
  3. 引用类型:指针、切片、映射、函数和通道都属于这一类。
  4. 接口类型

在这里,我们将讨论 Go 语言中的基本数据类型基本数据类型进一步分为三个子类别,它们是:

  • 数字
  • 布尔值
  • 字符串

数字

在 Go 语言中,数字分为三个子类别:

  • 整数:在 Go 语言中,有符号和无符号整数都有四种不同的大小,如下表所示。有符号整数由 int 表示,无符号整数由 uint 表示。

Data Type

Description

int8 8-bit signed integer
int16 16-bit signed integer
int32 32-bit signed integer
int64 64-bit signed integer
uint8 8-bit unsigned integer
uint16 16-bit unsigned integer
uint32 32-bit unsigned integer
uint64 64-bit unsigned integer
int Both in and uint contain same size, either 32 or 64 bit.
uint Both in and uint contain same size, either 32 or 64 bit.
rune It is a synonym of int32 and also represent Unicode code points.
byte It is a synonym of int8.
uintptr It is an unsigned integer type. Its width is not defined, but its can hold all the bits of a pointer value.

例子:

Go
// Go program to illustrate
// the use of integers
package main
import "fmt"
        
func main() {
     
    // Using 8-bit unsigned int
    var X uint8 = 225
    fmt.Println(X, X-3)
     
    // Using 16-bit signed int
    var Y int16 = 32767
    fmt.Println(Y+2, Y-2)
}


Go
// Go program to illustrate
// the use of floating-point
// numbers
package main
import "fmt"
        
func main() {
    a := 20.45
    b := 34.89
     
    // Subtraction of two
    // floating-point number
    c := b-a
     
    // Display the result
    fmt.Printf("Result is: %f", c)
     
    // Display the type of c variable
    fmt.Printf("\nThe type of c is : %T", c) 
}


Go
// Go program to illustrate
// the use of complex numbers
package main
import "fmt"
 
func main() {
     
   var a complex128 = complex(6, 2)
   var b complex64 = complex(9, 2)
   fmt.Println(a)
   fmt.Println(b)
    
   // Display the type
  fmt.Printf("The type of a is %T and "+
            "the type of b is %T", a, b)
}


Go
// Go program to illustrate
// the use of booleans
package main
import "fmt"
 
func main() {
     
    // variables
   str1 := "GeeksforGeeks"
   str2:= "geeksForgeeks"
   str3:= "GeeksforGeeks"
   result1:= str1 == str2
   result2:= str1 == str3
    
   // Display the result
   fmt.Println( result1)
   fmt.Println( result2)
    
   // Display the type of
   // result1 and result2
   fmt.Printf("The type of result1 is %T and "+
                   "the type of result2 is %T",
                             result1, result2)
    
}


Go
// Go program to illustrate
// the use of strings
package main
import "fmt"
 
func main() {
     
    // str variable which stores strings
   str := "GeeksforGeeks"
    
   // Display the length of the string
   fmt.Printf("Length of the string is:%d",
                                  len(str))
    
   // Display the string
   fmt.Printf("\nString is: %s", str)
    
   // Display the type of str variable
   fmt.Printf("\nType of str is: %T", str)
}


输出:

225 222
-32767 32765
  • 浮点数:在围棋语言,浮点数被分为两类,如图于下表:
Data Type

Description

float32 32-bit IEEE 754 floating-point number
float64 64-bit IEEE 754 floating-point number

例子:

// Go program to illustrate
// the use of floating-point
// numbers
package main
import "fmt"
        
func main() {
    a := 20.45
    b := 34.89
     
    // Subtraction of two
    // floating-point number
    c := b-a
     
    // Display the result
    fmt.Printf("Result is: %f", c)
     
    // Display the type of c variable
    fmt.Printf("\nThe type of c is : %T", c) 
}

输出:

Result is: 14.440000
The type of c is : float64
  • 复数:复数分为两部分,如下表所示。 float32 和 float64 也是这些复数的一部分。内置函数从其虚部和实部创建一个复数,内置虚部和实部函数提取这些部分。
Data Type

Description

complex64 Complex numbers which contain float32 as a real and imaginary component.
complex128 Complex numbers which contain float64 as a real and imaginary component.

例子:

// Go program to illustrate
// the use of complex numbers
package main
import "fmt"
 
func main() {
     
   var a complex128 = complex(6, 2)
   var b complex64 = complex(9, 2)
   fmt.Println(a)
   fmt.Println(b)
    
   // Display the type
  fmt.Printf("The type of a is %T and "+
            "the type of b is %T", a, b)
}

输出:

(6+2i)
(9+2i)
The type of a is complex128 and the type of b is complex64

布尔值

布尔数据类型仅表示真或假的一位信息。 boolean 类型的值不会隐式或显式转换为任何其他类型。

例子:

// Go program to illustrate
// the use of booleans
package main
import "fmt"
 
func main() {
     
    // variables
   str1 := "GeeksforGeeks"
   str2:= "geeksForgeeks"
   str3:= "GeeksforGeeks"
   result1:= str1 == str2
   result2:= str1 == str3
    
   // Display the result
   fmt.Println( result1)
   fmt.Println( result2)
    
   // Display the type of
   // result1 and result2
   fmt.Printf("The type of result1 is %T and "+
                   "the type of result2 is %T",
                             result1, result2)
    
}

输出:

false
true
The type of result1 is bool and the type of result2 is bool

字符串

字符串数据类型表示 Unicode 代码点序列。或者换句话说,我们可以说一个字符串是一个不可变字节的序列,这意味着一旦创建了一个字符串,就不能更改该字符串。字符串可以包含任意数据,包括人类可读形式的零值字节。

例子:

// Go program to illustrate
// the use of strings
package main
import "fmt"
 
func main() {
     
    // str variable which stores strings
   str := "GeeksforGeeks"
    
   // Display the length of the string
   fmt.Printf("Length of the string is:%d",
                                  len(str))
    
   // Display the string
   fmt.Printf("\nString is: %s", str)
    
   // Display the type of str variable
   fmt.Printf("\nType of str is: %T", str)
}

输出:

Length of the string is:13
String is: GeeksforGeeks
Type of str is: string