Pythonista to Gopher: Navigating the Transition- Getting Started with Go

Bestin Mathews
4 min readJun 4, 2024

--

Hey Python coder! Thinking about diving into Go?
This guide won’t hold your hand, but it’ll give you a heads-up on how some common Python tricks translate to Go-speak. Think of it as a cheat sheet to get you started.

Here’s a quick intro to Go for Python developers:

What is Go?

Go is a free programming language created by Google. Think of it as a turbocharged version of C (Just saying, No need to learn that too), designed for speed and efficiency. However, unlike C, Go is easier to learn and use, making it ideal for building web apps and cloud services. One of Go’s standout features is its ability to handle multiple tasks simultaneously, also known as concurrent programming. This capability ensures that your applications run smoothly and efficiently, even under heavy loads.

Similarities:

  • Both are backend languages: Both Go and Python (with Django) are great for building web applications and APIs.
  • Focus on developer experience: Both languages prioritize readability and clean syntax.

Key Differences:

  • Statically typed vs. dynamically typed: Go is statically typed, meaning you define variable types beforehand. This adds a step but improves code safety and performance.
  • Compiled vs. interpreted: Go programs are compiled into binaries, while Python relies on an interpreter. This gives Go a slight edge in execution speed.
  • Concurrency: Go excels at handling concurrent tasks with lightweight processes called goroutines and channels for communication. Django, while offering asynchronous capabilities, doesn’t have the same built-in focus on concurrency.
  • Frameworks vs. Libraries: Unlike python’s framework Django, an all-encompassing framework, Go favors a library-based approach. You’ll use specific libraries for routing, database interaction, etc., giving you more control over each component.

Let’s start with the most essential and iconic program: ‘Hello World’. It might seem basic and a bit old-school, but it’s the foundational step into the new and exciting world of Go. Think of it as your first footprint in this uncharted territory.

package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
fmt.Println("Welcome to Golang!")
}

Comments & Docstrings

Python

  • Single-line comments: Use the # symbol.
  • Docstrings: Use triple quotes (""") right below a class or function definition for documentation.
# some comment 
def sample_function():
"""This is a docstring."""
pass

Go

  • Single-line comments: Use //.
  • Multi-line comments: Use /* */ (though rarely used).
  • Docstrings: Write a comment above the function or class, starting with the name of the function or class.
// This is a comment
func SampleFunction() {
// This function does something
}
/*
Multi-line comments are also supported
but not commonly used.
*/

Naming Conventions

Python

Python has specific naming styles to follow:

  • Use snake_case for functions and variables.
  • Use PascalCase for class names.
  • Write global constants in ALL_CAPS.
  • Prefix private members with an underscore (_).
# Public constant
MAX_LIMIT = 100
# Public function
def calculate_sum():
first_number = 3
second_number = 4
sum = first_number + second_number
return sum
# Private function
def _helper_function():
return something
# Public class
class DataProcessor:
pass

Go

Go uses straightforward naming rules:

  • Names starting with a lowercase letter are private.
  • Names starting with a capital letter are public.
  • Use camelCase for private names.
  • Use PascalCase for public names.
// Public constant
const MaxLimit = 100
// Public function
func CalculateSum() int {
firstNumber := 4
secondNumber := 3
returnValue := firstNumber + secondNumber
fmt.Println(returnValue)
}// Private function
func helperFunction() string {
return "something"
}
// Public struct
type DataProcessor struct {}

Variables

The main difference between Go and Python is how variables are defined and assigned.

Python

In Python, you can directly assign a value to a variable:

name = john

Golang:

In Go, variables are first declared before being assigned a value. This can be done in a less concise way (:=). The := operator is like a combination of var (declaration) and = (assignment) in one line. It's especially useful when you're first creating a variable and assigning a value to it at the same time. It also infers the type of the variable based on the value you assign.

// Declare and assign a string variable.
var name string
name = "John"

//Or Use the short assignment operator for a new variable.
name := "John" //Go automatically figures out that name should be a string type because of the value "John".

String Formatting

Python:

  • %s and %d are placeholders for strings and numbers.
  • Newer f-strings let you directly write variables inside curly braces.
first_name = "John"
last_name = "Doe"

old_way = "Name is %s %s" % (first_name, last_name) # old way (c-style)
another_way = f"Name is {first_name} {last_name}" # f-string

Golang:

  • It uses a similar approach as the older way in Python (%s and %d).
  • Golang offers basic string formatting for console and logs.
  • For example, use the fmt.Printf function. This function takes two arguments: the message with placeholders, followed by the variables in the order they appear.
// example from Go's official doc

package main

import (
"fmt"
)

func main() {
const name, age = "Kim", 22
fmt.Printf("%s is %d years old.\n", name, age) //output: Kim is 22 years old.
}

That’s it for this blog post! We’ll dive deeper in the next part where we cover control structures in Go. Stay tuned, and I hope you’re enjoying the journey from Python to Go as much as I am. I’m no expert in blogging, but I’m learning and having fun with it. Hope you are too! See you in the next one!

--

--

Bestin Mathews
Bestin Mathews

Written by Bestin Mathews

In the mirror's truth and the cosmos embrace, I find my compass.

Responses (1)