Learn Go with the help of Python
Tags: python, go, tutorial
Published: 2020-05-28 11:07:44

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](https://www.cuelogic.com/blog/go-programming-and-why-should-you-learn-go) 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.
```python
print("Hello world")
```
In Go, a hello world program will look like this:
```go
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

```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
```python
a = 50
a = "fifty"
```
This is valid in python. But you cannot do this in Go.
```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
```go
fruit := "apple"
```

[Here](https://www.tutorialspoint.com/go/go_variables.htm) 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.
<table>
<tr>
<th>Python</th>
<th>Go lang</th>
</tr>
<tr>
<td>int</td>
<td>int8, int8, int16, int32, int64, uint8, uint16, uint32, uint64</td>
<tr>
<tr>
<td>float</td>
<td>float32, float64, complex64, complex128</td>
</tr>
<tr>
<td>string</td>
<td>string</td>
</tr>
<tr>
<td>Boolean</td>
<td>Boolean</td>
</tr>
<tr>
<td>List</td>
<td>Array</td>
</tr>
</table>

## 4.Input-output
This is how you take input in python
```python
name = input("What's your name?")
```

This is how to do that in Go
```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.
```python
for i in range(50):
    # Do something
```
Here is how you write loops in go
```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.
```python
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
```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!

```python
def add(a, b):
    return a + b
```

And in Go
```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.
- [Tutorialspoint](https://www.tutorialspoint.com/go/index.htm)
- [Codecademy](https://www.codecademy.com/learn/learn-go)
- [Official Tutorial](https://tour.golang.org/welcome/1)

Happy learning for you. 🤗