Go - Data types

Go - Data types

👉 Go is one of the most popular programming language, here we would be looking into Datatypes in detail.

Data Type

What is Data Type?

  • A data type is a classification made on the kind of data.

  • They categorize a set of related values.

  • Describe the operation done on them.

  • For example:-

    1. String

      Which is sequence of character and used to represent text

    2. Number

      Consist of integers(without decimal like -12 0) and floats(contains decimals such as 7.0 or 85.56.)

    3. Boolean

      Value True or False.

    4. Array

      An array is like a list or a sequence of elements of a single data type(array of just integer strings or floats)

    5. Slices

      Represent flexible array-like data type but they also provide more control over memory allocation.

Why are data types needed?

  1. categorize a set of related values

  2. describe the operations that can be done on them

  3. define the way the data is stored in the memory allocation Integer 4bytes 32-bit machine,8-bytes 64-bit machine

Static vs Dynamic typed languages

Static typed

  • Compiler throws an error when types are used incorrectly. Eg:- C++, Java

  • main.cpp

  •     void add (int a, int b) {
        count<<a+b
        }<br>
    
        add(1,2)->3
        add(1,"two")->ERROR
    

Dynamic typed

  • Compiler does not enforce the type system Ex. Python, JavaScript

  • Such languages are called weakly typed, loosely typed or dynamic typed.

  • main.js

  •     function add (a,b)
        return a+b;
        }
        add(1,2)->3
        add(1,"two")->1two
    

Static typed advantages

  1. Better Performances

  2. Bugs can often be caught by a compiler

  3. Better data integrity

Dynamic Typed advantages

  1. Faster to write code.

  2. Generally, less rigid.

  3. Shorter learning curve.

Golang

  1. Go has a concept of types that is either explicitly declared by a programmer or is inferred by the compiler.

  2. It's fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

  3. Example:- main.go

  4.   package main
      import ("fmt")
      func main() {
      name:="Shounak"
      fmt.Println(name)
      }
      >>>go run main.go
      Shounak
    

Kinds of Data Types

Numbers

Integers

int-->200,-900,-90,100000,453

Data TypeMemory
unit88 bits or 1 byte
unit1616 bits or 2 bytes
unit3232 bits or 4 bytes
unit6464 bites or 8 bytes
int88 bits or 1 byte
int1616 bits or 2 bytes
int3232 bites or 4 bytes
int6464 bits or 8 bytes
int4 bytes for 32-bit machines, 8 bytes for 64-bit machines

unit means "unsigned integer"
int means "signed integer"

Float

data TypeMemory
float3232 bits or 4 bytes
float6464 bits or 8 bytes

String

Any sequence of character which occupies 16 bytes of memory

Boolean

True and False Occupies 1 byte of memory

That's great if you have make till here you have covered basic of Golang Data types

If you liked what you read, do follow and any feedback for further improvement will be highly appreciated!

Thank you and Happy Learning!👏