What Is Data Type in Golang?

//

Larry Thompson

What Is Data Type in Golang?

In the Go programming language (Golang), data types are crucial components that define the kind of values a variable can store. When you create a variable, you must specify its data type, which helps the compiler allocate memory and perform operations on that variable.

Basic Data Types

Golang provides several basic data types:

  • bool: Represents boolean values, either true or false.
  • string: Stores textual data enclosed within double quotes (“example”).
  • int: Represents signed integers with a predefined size, such as -42.
  • float64: Stores floating-point numbers with double precision, such as -3.14.

Different Integer Types

In addition to the basic int type, Golang offers different integer types with varying sizes:

  • int8: Represents signed integers with a size of 8 bits (-128 to 127).
  • int16: Represents signed integers with a size of 16 bits (-32,768 to 32,767).
  • int32: Represents signed integers with a size of 32 bits (-2,147,483,648 to 2,147,483,647).
  • int64: Represents signed integers with a size of 64 bits (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).

Unsigned Integers

Golang also provides unsigned integer types:

  • uint8: Represents unsigned integers with a size of 8 bits (0 to 255).
  • uint16: Represents unsigned integers with a size of 16 bits (0 to 65,535).
  • uint32: Represents unsigned integers with a size of 32 bits (0 to 4,294,967,295).
  • uint64: Represents unsigned integers with a size of 64 bits (0 to 18,446,744,073,709,551,615).

Numeric Types

Besides integers and floating-point numbers, Golang provides other numeric types:

  • byte: Alias for uint8; it represents ASCII characters.
  • rune: Alias for int32; it represents Unicode code points.
  • complex64: Represents complex numbers with float32 real and imaginary parts.
  • complex128: Represents complex numbers with float64 real and imaginary parts.

Type Inference

Golang supports type inference. When you declare a variable without explicitly specifying its data type using the colon-equals (:=) operator,
the compiler automatically determines the appropriate data type based on the assigned value. For example:

var message = "Hello World!" // The compiler infers that message is of type string.

Custom Data Types

In addition to the built-in data types, Golang allows you to create custom data types using the type keyword. Custom data types can be useful for enhancing code readability and creating more expressive abstractions. For example:

type Age int

func main() {
  var myAge Age = 27
}

In this example, Age is a custom data type that is an alias for the int type. We declare a variable myAge of type Age, which can only store integer values.

In Conclusion

Data types in Golang are essential for defining variables and specifying the kind of values they can hold. By understanding and utilizing different data types effectively, you can write robust and efficient code in Golang.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy