Go Basic Stuff compared to other languages
Strong Typing vs. Dynamic Typing
Go
is statically typed, meaning you must declare the type
of a variable when it is defined, and the type of a variable cannot change. This promotes type safety and helps catch errors at compile time. Although you can user the :=
operator in local scopes
Python
is dynamically typed, where you don't need to declare the type of a variable explicitly, and the type of a variable can change during runtime. This provides more flexibility but can lead to runtime type errors.
Structs instead of classes
Python
is a multi-paradigm language that supports object-oriented programming (OOP) with classes, objects, and inheritance.
Go
, on the other hand, does not have traditional classes or support for inheritance. Instead, it uses a struct
type to define data structures, and methods can be attached to these structures to achieve behavior similar to OOP.
Functions
- In
Python
, functions are first-class citizens, which means you can pass functions as arguments to other functions, return them from functions, and store them in data structures.
- In
Go
, functions are also first-class citizens, and you can use them similarly to Python. However, Go lacks lambda
functions (anonymous functions) and some higher-order functions that are common in Python.
Error Handling
- Go has a
unique error handling
pattern where functions return both a result and an error
value, and you're expected to check the error value after every function call to handle errors explicitly. This promotes a more robust error-handling approach.
- Python often uses exceptions for error handling, and you can use
try
and except
blocks to handle exceptions. Python's approach can lead to less verbose code, but it may not be as explicit as Go's approach.
- Go enforces a strict code formatting style using the
gofmt
tool to maintain consistency and readability across Go codebases.
- Python also has recommended coding conventions (PEP 8), but they are not enforced by the language itself.