Discovering Go : Feels Like C, Thinks Like Python
A few months ago, I started working with Go (Golang) at my job. Coming from Python and C++, I wasn’t sure where it would fit in. But after spending time with it, I realized that Go was designed to solve real-world problems without unnecessary complexity. It didn’t take long for me to understand why companies like Google, Uber, and Netflix rely on it for their backend systems, cloud computing, and even some embedded applications.
First Impressions: Feels Like C, Thinks Like Python
The first thing that stood out to me about Go was how clean it felt. It reminded me of writing in C—but without the low-level headaches. At the same time, its syntax was just as easy to follow as Python.
One of the first things I did was compare a simple loop across all three languages to see how Go stacks up in readability and performance. But instead of the usual boring "print numbers from 1 to 10" example, let’s try something a bit more fun:
Counting Prime Numbers (The Classic Efficiency Test)
C++: Blazing Fast, But With Some Boilerplate
#include <iostream>
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
int count = 0;
for (int i = 1; i <= 1000000; i++) {
if (isPrime(i)) count++;
}
std::cout << "Total primes: " << count << std::endl;
return 0;
}
? Fastest execution time
? Manual memory management, verbose syntax
Python: Elegant, but Slower
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
count = sum(1 for i in range(1, 1000000) if is_prime(i))
print(f"Total primes: {count}")
? Short and easy to read
? Significantly slower due to Python’s interpreter overhead
Go: The Middle Ground Between Speed and Simplicity
package main
import (
"fmt"
"math"
)
func isPrime(n int) bool {
if n < 2 {
return false
}
for i := 2; i <= int(math.Sqrt(float64(n))); i++ {
if n%i == 0 {
return false
}
}
return true
}
func main() {
count := 0
for i := 1; i <= 1000000; i++ {
if isPrime(i) {
count++
}
}
fmt.Println("Total primes:", count)
}
? Fast execution, close to C++ performance
? Much easier to write and read than C++
? Automatic memory management
For this test, Go came very close to C++ in speed while maintaining a much simpler syntax. This balance is exactly what makes Go so appealing for high-performance backend systems.
Concurrency: Go’s Biggest Advantage Over Python & C++
One of the reasons Go caught my attention was its built-in support for concurrency. Writing multithreaded code in C++ can be complex and error-prone, while Python’s concurrency is limited by the Global Interpreter Lock (GIL).
领英推荐
Here’s an example of a simple concurrent task across all three languages:
C++: Managing Threads is a Hassle
#include <iostream>
#include <thread>
#include <vector>
void printNumbers(int id) {
for (int i = 1; i <= 5; i++) {
std::cout << "Thread " << id << ": " << i << std::endl;
}
}
int main() {
std::vector<std::thread> threads;
for (int i = 0; i < 3; i++) {
threads.emplace_back(printNumbers, i);
}
for (auto& t : threads) {
t.join();
}
}
? Highly efficient
? Manually managing threads is error-prone
Python: Threads, But Not True Parallelism
import threading
def print_numbers(id):
for i in range(1, 6):
print(f"Thread {id}: {i}")
threads = [threading.Thread(target=print_numbers, args=(i,)) for i in range(3)]
for t in threads:
t.start()
for t in threads:
t.join()
? Easy to write
? Not true parallelism due to GIL
Go: Concurrency Done Right
package main
import (
"fmt"
"sync"
)
func printNumbers(id int, wg *sync.WaitGroup) {
defer wg.Done()
for i := 1; i <= 5; i++ {
fmt.Printf("Goroutine %d: %d\n", id, i)
}
}
func main() {
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go printNumbers(i, &wg)
}
wg.Wait()
}
? Simple concurrency model using goroutines ? Efficient, lightweight, and true parallelism
This was a game-changer for me. In C++, managing concurrency is painful; in Python, it’s limited. But Go makes it so easy that I now find myself using concurrency even in simple applications.
Real-World Use Cases: Where Go is Winning
After getting comfortable with Go, I started seeing its real-world advantages.
I even experimented with writing some Go scripts for Linux automation, and it felt just as easy as writing Bash scripts but with better performance and more flexibility.
Final Thoughts: A Strong Addition to My Toolkit
At first, I wasn’t sure where Go would fit into my workflow. But after using it, I now see it as an invaluable tool for certain types of projects:
I didn’t expect to enjoy Go this much, but now, whenever I start a new project, I find myself asking: “Can I do this in Go?”
If you’ve worked with Go or are thinking about trying it, I’d love to hear your thoughts!
Full-Stack Developer | Expert in NestJS,ReactJS, Python Django & SQL | AWS Enthusiast
3 周I think so. Really I love it.
Senior Android Developer
3 周Love this