Data Types in Go

Data types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows:

  1. Basic type: Numbers, strings, and booleans come under this category.
  2. Aggregate type: Array and structs come under this category.
  3. Reference type: Pointers, slices, maps, functions, and channels come under this category.
  4. Interface type

Here, we will discuss Basic Data Types in the Go language. The Basic Data Types are further categorized into three subcategories which are:

  • Numbers
  • Booleans
  • Strings

Numbers

In Go language, numbers are divided into three sub-categories that are:

  • Integers: In Go language, both signed and unsigned integers are available in four different sizes as shown in the below table. The signed int is represented by int and unsigned integer is represented by uint.
    Data Type Description
    int8 8-bit signed integer
    int16 16-bit signed integer
    int32 32-bit signed integer
    int64 64-bit signed integer
    uint8 8-bit unsigned integer
    uint16 16-bit unsigned integer
    uint32 32-bit unsigned integer
    uint64 64-bit unsigned integer
    int Both in and uint contain same size, either 32 or 64 bit.
    uint Both in and uint contain same size, either 32 or 64 bit.
    rune It is a synonym of int32 and also represent Unicode code points.
    byte It is a synonym of int8 .
    uintptr It is an unsigned integer type. Its width is not defined, but its can hold all the bits of a pointer value.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    226 225
    -32767 32765
    
  • Floating-Point Numbers: In Go language, floating-point numbers are divided into two categories as shown in the below table:
    Data Type Description
    float32 32-bit IEEE 754 floating-point number
    float64 64-bit IEEE 754 floating-point number

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Result is: 14.440000
    The type of c is : float64
    
  • Complex Numbers: The complex numbers are divided into two parts are shown in the below table. float32 and float64 are also the part of these complex numbers. The in-built function creates the complex number from its imaginary and real part and in-built imaginary and real function extract those parts.
    Data Type Description
    complex64 Complex numbers which contain float32 as a real and imaginary component.
    complex128 Complex numbers which contain float64 as a real and imaginary component.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

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

Booleans

The boolean data type represents only one bit of information either true or false. The values of type boolean are not converted implicitly or explicitly to any other type.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

false
true
The type of result1 is bool and the type of result2 is bool

Strings

String data type represents a sequence of Unicode code points. Or in other words, we can say a string is a sequence of immutable bytes, means once a string is created you cannot change that string. A string may contain arbitrary data, including bytes with zero value in the human-readable form.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Length of the string is:13
String is: GeeksforGeeks
Type of str is: string


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.