Go中的数据类型介绍和用法指南

2021年3月24日14:31:42 发表评论 1,092 次浏览

数据类型指定有效的数据类型变数能把持住。在Go语言中, 类型分为以下四类:

  1. 基本类型:数字, 字符串和布尔值属于此类别。
  2. 集合类型:数组和结构属于此类别。
  3. 引用类型:指针, 切片, 哈希表, 函数和管道属于此类别。
  4. 接口类型

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

  • 数字
  • 布尔值
  • 字符串

数字

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

整数:

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

数据类型 描述
int8 8位有符号整数
int16 16位有符号整数
int32 32位有符号整数
int64 64位有符号整数
uint8 8位无符号整数
uint16 16位无符号整数
uint32 32位无符号整数
uint64 64位无符号整数
int in和uint都包含相同的大小, 无论是32位还是64位。
uint in和uint都包含相同的大小, 无论是32位还是64位。
rune 它是int32的同义词, 还表示Unicode代码点。
byte 它是int8的同义词。
uintptr 它是无符号整数类型。它的宽度没有定义, 但是可以保存指针值的所有位。

例子:

// 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+1, X)
      
     // Using 16-bit signed int 
     var Y int16 = 32767
     fmt.Println(Y+2, Y-2) 
}

输出如下:

226 225
-32767 32765

浮点数:在Go语言中,浮点数分为两类,如下表所示:

数据类型 描述
float32 32位IEEE 754浮点数
float64 64位IEEE 754浮点数

例子:

// 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也是这些复数的一部分。内置函数根据其虚部和实部创建复数, 而内置虚部和实函数则提取这些虚部。

数据类型 描述
complex64 包含float32作为实数和虚数分量的复数。
complex128 包含float64作为实数和虚数分量的复数。

例子:

// 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

布尔值

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

例子:

// Go program to illustrate
// the use of booleans
package main
import "fmt"
  
func main() {
      
     // variables
    str1 := "lsbin"
    str2:= "lsbin"
    str3:= "lsbin"
    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 := "lsbin"
     
    // 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: lsbin
Type of str is: string

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: