English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
O (file operations)Data type specifies validGo variables
Data types that can be saved. In Go language, types are divided into the following four categories:Basic types:
Numbers, strings, and boolean values belong to this category.Aggregate types:
Arrays and structures belong to this category.Reference types:
Pointer, slice, map set, function, and Channel belong to this category.
Here, we will discuss the interface types of Go languageBasic data types. InBasic data typesare further divided into three subcategories, namely:
Numbers
Booleans
Strings
In Go language, numbers are divided intothreeSubcategory:
Integer:In Go language, both signed and unsigned integers can be used in four different sizes, as shown in the table below. The signed int is represented by int, and the unsigned integer is represented by uint.
Tipo de datos | Descripción |
---|---|
int8 | 8bit signed integer |
int16 | 16bit signed integer |
int32 | 32bit signed integer |
int64 | 64bit signed integer |
uint8 | 8bit unsigned integer |
uint16 | 16bit unsigned integer |
uint32 | 32bit unsigned integer |
uint64 | 64bit unsigned integer |
int | in and uint both contain the same size, whether it is32bit or64bit. |
uint | in and uint both contain the same size, whether it is32bit or64bit. |
rune | It is int32synonym, also representing Unicode code points. |
byte | It is int8synonym. |
uintptr | It is an unsigned integer type. Its width is undefined, but it can accommodate all bits of a pointer value. |
// using integers package main import "fmt" func main() { // using8bit unsigned integer var X uint8 = 225 fmt.Println(X+1, X) // using16bit signed integer var Y int16 = 32767 fmt.Println(Y+2, Y-2) }
Salida:
226 225 -32767 32765
Floating-point numbers:In Go language, floating-point numbers are divided into2as shown in the following table:
Tipo de datos | Descripción |
---|---|
float32 | 32bit IEEE 754floating-point numbers |
float64 | 64bit IEEE 754floating-point numbers |
// the use of floating-point numbers package main import "fmt" func main() { a := 20.45 b := 34.89 //two floating-point numbers subtract c := b-a //show result fmt.Printf("result: %f", c) //Mostrar el tipo de la variable c fmt.Printf("\nc es: %T", c) }
Salida:
Resultado: 14.440000 El tipo de c es: float64
Número complejo:Dividen el número complejo en dos partes, como se muestra en la tabla a continuación. float32y float64También son parte de estos números complejos. Las funciones integradas crean un número complejo a partir de sus partes imaginarias y reales, y las funciones integradas de parte imaginaria y real extraen estas partes.
Tipo de datos | Descripción |
---|---|
complex64 | Incluye float32Como números complejos de componentes real e imaginario. |
complex128 | Incluye float64Como números complejos de componentes real e imaginario. |
//Uso de números complejos package main import "fmt" func main() { var a complex128 = complex(6, 2) var b complex64 = complex(9, 2) fmt.Println(a) fmt.Println(b) //Mostrar tipo fmt.Printf("El tipo de a es %T y"+ "El tipo de b es %T", a, b) }
Salida:
(6+2i) (9+2i) El tipo de a es complex128 y el tipo de b es complex64
El tipo de datos booleano solo representa true o false. Los valores del tipo booleano no se convierten implícitamente ni explícitamente en ningún otro tipo.
//Uso de valores booleanos package main import "fmt" func main() { //Variable str1 := "w3codebox" str2 := "w3codebox" str3 := "w3codebox" result1 := str1 == str2 result2 := str1 == str3 //Imprimir el resultado fmt.Println(result1) fmt.Println(result2) //Mostrar result1yresult2El tipo fmt.Printf("result1 El tipo es %T , "+"result2El tipo es %T", result1, result2) }
Salida:
true true result1 El tipo es bool , result2El tipo es bool
El tipo de datos de cadena representa una secuencia de puntos de código Unicode. En otras palabras, podemos decir que una cadena es una secuencia inmutable de bytes, lo que significa que una vez creada una cadena, no se puede cambiar. Las cadenas pueden contener cualquier tipo de datos, incluyendo bytes con valores nulos en forma legible por humanos.
//Uso de cadena package main import "fmt" func main() { //Usado para almacenar la cadena str str := "w3codebox" //Mostrar la longitud de la cadena fmt.Printf("La longitud de la cadena:%d", len(str)) //Mostrar la cadena fmt.Printf("\nLa cadena es: %s", str) // Mostrar el tipo de la variable str fmt.Printf("\nEl tipo de str es: %T", str) }
Salida:
La longitud de la cadena:5 La cadena es: w3codebox El tipo de str es: string