What are the basic types in Go or Golang?
Tags: go
Published: 2022-08-15 20:34:54

---
  title: What are the basic types in Go or Golang?
  published: true
  tags: Go
  date: Tue Aug 16 2022 02:04:54 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/basic-types-go-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-5899876d7ef72361f61ff18182c550a4.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/basic-types-go-golang)
  
There are a total of `19` basic types in Go or Golang. Those are mentioned below:

- [`bool`](#bool-type)
- [`string`](#string-type)
- [`int`](#int-type)
- [`int8`](#int-type)
- [`int16`](#int-type)
- [`int32`](#int-type)
- [`int64`](#int-type)
- [`uint`](#uint-type)
- [`uint8`](#uint-type)
- [`uint16`](#uint-type)
- [`uint32`](#uint-type)
- [`uint64`](#uint-type)
- [`uintptr`](#uintptr-type)
- [`byte`](#byte-type)
- [`rune`](#rune-type)
- [`float32`](#float-type)
- [`float64`](#float-type)
- [`complex64`](#complex-type)
- [`complex128`](#complex-type)

### <a name="bool-type"></a> `bool` type

The `bool` type is used for holding truthy/falsy values such as `true` or `false`.

```go
var isAdmin bool // isAdmin variable has a type of `bool`
isAdmin = true
```

### <a name="string-type"></a> `string` type

The `string` type is used to hold a sequence of characters. The characters are enclosed in double quotation marks symbol (`""`).

```go
var name string // name variable has a type of `string`
name = "John Doe"
```

### <a name="int-type"></a> `int`, `int8`, `int16`, `int32`, and `int64` type

The `int` types are used to hold numerical values including the negative and non-negative numbers.

The numbers after the word `int` refer to the size. For example, the `int8` type can hold values of `8-bit` size.

Each of the `int` types can have negative and non-negative numbers up to a certain range. These are mentioned below:

- The `int8` type can have numbers ranging from `-128` to `127`.
- The `int16` type can have numbers ranging from `-32768` to `32767`.
- The `int32` type can have numbers ranging from `-2147483648` to `2147483647`.
- The `int64` type can have numbers ranging from `-2^63` (-2 to the power of 63) to `2^63 - 1` (2 to the power of 63 minus 1).
- The `int` type is a platform-dependent type which means that on a `32` bit system it will be the same as the `int32` type and on a `64` bit system it will be the same as the `int64` type.

```go
var num int8 // num variable has a type of `int8`
num = -128
```

### <a name="uint-type"></a> `uint`, `uint8`, `uint16`, `uint32`, and `uint64` type

The `uint` types are used to hold numerical values for non-negative numbers. It is also called as unsigned integer type.

The numbers after the word `int` refer to the size. For example, the `uint8` type can hold values of `8-bit` size.

Each of the `uint` types can have non-negative numbers up to a certain range. These are mentioned below:

- The `uint8` type can have numbers ranging from `0` to `255`.
- The `uint16` type can have numbers ranging from `0` to `65535`.
- The `uint32` type can have numbers ranging from `0` to `4294967295`.
- The `uint64` type can have numbers ranging from `0` to `2^64 - 1` (2 to the power of 64 minus 1).
- The `uint` type is a platform-dependent type which means that on a `32` bit system it will be the same as the `uint32` type and on a `64` bit system it will be the same as the `uint64` type.

```go
var num uint8 // num variable has a type of `uint8`
num = 255
```

### <a name="uintptr-type"></a> `uintptr` type

You may never have to use the `uintptr` type unless you are developing code related to Go runtime libraries. It is used to bypass the Go's type system and can be used as the reference of memory locations of `C` or other system-level programming languages codes.

### <a name="byte-type"></a> `byte` type

The `byte` is an alias for the `uint8` type.

### <a name="rune-type"></a> `rune` type

The `rune` is an alias for the `int32` type.

One cool thing with the `rune` type is that if you assign a Unicode symbol like `😃` (Smiling Face with Open Mouth) to the variable, then it will be automatically converted into its hexadecimal Unicode codepoint. The hexadecimal is essentially an integer.

```go
var favEmoji rune
favEmoji = '😃'
fmt.Println(favEmoji) // 128515
```

### <a name="float-type"></a> `float32` and `float64` type

The `float32` and `float64` type is used to hold numerical values having a decimal or fractional part.

- The `float32` type has a `32` bit size and has single precision.
- The `float64` type has a `64` bit size and has double precision.

```go
floatNum := 78.65 // floatNum variable has the type of float64
```

### <a name="complex-type"></a> `complex64` and `complex128` type

Go can easily handle complex numbers also with the `complex64` and `complex128` types.

- The `complex64` type has a real part composed of the `float32` type and an imaginary part composed of the `float32` type.
- The `complex128` type has a real part composed of the `float64` type and an imaginary part composed of the `float64` type.

It can be done like this,

```go
complexNum := 5 + 8i // complexNum variable type is complex128
```

Or you can use the built-in `complex()` function to create the real and imaginary parts of a complex number like this,

```go
complexNum := complex(5, 8) // complexNum variable type is complex128
fmt.println(complexNum) // 5 + 8i
```

These are the available basic types in Golang.

That's all 😃!

### Feel free to share if you found this useful 😃.

---