This project is inspired by the Golang for Node.js developers project. Similar to that project, I will do my best to provide numerous examples for those who are familiar with Python and are interested in learning Go. Personally, my journey is similar to yours. I started with Python and later became drawn to this delightful and efficient language :)
mutable_variable = 2
CONST_VARIABLE = 3.14 # There isn't a way to define a constant variable in Python
a, b = 1, "one" # Declaring two mutable variables at oncevar mutableVariable int = 2
const ConstVariable float = 3.14
var a, b = 1, "one" // Go automatically assigns types to each variable (type inferred), so you can't change them later.string_var = "Hello Python!"
integer_var = 2
float_var = 3.14
boolean_var = Truevar string_var string = "Hello Go!"
var integer_var int = 2
var float_var float = 3.14
var boolean_var bool = truei = 10
for i in range(10):
print(i)// Initial; condition; after loop
for i := 0; i < 10; i++ { // Using the shorthand syntax of declaring a variable in Go (mutableVar := the value)
fmt.Println(i)
}counter = 0
while counter < 5:
print(counter)
counter += 1var counter int = 0
for counter < 5 {
fmt.Println(counter)
counter += 1
}age = 25
if age >= 13 and age <= 19:
print("Teenager")
elif age >= 20 and age <= 29:
print("Young adult")
elif age >= 30 and age <= 39:
print("Adult")
else:
print("Other")var age int = 25
if age >= 13 && age <= 19 {
fmt.Println("Teenager")
} else if age >= 20 && age <= 29 {
fmt.Println("Young adult")
} else if age >= 30 && age <= 39 {
fmt.Println("Adult")
} else {
fmt.Println("Other")
}mix_list = [False, 1, "two"] # Can be any sizevar boolArray [3]bool = [3]bool{false, true, true} // var variableName [array size]type of array elements
var stringArray [3]string = [3]string{"zero", "one", "two"}
var intArray [3]int = [3]int{0, 1, 2}
var boolSlice []bool = []bool{false} // var variableName []type of slice elements
var stringSlice []string = []string{"zero", "one"}
var intSlice []int = []int{0, 1, 2}mix_list = [False, 1, "two"]
for item in mix_list:
print(item)var intSlice []int = []int{0, 1, 2}
for index, value := range intSlice {
fmt.Println(index, value)
}Think of Map as Python's dictionary. Like an Array/Slice, it requires specifying key and value types.
the_dictionary = {"hi": 1, "bye": False}
print(the_dictionary["hi"])var theMap map[string]int = map[string]int{"hi": 1, "bye": 0}
fmt.Println(theMap["hi"])def the_function(first_arg, second_arg):
return f"The first argument is {first_arg} and the second argument is {second_arg}", Truefunc theFunction(firstArg string, secondArg string) (string, bool) { // (argument argumentType) (return typeValue)
return fmt.Sprintf("The first argument is %s and the second argument is %s", firstArg, secondArg), true
}import requests
response = requests.get("https://example.com/")
print(response.content)import (
"fmt"
"io"
"log"
"net/http"
)
resp, err := http.Get("https://example.com/")
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
fmt.Println(string(body))