DataTypes in Go Programming Language


In Golang the BasicData Types are categorized into following three different types

  1. Numbers
  2. Strings
  3. Booleans

1. Number Data Types

Number Data Types again categorized into following two

  1. Integer Data type
  2. Floating-point numbers

Integer Data type

In Go language, integer types are both signed and unsigned with four different sizes. Unsigned means it has only positive values.

  • int8
    Signed 8-bit integers (-127 to 127)
  • int16
    Signed 16-bit integers (-32768 to 32767)
  • int32
    Signed 32-bit integers (-2147483648 to 2147483647)
  • int64
    Signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
  • uint8
    UnSigned 8-bit integers (0 to 255)
  • uint16
    UnSigned 16-bit integers (0 to 65535)
  • uint32
    UnSigned 32-bit integers (0 to 4294967295)
  • uint64
    UnSigned 64-bit integers (0 to 18446744073709551615)
  • int, uint
    Both int and uint are platform dependent
  • uintptr

Example

package main
import "fmt"

func main() {
	 var age uint8 = 23
	fmt.Print("Age data type is:")
	fmt.Printf("%T\n",age)
	
	var defaultAge = 25
	fmt.Print("Age data type is:")
	fmt.Printf("%T\n",defaultAge)
	
	//declare variable with out var keyword
	cost := 100
	fmt.Print("cost data type is:")
	fmt.Printf("%T\n",cost)
	
}

output

Age data type is:uint8
Age data type is:int
cost data type is:int

Integer Type Aliases

Golang has two additional integer types

  • byte
    Alias for uint8.
  • rune
    Alias for int32.

Golang doesn’t have a char data type. It uses byte and rune to represent character values. The byte data type represents ASCII characters and the rune data type represents Unicode characters that are encoded in UTF-8 format.

Example

package main
import "fmt"

func main() {
	var myByte byte = 'a'
	var myRune rune = '♥'

//%b--base 2
//%c--the character represented by the corresponding Unicode code point
//%d--base 10
//%U--Unicode format: U+1234; same as "U+%04X"

	fmt.Printf("%c = %d and %c = %U\n", myByte, myByte, myRune, myRune)
}

You can run the above program live here https://onecompiler.com/go/3uspj8wnt

Output

a = 97 and ♥ = U+2665

Floating-point numbers

Golang has two floating point types

  1. float32
  2. float64

Default type for floating point value is float64.

Example

package main
import "fmt"

func main() {
	 appleCost := 10.45 
   var bananaCost float32 = 5.89 
      
      
    // Display the type of variables
    fmt.Printf("\nThe type of appleCost is : %T", appleCost)
    
    // Display the type of variables
    fmt.Printf("\nThe type of bananaCost is : %T", bananaCost)
}

You can run the above program live here https://onecompiler.com/go/3usyent88

Output

The type of appleCost is : float64
The type of bananaCost is : float32

2. Strings

In Golang String is nothing but sequence of bytes. Strings are declared using doubleQuotes(") or back-ticks(`).

Double-quoted strings cannot contain newlines, they can have escape characters like \n, \t etc. \n character is replaced with a newline, and \t character is replaced with a tab space etc.

Strings enclosed within back ticks can span multiple lines. Moreover, Escape characters don’t have any special meaning in raw strings.

Example

package main
import "fmt"

func main() {
	
	var welcomeMsg = "Hello Friends\n welcome to OneCompiler"
	
	var appleFeatures = `Apple taste is juicy and delicious.
	Apple colour is red\t\n`
	                     
	 fmt.Println("WelcomeMsg:",welcomeMsg)
	 fmt.Println("AppleFeatures:",appleFeatures)
}

You can run the above program live here https://onecompiler.com/go/3usykpr8p

Output


WelcomeMsg: Hello Friends
 welcome to OneCompiler
AppleFeatures: Apple taste is juicy and delicious.
	Apple colour is red\t\n

3. Booleans

Golang provides bool data type to store boolean values.

Example


package main
import "fmt"

func main() {

	var booleanTrue bool = true
	var booleanFalse = false

	var res1 = booleanTrue && booleanFalse
	var result2 = 10<20 && 5==5
	
	var result3 = 10<20 || 5!=5

	fmt.Println(res1)
	fmt.Println(result2)
	fmt.Println(result3)

}

You can run the above program live here https://onecompiler.com/go/3usymqc49

Output

false
true
true