Golang 数据类型

Golang 数据类型

数据类型指定了一个有效的Go变量所能容纳的数据类型。在Go语言中,数据类型被分为以下四类。

  1. 基本类型: 数字、字符串和布尔都属于这个类别。
  2. 聚合类型: 数组和结构属于这一类别。
  3. 参考类型: 指针、分片、地图、函数和通道都属于这一类。
  4. 接口类型

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

  • 数字
  • 布尔
  • 字符串

数字

在Go语言中,数字被分为三个子类别,分别是。

  • 整数: 在Go语言中,有符号和无符号的整数都有四种不同大小,如下表所示。有符号整数用int表示,无符号整数用uint表示。
数据类型 描述
int8 8位有符号整数
int16 16位有符号整数
int32 32位有符号整数
int64 64位有符号整数
uint8 8位无符号整数
uint16 16位无符号整数
uint32 32位无符号整数
uint64 64位无符号整数
int int和uint都包含相同的大小,可以是32位或64位。
uint int和uint都包含相同的大小,可以是32位或64位。
符文 它是int32的同义词,也代表Unicode代码点。
字节 它是uint8的同义词。
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, X-3)
     
    // Using 16-bit signed int
    var Y int16 = 32767
    fmt.Println(Y+2, Y-2)
}

输出

225 222
-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 := "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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程