English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online tools

Golang basic tutorial

Golang control statements

Golang function & method

Golang structure

Golang slice & array

Golang string (String)

Golang pointer

Golang interface

Golang concurrency

Golang exceptions (Error)

Tipos de datos del lenguaje Go

O (file operations)Data type specifies validGo variables

  1. Data types that can be saved. In Go language, types are divided into the following four categories:Basic types:

  2. Numbers, strings, and boolean values belong to this category.Aggregate types:

  3. Arrays and structures belong to this category.Reference types:

  4. 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

Number types

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 datosDescripción
    int88bit signed integer
    int1616bit signed integer
    int3232bit signed integer
    int6464bit signed integer
    uint88bit unsigned integer
    uint1616bit unsigned integer
    uint3232bit unsigned integer
    uint6464bit unsigned integer
    intin and uint both contain the same size, whether it is32bit or64bit.
    uintin and uint both contain the same size, whether it is32bit or64bit.
    runeIt is int32synonym, also representing Unicode code points.
    byteIt is int8synonym.
    uintptrIt 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 datosDescripción
    float3232bit IEEE 754floating-point numbers
    float6464bit 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 datosDescripción
    complex64Incluye float32Como números complejos de componentes real e imaginario.
    complex128Incluye 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

Tipo de datos booleano

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

Tipo de datos de cadena

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