👉 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:-
String
Which is sequence of character and used to represent text
Number
Consist of integers(without decimal like -12 0) and floats(contains decimals such as 7.0 or 85.56.)
Boolean
Value True or False.
Array
An array is like a list or a sequence of elements of a single data type(array of just integer strings or floats)
Slices
Represent flexible array-like data type but they also provide more control over memory allocation.
Why are data types needed?
categorize a set of related values
describe the operations that can be done on them
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
Better Performances
Bugs can often be caught by a compiler
Better data integrity
Dynamic Typed advantages
Faster to write code.
Generally, less rigid.
Shorter learning curve.
Golang
Go has a concept of types that is either explicitly declared by a programmer or is inferred by the compiler.
It's fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.
Example:- main.go
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 Type | Memory |
unit8 | 8 bits or 1 byte |
unit16 | 16 bits or 2 bytes |
unit32 | 32 bits or 4 bytes |
unit64 | 64 bites or 8 bytes |
int8 | 8 bits or 1 byte |
int16 | 16 bits or 2 bytes |
int32 | 32 bites or 4 bytes |
int64 | 64 bits or 8 bytes |
int | 4 bytes for 32-bit machines, 8 bytes for 64-bit machines |
unit means "unsigned integer"
int means "signed integer"
Float
data Type | Memory |
float32 | 32 bits or 4 bytes |
float64 | 64 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!👏