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

Online Tools

Golang Basic Tutorial

Golang Control Statements

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String(String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception(Error)

Go Language Data Type

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 use four different sizes, as shown in the table below. The signed int is represented by int, and the unsigned integer is represented by uint.

    Data typeDescription
    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 indicating 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() { 
          
        // used8bit unsigned integer
        var X uint8 = 225 
        fmt.Println(X+1, X) 
          
        // used16bit signed integer
        var Y int16 = 32767 
        fmt.Println(Y+2, Y-2)  
    }

    Output:

    226 225
    -32767 32765
  • Floating-point numbers:In Go language, floating-point numbers are divided into2As shown in the following table:

    Data typeDescription
    float3232bit IEEE 754Floating-point numbers
    float6464bit IEEE 754Floating-point numbers
    // Usage of floating-point numbers 
    package main  
    import "fmt"
             
    func main() { 
        a := 20.45 
        b := 34.89 
          
        //Subtraction of two floating-point numbers
        c := b-a 
          
        //Display the result 
        fmt.Printf("Result: %f", c) 
          
        //Display the type of c variable
        fmt.Printf("\nThe type of c is : %T", c)   
    }

    Output:

    Result: 14.440000
    The type of c is: float64
  • Complex numbers:Divide complex numbers into two parts, as shown in the table below. float32and float64are also part of these complex numbers. Built-in functions create a complex number from its imaginary and real parts, and built-in imaginary and real functions extract these parts.

    Data typeDescription
    complex64Containing float32As complex numbers consisting of real and imaginary components.
    complex128Containing float64As complex numbers consisting of real and imaginary components.
    //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 type 
      fmt.Printf("The type of a is %T as well as"+ "The type of b is %T", a, b) 
    }

    Output:

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

Boolean type

The boolean data type only represents true or false. The values of the boolean type do not implicitly or explicitly convert to any other type.

//Use of boolean values
package main
import "fmt"
func main() {
    //variable
    str1 := "w3codebox"
    str2 := "w3codebox"
    str3 := "w3codebox"
    result1 := str1 == str2
    result2 := str1 == str3
    //Print result
    fmt.Println(result1)
    fmt.Println(result2)
    //Display result1and result2The type
    fmt.Printf("result1 The type is %T , "+"result2The type is %T", result1, result2)
}

Output:

true
true
result1 The type is bool , result2The type is bool

String type

The string data type represents a sequence of Unicode code points. In other words, we can say that a string is an immutable byte sequence, which means that once a string is created, you cannot change the string. Strings can contain any data, including bytes that represent zero values in human-readable form.

//Using string
package main 
import "fmt"
  
func main() { 
      
    //The str variable is used to store the string
   str := "w3codebox"
     
   //Display the length of the string
   fmt.Printf("The length of the string:%d", len(str)) 
     
   //Display the string 
   fmt.Printf("\nThe string is: %s", str) 
     
   // Display the type of str variable
   fmt.Printf("\nThe type of str is: %T", str) 
}

Output:

The length of the string:5
The string is: w3codebox
The type of str is: string