DEV Community

Cover image for Learn Go with the help of Python
Muhimen
Muhimen

Posted on

Learn Go with the help of Python

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")
Enter fullscreen mode Exit fullscreen mode

In Go, a hello world program will look like this:

package main

import "fmt"

func main() {
    fmt.Println("Hello, world")
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

However, once you assign a value to a variable, you cannot change the data type of it. For example

a = 50
a = "fifty"
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

If you don't want to type var all the time, you can do this

fruit := "apple"
Enter fullscreen mode Exit fullscreen mode

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?")
Enter fullscreen mode Exit fullscreen mode

This is how to do that in Go

var name
fmt.Scan(&name)
Enter fullscreen mode Exit fullscreen mode

(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
Enter fullscreen mode Exit fullscreen mode

Here is how you write loops in go

for i := 0; i < 50; i++{
    // Do something
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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")
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And in Go

func add(a int, b int){
    return a + b
}
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
bhupesh profile image
Bhupesh Varshney ๐Ÿ‘พ

It has a cute mascot

One of the reasons I started learning it too ๐Ÿ˜„
Do share your progress ๐Ÿ”ฅ