Skip to content

Basic Data Types in GO

Updated: at 07:20 PM

Basic data types in golang | abdadeel

Table of contents

Open Table of contents

Introduction

GO has all the basic and advanced data types like other programming languages. Before we dive into the data types, let’s first have a look at some conventions in GO.

are-you-ready

Filenames in GO

GO code is written and stored in the .go file. The filename itself can consist of lower-case letters by convention. For example codewithab.go is a valid filename by convention. In another case, if file names have multiple words, that word should be separated by _ rather than camel case by convention. Like code_with_ab.go is a valid file name in that case. The main constraints in the filename that cause the filename to be invalid are 👇:

- Including spaces in the filename
- Including special characters like `@`, `#`, `$`

Identifiers in GO

Identifiers are the elements of a program (go program) that are assigned by the user/developer like a variable is defined by the developer. The same is the case for a function name. Below are some examples of NOT valid identifiers 👇


Go Data Types

In this article, we will focus and discuss the details about these data types 👇

Strings

String type in GO is basically a slice of bytes type. But here is the interesting fact about this type. GO in fact does not have any byte type but the byte type is an alias for the uint8 type. Pretty confusing right 😵?

mind-boggling

Okay, so let’s simplify it. In short, the string type is basically a collection of byte types where each character of the string is represented separately.

package main

import (
    "fmt"
)

func printIndividialBytes(fullString string) {
    fmt.Printf("Bytes: ")
    for i := 0; i < len(fullString); i++ {
        fmt.Printf("%x ", fullString[i])
    }
}

func main() {
    name := "Code with AB"
    fmt.Printf("String: %s\n", name)
    printIndividialBytes(name)
}

/* Output: 👇
String: Code with AB
Bytes: 43 6f 64 65 20 77 69 74 68 20 41 42
*/

Run In Playground 🔗

Boolean

Boolean is the same as pretty much any other programming language with true and false as options.

var isPaidCustomer bool = true;

Integers Type

Integer types are differed by the memory allocation of the particular variable. To make it more clear. Let’s take an example. Let’s define a variable and initialize it with a number and a data type of int8.

var years_of_coding int8 = 3;

In the above example, the variable years_of_coding will take 8 bits of space on memory.

Similarly

For unsigned integers, GO documentation clearly says this 👇

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

Complex numbers are supported by GO. They have the following form 👇:

re + img¡

where re is the real part of the complex number and img is the imaginary part of the complex number.

package main
import "fmt"

func main(){
    // To declare complex number (real +imaginary(¡))
    var comp complex64 = 5 + 10i
    fmt.Printf("The value is: %v", comp)
}
/*
OUTPUT 👇
The value is: (5+10i)
*/

%v is used to print the formatted complex number. To print real and imaginary parts as separate use %f


That’s it from my side for today. I hope you learn something today. If you want to practice along with that I would highly encourage, you can visit the playground here.