GoLang (Part 1)
GoLang is the latest language that has become very apparent in cloud technologies. Every major projects are turning to goLang. Its high time to learn
💜Little about goLang..
- goLang was designed by google in 2007.
- They wanted to create a language that would fix problems with traditional languages like concurrency.
- Languages like Java, C++ can be used to implement concurrency but its quite hard as well as complicated.
- They also wanted the new language to have better memory safety, better garbage collection, better structural typing with being much faster and readable.
- goLang is very efficient and mainly used in distributed systems.
- This language quite resembles with python for simplicity and C++ for efficiency.
👉Remember, The best part of goLang is it removes complication for implementing concurrency. We will look into that in the upcoming articles.
👉 Follow this article for setting up your system to run goLang Here
💜VARIABLES
- Variables are like small storage values with predefined data types.
- We define these to store values, that can be used ahead in the code.
- We define variables :
There are 3 ways to define a variable ->
1. var <variable_name> = <value>
2. <variable_name> := <value>
3. var <variable_name> <data_type> OR var <variable_name> <data_type> = <value>
**Explanation**
1 -> When we use first way, goLang compiler automatically assigns a data type depending on the value given to it. So, First one requires value when we declare the variable else we will get compiler error as shown.
❌ var <variable_name>
✅ var <variable_name> = <value>
2 -> When we use second way, that also assigns data type automatically to the variable name and it also requires a value.
❌ <variable_name>
✅ <variable_name> := <value>
3 -> We use third way when we don't already know the value of variable. We can use it both with the value or without the value.
✅ var <variable_name> <data_type>
✅ var <variable_name> <data_type> = <value>
💜When, Where to use what??
- We select between first and second way, when we need to define a const variable in the code. As by default goLang will take variables to be var.
- When using first and second way to declare the variable, goLang automatically assigns data type which would not be the required behaviour always.
- My personnel preference would be the third one as it works with the value or without as well as we can define a var or const and with proper data type.
💜Examples
1. var numberOfTickets = 69 // We can define here const/var but not specific data type.
2. numberOfTickets := 69 // We can't define here const/var.
3. var numberOfTickets int = 69 // We can define here const/var with proper data type.
💜Data types
**int8** 8-bit signed(+/-) integer
**int16** 16-bit signed(+/-) integer
**int32** 32-bit signed(+/-) integer
**int64** 64-bit signed(+/-) integer
**uint8** 8-bit unsigned(+) integer
**uint16** 16-bit unsigned(+) integer
**uint32** 32-bit unsigned(+) integer
**uint64** 64-bit unsigned(+) integer
**float32** 32-bit IEEE 754 floating-point number
**float64** 64-bit IEEE 754 floating-point number
**int** Both int and uint contain same size, either 32 or 64 bit.
**uint** Both int and uint contain same size, either 32 or 64 bit.
**complex64** Complex numbers which contain float32 as a real and imaginary component.
**complex128** Complex numbers which contain float64 as a real and imaginary component.
**string** word/sentences
👉 Remember, by default goLang takes either int,float64,complex64 or string i.e. highest order data type depending on the value assigned. If we need to have specific data type, we need to define it specifically.
💜Run your program
- Paste this code in and run your program using "go run ."
- We will look into packages, fmt and many more in the next articles.
package main
import "fmt"
func main() {
//string
var1 := "hello world"
// integer
var2 := 10
var var6 uint = 10
// float
var3 := 1.55
var var5 float32 = 1.55
// boolean
var4 := true
fmt.Printf("var1 = %T\n", var1) //string
fmt.Printf("var2 = %T\n", var2) //int
fmt.Printf("var3 = %T\n", var3) //float64
fmt.Printf("var4 = %T\n", var4) //boolean
fmt.Printf("var5 = %T\n", var5) //float32
fmt.Printf("var6 = %T\n", var6) //uint
}
That's it folks. Do follow to get notified about future articles about few advanced concepts of goLang like concurrency, context and many more. Next Article would be about structure, functions and packages in goLang. 💁♂️ till then HAPPY READING!!
Hello, I am Tanisk Annpurna
I post about
🚀web3, Blockchain, Ethereum
🐦Smart Contract, Solidity
🎉JavaScript, ReactJS, NodeJS and many more...
Follow and like for more such posts. !!✌️!!