Basic datatypes in Golang

Basic datatypes in Golang

Integer Types

  • int, int8, int16, int32, int64 are signed integer types.

    • int is a platform-dependent signed integer (either 32 or 64 bits).
    • int8 is an 8-bit signed integer.
    • int16 is a 16-bit signed integer.
    • int32 is a 32-bit signed integer.
    • int64 is a 64-bit signed integer.
  • uint, uint8, uint16, uint32, uint64 are unsigned integer types.

    • uint is a platform-dependent unsigned integer (either 32 or 64 bits).
    • uint8 is an 8-bit unsigned integer.
    • uint16 is a 16-bit unsigned integer.
    • uint32 is a 32-bit unsigned integer.
    • uint64 is a 64-bit unsigned integer. uintptr is an integer type that is large enough to hold the bit pattern of any pointer.

Floating Point Types

  • float32 is a single-precision floating-point number.
  • float64 is a double-precision floating-point number.
Example
var a float32 = 3.14
var b float64 = 3.141592653589793

fmt.Println(a) // Output: 3.14
fmt.Println(b) // Output: 3.141592653589793

Boolean Type

  • bool - Boolean Type
Example
var isLoading bool = true
fmt.Println(isLoading)  // Output: true
isLoading = false
fmt.Println(isLoading) // Output: false

String Type

  • string - String Type
Example
var greeting string = "Hello, World!"
fmt.Println(greeting) // Output: Hello, World!

Byte Type

  • byte- alias for uint8
  • rune - alias for int32 (represents a Unicode code point)
Example
var b byte = 'a' // byte is an alias for uint8
var r rune = '世' // rune is an alias for int32 and represents a Unicode code point

fmt.Printf("%c\n", b) // Output: a
fmt.Printf("%c\n", r) // Output: 世

Complex Types

  • complex64 is a complex number with float32 real and imaginary parts.
  • complex128 is a complex number with float64 real and imaginary parts.
Example
var a complex64 = 1 + 2i
var b complex128 = 2 + 3i

fmt.Println(a) // Output: (1+2i)
fmt.Println(b) // Output: (2+3i)
Last updated on