Thumbnail article
Data Types in Golang

Data types are stored in a variable and have different format and uses. The following types of data types are found in Golang:

  1. Number Data Type This data type is commonly referred to as a non-decimal or non-floating point numeric data type. In general there are 2 types of data that you need to know in this category.
    • uint, data type for whole numbers (positive numbers).
    • int, data type for integers (negative and positive numbers).

    The two data types above are explained in detail based on the scope and magnitude of the value.

    The following are non-decimal numeric data types:

    • uint8 : 0 up to 255
    • uint16 : 0 up to 65535
    • uint32 : 0 up to 4294967295
    • uint64 : 0 up to 18446744073709551615
    • uint : same as uint32 or uint64 (depending on value)
    • byte : same as uint8
    • int8 : -128 up to 127
    • int16 : -32768 up to 32767
    • int32 : -2147483648 up to 2147483647
    • int64 : -9223372036854775808 up to 9223372036854775807
    • int : same as int32 or int64 (depending on value)
    • rune : same as int32

    It is recommended not to be careless in determining the data type of a variable, as much as possible the data type chosen must be adjusted to its value, because it will affect the memory allocation for the variable. Choosing the right data type will make memory usage more optimal.

  2. Numeric Decimal Data Type There are two decimal numeric data types that you need to know: float32 and float64. The difference between the two data types is in the width of the range of decimal values that can be accommodated.The number of digits displayed can be controlled using %.nf , just replace n with the desired number.Example:

    %.3f will return 3 decimal digits, %.10f will return 10 decimal digits.

  3. Boolean Data Type Boolean data type can only be used to provide 2 values, namely TRUE and FALSE. This data type is usually used for condition selection and iteration. Conditions or expressions usually use && (and) and || signs (or).Expression && (and):
    • true && true : true
    • true && false : false
    • false && true : false
    • false && false : false

    Expression || (or):

    • true || true : true
    • true || false : true
    • false || true : true
    • false || false : false
  4. String Data Type String data type is used for data in the form of text. In Golang, text values must be enclosed in double quotes (“…”). In addition to using double quotes, you can also use grave accent/backticks (`) to declare strings, this mark is to the left of number 1. Strings declared using backticks are to make all characters in them not escape, including \n, double quotes and single quotes, newlines, and so on. Everything will be detected as a string.
  5. Value nil & Zero Value Other programming languages use the term null, while go uses the term nil. nil is a value, not a data type. A variable whose value is nil means that it has an empty value.All the data types described above have a zero value (default value of the data type). This means that even if a variable is declared with no initial value, it will still have the default value. The following is a zero value for each data type:
    • Zero value of string is “” (empty string).
    • Zero value of the boolean is false.
    • Zero value of a non-decimal numeric type is 0.
    • Zero value of the decimal numeric type is 0.0.

    Zero value is different from nil. Nil is an empty value that is really empty. nil cannot be used on the above data types. There are several data types that can be set to nil, including:

    • pointer
    • function data type
    • slice
    • map
    • channel
    • empty interface or interface{}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *