[Golang] Basic Data Types

Taha Mazari
4 min readMar 26, 2022

--

The Go gopher

Golang is a statically typed programming language developed at Google. With a syntax and efficiency similar to C, and readability and usability similar to Python and JavaScript, it is currently amongst one of the most easiest language you can learn.

Basic Data types

In Go, there are 3 basic data types that are strings, numbers and booleans.
Remember, once a variable is typed, it can never in the future store data of another type.

Declaring Variables

Declaring variables is super easy in Go. Since it is a statically typed language, ideally you will need to declare variables along with their types as well.

In the code following below, we declare a variable using the var keyword along with its name and data type i.e. in this case a string.

Declaring strings in Go

For the case of number (integer) and boolean types, it is quiet simple as well,

String, number and boolean respectively

Note, as a developer you should be aware of signed and unsigned integer values. Signed integers represent negative and positive values including 0. While unsigned integers represent positive integers along with 0.

Moreover, when it comes to integers, Go also provides storing integers of different sizes e.g. -128 to 127 for int8. Likewise we have integers for sizes int16, int32 and int64. We can also use unsigned integers along with their size that would translate as uint8, uint16, uint32 and uint64. The diagram explains this,

Integers and their different sizes

Floats and complex numbers can also be typed using the following,

Floats and Complex numbers

Constant Variables

Go also provides support for having constant variables. Which obviously can not be redeclared. Doing so would give you an error. You can use constants for storing data that you know certainly will never be suspect to change in future e.g. URL’s.

Constant Variables

Explicit vs Implicit data Types

You can also define variables in an implicit manner by letting the Go interpreter decide the type of the variable itself. In the figure below, we are not explicitly declaring the types of the variables, so the interpreter, based on the value stored in that variable, would determine its type.

Explicit vs Implicit Variables

Expression Assignment Operator

There is also another way to declare variables, basically it’s a short hand way or you could say syntactic sugar. This is called the expression assignment operator in Go.

You can simply do this by using := (so no need for writing var before variable and assigning its type).

Expression Assignment Operator

Default Values

In Go, if you just declare a variable and don't assign a value to it, it will automatically give it a default value. It is 0, empty string and false for integers, strings and booleans respectively.

Default Values

This is all for now, I hope you liked the post. I am learning Go these days, and everything I learn, it will keep on getting documented on Medium. Hope to continue uploading new content every week here, so please follow me here and on LinkedIn as well.

--

--