All Basic Data Types in GO

All Basic Data Types in GO

Beginners guide to all the basic data types available in GO (golang)

ยท

4 min read

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 ๐Ÿ‘‡

  • 1codewithab -> INVALID (Reasonโ“: identifier name cannot start with a number)
  • for -> INVALID (Reasonโ“: for is a special/reserved keyword in GO use to define for-loops )
    If you want to read about all the GO reserved words, you can have a brief look at them here ๐Ÿ‘ˆ
  • code+with+ab -> INVALID (Reasonโ“: identifier name connot contain special characters)
  • code_with_ab -> VALID

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

  • int8 -> -128 to 127 bits
  • int16 -> -32768 to 32767
  • int32 -> โˆ’ 2,147,483,648 to 2,147,483,647
  • int64 -> โˆ’ 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

For unsigned integers

  • uint8 -> 0 to 255
  • uint16 -> 0 to 65,535
  • uint32 -> 0 to 4,294,967,295
  • uint64 -> 0 to 18,446,744,073,709,551,615

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.

ย