Skip to content

Latest commit

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Go (Golang) for Python developers 🚀

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 :)

Examples


Variables

Python

mutable_variable=2CONST_VARIABLE=3.14# There isn't a way to define a constant variable in Pythona, b=1, "one"# Declaring two mutable variables at once

Go

varmutableVariableint=2constConstVariablefloat=3.14vara, b=1, "one"// Go automatically assigns types to each variable (type inferred), so you can't change them later.

Data Types

Python

string_var="Hello Python!"integer_var=2float_var=3.14boolean_var=True

Go

varstring_varstring="Hello Go!"varinteger_varint=2varfloat_varfloat=3.14varboolean_varbool=true

For loop

Python

i=10foriinrange(10):
print(i)

Go

// Initial; condition; after loopfori:=0; i<10; i++ { // Using the shorthand syntax of declaring a variable in Go (mutableVar := the value)fmt.Println(i)
}

While loop

Python

counter=0whilecounter<5:
print(counter)
counter+=1

Go

varcounterint=0forcounter<5 {
fmt.Println(counter)
counter+=1
}

If/Else

Python

age=25ifage>=13andage<=19:
print("Teenager")
elifage>=20andage<=29:
print("Young adult")
elifage>=30andage<=39:
print("Adult")
else:
print("Other")

Go

varageint=25ifage>=13&&age<=19 {
fmt.Println("Teenager")
} elseifage>=20&&age<=29 {
fmt.Println("Young adult")
} elseifage>=30&&age<=39 {
fmt.Println("Adult")
} else {
fmt.Println("Other")
}

Array/Slice

Python

mix_list= [False, 1, "two"] # Can be any size

Go

varboolArray [3]bool= [3]bool{false, true, true} // var variableName [array size]type of array elementsvarstringArray [3]string= [3]string{"zero", "one", "two"}
varintArray [3]int= [3]int{0, 1, 2}
varboolSlice []bool= []bool{false} // var variableName []type of slice elementsvarstringSlice []string= []string{"zero", "one"}
varintSlice []int= []int{0, 1, 2}

For loop Over Array/Slice

Python

mix_list= [False, 1, "two"]
foriteminmix_list:
print(item)

Go

varintSlice []int= []int{0, 1, 2}
forindex, value:=rangeintSlice {
fmt.Println(index, value)
}

Map

Think of Map as Python's dictionary. Like an Array/Slice, it requires specifying key and value types.

Python

the_dictionary= {"hi": 1, "bye": False}
print(the_dictionary["hi"])

Go

vartheMapmap[string]int=map[string]int{"hi": 1, "bye": 0}
fmt.Println(theMap["hi"])

Function

Python

defthe_function(first_arg, second_arg):
returnf"The first argument is {first_arg} and the second argument is {second_arg}", True

Go

functheFunction(firstArgstring, secondArgstring) (string, bool) { // (argument argumentType) (return typeValue)returnfmt.Sprintf("The first argument is %s and the second argument is %s", firstArg, secondArg), true
}

HTTP Get Request

Python

importrequestsresponse=requests.get("https://example.com/")
print(response.content)

Go

import (
"fmt""io""log""net/http"
)
resp, err:=http.Get("https://example.com/")
iferr!=nil {
log.Println(err)
}
deferresp.Body.Close()
body, err:=io.ReadAll(resp.Body)
fmt.Println(string(body))

About

Examples of Go (Golang) compared to Python

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors