GO Data Types

GO Data Types

Simple data types

ยท

3 min read

In my previous post I wrote about why I'm learning Go, I said that I like it (that's the case so far), and I explained how to install it and write our first program. I will now go over variables, constants and simple data types.

Simple data types

As you already guessed, Go is a statically typed language. This means that each variable has a data type and it cannot change during the execution of the program.

Simple data types in Go refer to the ones that can contain only one value simultaneously. Go has different data types, but they can be grouped in four categories: strings, numbers, booleans and errors. I will explain them.

Strings

Strings are a chain of characters (text basically). There are two types of strings in Go.

"This is an interpreted string" // special keywords will affect
`this is a raw string` // it's taken literally as is

// some examples
interpreted := "I'm going to create a new line \n now"
raw := `It doesn't take \n into consideration`
println(interpreted)
println(raw)

raw = `Now I'm going to create a new line
using raw strings.
Look at me...`
println(raw)

Place this code inside of the main function to see what happens and you'll understand the difference. I will later explain how to create variables.

Numbers

There are different data types considered as numbers. They are all described here. I will try to summarize them.

Integer

  • int8: signed 8-bit integers (-128 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)

  • rune: alias for int32

Unsigned integer

  • 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)

  • byte: alias for uint8

Float

  • float32: 32-bit floating-point numbers

  • float64: 64-bit floating-point numbers

Complex

  • complex64: complex numbers with float32 real and imaginary parts

  • complex128: complex numbers with float64 real and imaginary parts

Booleans

It's a data type that allows storing True or False as a value. There isn't a lot to explain about this. I'll add some examples later.

Errors

As opposed to the other data types where you have a list of types that considered as numeric, for instance, the error data type is just an interface that contains a method called Error(). When you use this interface, you have to implement that method. It will also return nil when no error occurred.

This is what the interface looks like:

type error interface {
    Error() string
}

You can learn more about errors here.

This is also a very practical guide to them.

Final words

Now that we have a grasp on the different simple data types we can use, we can start creating variables. I'll explain that in my next post. Stay tuned.

ย