Data Types in Go — I

Özge Büyükaşık
4 min readMar 12, 2022

Go is a programming language with various data types. That means you can declare variables or functions in different types. Go provides type safety so you can’t ignore these data types.

We can categorize data types into 4 categories:

  • Basic types,
  • Aggregate types,
  • Reference types,
  • Interface types.

Basic Types

Numbers, strings and booleans are basic types in Go language. These types are also can be called as built-in types.

Numbers

Go has 12 different numeric types: int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128.

Go provides both signed and unsigned integers. Integers have different sizes. Because of the inconsistency between CPUs, int doesn’t have a certain size. If your CPU is 64-bit, that means int is a 64-bit signed integer. Therefore you need to be carefull while performing mathematical operations between int and int32 or int64.

An alias for int32, the type rune represents a Unicode code point. Similarly, the type byte is an alias for uint8.

If you assign a value that has more bits that can be represented in the data type, it causes to overflow. When we run the code snipet above, we will get an error because we can only assign values from -128 to 127 with int8 type.

./prog.go:6:6: constant 400 overflows int8

Go build failed.

We can execute arithmetical, logical operations with binary operators. If two integers have the same type, we can also compare them with operators.

Arithmetic Operators: +, -, *, /, %, +=, -=, *=, /=, %=

Bitwise Operators: &, |, ^, &^, <<, >>

Comparison Operators: ==, !=, <, >, <=, >=

Logical Operators: &&, ||, !

Go provides two sizes of floats, float32 and float64. Go uses IEEE 754 standard to store floating point numbers and this standardd is not straightforward. Floating point numbers have a huge range in Go but they store the nearest approximation, not exact value.

You can also execute arithmetic, bitwise or logical operations between float numbers and compare two float number if both of two have same size. Be careful if you want to reach a float result, your operation must occur between two float numbers. If you divide 2 integers, you will get an integer result.

When you run code snippet, you will get:

Result is: 7
Type of result is: int32

Booleans

Boolean variables have two possible values: true or false. If and for statements contain conditions and this conditions are booleans. You can process a comparsion operation to get a boolean value. ! operator is used to get logical negation. &&(AND) and ||(OR) operators are used to combine boolean values. Go has a short circuit behavior while combining boolean values, that means if the result is already determined by the left boolean, the right boolean is ignored.

Is 3 equal or bigger than 2: true
Is 3 smaller than 2: false
Is 3 bigger than 2 and smaller than 5: true
Is 3 bigger than 4 and smaller than 8: false
Is 3 bigger than 4 or smaller than 8: true

At the code snippet above, our number is 3. We can say that 3 is equal or bigger than 2 and that means it’s not smaller than 2 with ! operator. Only one false value is enough for AND operation so at line 14 we didn’t check if number is smaller than 8. At the same time if our first value is true, we don’t need to check second value for OR operation so we skip the second operand at line 17.

Strings

Strings are immutable sequences of Unicode code points and they usually contains human-readable text. Since strings are immutable, you can reassign the value of a string variable, but you can not change a string’s data.

This code snippet returns a compile error beacuse of the line 10. You can modify a string with reassigning or concatenating with another string but don’t even try to change the byte sequence of a string.

Two strings can be compared with ==, >, < operators in lexicographic order. Beacuse of strings are sequence of bytes, you can find number of bytes in a string with len built-in function, reach i-th byte of string with index operation like string[0] or get substring from i-th index to j-th index of a string with substring operation like string[i:j].

Zero Values

When you declare a variable but not assign a value, Go assigns a zero value to that variable. Each data type has a zero value.

Name: , Age: 0, Is Graduated: false, GPA: 0.000000

Type Conversion

You can’t process any operation between two variables, if they don’t have the same data type because Go doesn’t allow automatic type promotion. When data types are not the same, you must use a type conversion.

  • Be careful when you convert an integer or float to bigger size, it can be overflowed and you can’t reach the exact value.
  • You can’t convert any other type to bool. Integer 0 isn’t false…
  • Use strconv package btween stings and numeric values. fmt.Sprintf() is another option to convert numeric values to string.

Thats’s all! Thanks for reading and if you have any feedback, please share with me :)

--

--