Types in Go 
Tags: go, webdev, programming
Published: 2017-11-22 02:36:15

---
title: Types in Go 
published: true
description: In this part of the Golang series, we look at the basic types available in Go.
tags: go,golang,webdev,programming
---

In the [previous article](https://dev.to/codehakase/getting-started-with-go---variables-aef), I wrote about variables and how they are used in Go. In this part, we're going to look at the Basic types available in Go.

Go is a statically typed language. This means that variables always have a specific type and that type cannot change. Static typing may seem cumbersome at first. You'll spend a large amount of your time just trying to fix your program so that it finally compiles.

The following are the basic types available in 

- Bool
- String
- Numeric Types
  - int8, int16, int32, int64, int
  - uint8, uint16, uint32, uint64, uint
  - float32, float64
  - complex64, complex128
  - byte
  - rune

### Bool
A boolean value, is a special 1 bit integer type used to represent true and false (or on and off). Three logical operators are used with boolean values:
- && - and
- || - or
- !  - not

Here is an example program showing how they can be used:

```go
package main

import "fmt"

func main() {
  fmt.Println(true && true)
  fmt.Println(true && false)
  fmt.Println(true || true)
  fmt.Println(!false)
}
```

Running this program should give you:

```bash
$ go run main.go
true
false
true
true
```

### Strings
A string is a sequence of characters with a definite length used to represent text. Go strings are made up of individual bytes, usually one for each character.

```go
package main

import "fmt"

func main() {

  fmt.Println("Hello World \n")
  
  fmt.Println("Golang rocks!")
}
```

String literals can be created using double quotes "Hello World" or back ticks ` `Hello World` `. The difference between these is that double quoted strings cannot contain newlines and they allow special escape sequences. For example `\n` gets replaced with a newline and `\t` gets replaced with a tab character.

### Integers
Integers, like their mathematical counterpart, are numbers without a decimal component. (-3, -2, -1, 0, 1, …). Unlike the base-10 decimal system we use to represent numbers, computers use a base-2 binary system.

```go
package main 

import "fmt"

func main() {
  
  fmt.Println("10 + 7 = ", 10 + 7)
}
```
```bash
$ go run main.go
10 + 7 = 17
```

### Floating-point Numbers
Go has two floating point types: **float32** and **float64** (also often referred to as single precision and double precision respectively) as well as two additional types for representing complex numbers (numbers with imaginary parts): **complex64** and **complex128**.

The following is a simple program to illustrate integer and floating point types.

```go
package main

import (  
    "fmt"
)

func main() {  
    x, y := 2.12, 5.4

    sum := x + y
    diff := x - y

    fmt.Println("the sum: ", sum, "diff: ", diff)
}

```

Running the above you should see the following:
```bash
$ go run main.go
the sum:  7.5200000000000005  diff:  -3.2800000000000002

```

### Complex Numbers

**complex64:** complex numbers which have float32 real and imaginary parts
**complex128:** complex numbers with float64 real and imaginary parts

The built-in function complex is used to construct a complex number with real and imaginary parts.

> It takes a real and imaginary part as parameter and returns a complex type. Both the real and imaginary parts should be of the same type. ie either float32 or float64. If both the real and imaginary parts are float32, this function returns a complex value of type *complex64*. If both the real and imaginary parts are of type float64, this function returns a complex value of type *complex128*.

```go
package main

import "fmt"

func main() {
  c1 := complex(2, 3)
  c2 := 2 + 47i

  cadd := c1 + c2

  fmt.Println("sum:", cadd)
 
  cmul := c1 * c2
  fmt.Println("product:", cmul)
}
```

```bash
$ go run main.go
sum: (4+50i)
product: (-137+100i)
```

## Conclusion
In this part, we saw the basic types in Go. In the next parts of the series, we'd be using these various types interchangeably. In the next part, we'd look at Constants in Go.

I may have missed something, do me a favour and report me in the comments below or fire me a tweet [@codehakase](https://twitter.com/codehakase).

PS: This is the third part of the series, you refer to the [first part](https://dev.to/codehakase/golang---getting-started-16c) to get a hint of the series.

Cheers!