Whether you are a programming rookie or a seasoned veteran, there is a high probability that you have heard the following at least once in your life
All the programming language is the same. Just they have a different syntax. If you can learn a language you can learn others too.
I am also a sore believer in this. And then I thought why not give it a spin. Also, I have been willing to learn Go for a while but couldn't do so because of my procrastination๐ . So, I thought it will be fun(and probably a smooth journey).
Why Go lang
Now, there is a ton of programming language available out there. So, why I chose Go instead of some other language? This is the reason why.
- It has a cute mascot ๐
- It is compiled(that means it will run fast)
- Simple code syntax
- Great backend support
- Good for writing CLI apps
- What you can do in python you can do in Go(a bit faster)
- A worthy opponent for Python ๐
Okay, here is a more detailed version.
Before I start
- If you already know python then great, your transition to Go will be smooth like butter
- If you don't know Python then awesome, you are going to learn two new languages at once. Isn't that cool?
Basic syntax
Despite fancy syntax of various languages, there lies some common stuff that you will need to know for a jump start with any new programming language. This is what makes the backbone of a programming language(in no particular order).
- Variables
- Data types
- Input-output
- Loops
- Control statements aka, conditions
- Function
So, let's go ๐
1.Hello world in Go
Go follows a C like syntax. So, you don't have the luxury to just start coding as you would do in python.
print("Hello world")
In Go, a hello world program will look like this:
package main
import "fmt"
func main() {
fmt.Println("Hello, world")
}
If you are familiar with C, you will see a common pattern here. First, import header then call main function and then write your stuff.
2.Variables
While you just type the name of a variable and then the value in python, there is some exception in go. This is what a basic variable declaration looks like in python.
var variable_name = value
And this is how you should do this in Go
package main
import "fmt"
func main() {
fmt.Println("Hello, world")
var fruit = "apple"
var number = 10
}
However, once you assign a value to a variable, you cannot change the data type of it. For example
a = 50
a = "fifty"
This is valid in python. But you cannot do this in Go.
package main
import "fmt"
func main() {
fmt.Println("Hello, world")
var a = 50
var a = "fifty"
}
If you don't want to type var
all the time, you can do this
fruit := "apple"
Here is some more way you can create a variable in Go.
3.Data types
There are a lot of advance data types in both Python and in Go. Here are some common data types you will probably need to use more often.
Python | Go lang |
---|---|
int | int8, int8, int16, int32, int64, uint8, uint16, uint32, uint64 |
float | float32, float64, complex64, complex128 |
string | string |
Boolean | Boolean |
List | Array |
4.Input-output
This is how you take input in python
name = input("What's your name?")
This is how to do that in Go
var name
fmt.Scan(&name)
(Don't forget to import fmt)
5.Loops
Unfortunately, there is no while loop in Golang. So, you have to get the job done only with for loops.
for i in range(50):
# Do something
Here is how you write loops in go
for i := 0; i < 50; i++{
// Do something
So, first, you initialize a variable, then set the range of the variable and then increment the variable.
6.Conditionals
You can control the flow of your code with the help of conditionals.
if num == 1:
print("The number is one")
elif num == 2:
print("The number is two")
else:
print("The number is neither 1 nor 2")
And in Go
if num == 1 {
fmt.Println("The number is one")
} else if num == 2 {
fmt.Println("The number is two")
} else {
fmt.Println("The number is neither one nor two")
}
7.Functions
Finally, lets declare some functions. Oh! wait, you have already done that. When? the func main()
itself is a function!
def add(a, b):
return a + b
And in Go
func add(a int, b int){
return a + b
}
Further reading
If you want a more in-depth knowledge of Go(which you sure do), I will suggest you read this.
Happy learning for you. ๐ค
Top comments (1)
One of the reasons I started learning it too ๐
Do share your progress ๐ฅ