Everything I learned | Week 21| Encora/Nearsoft Academy

Gibran Herrera
2 min readAug 31, 2021

Hi everyone! πŸ€—

This week was also full of amazing projects and a ton of learning bits in my main tools, Python and Golang. Let’s dive in!

β€” β€” β€” 🍣 The HTTP Package πŸ£β€” β€” β€”

Go has one of the most complete HTTP packages I had worked with, even though I do not have much experience in the field; nevertheless, that single package was enough to complete my CRUD for the contacts app I made, and it was easy to follow up, comparing to the HTTP Python package in which it has an overall more complicated step by step process. But nothing is perfect, and I realize that something I took for granted, was not included, and I’m talking about the HTTP verbs.

On the Golang HTTP package, there is no way to create a single instruction to differentiate different functions to execute according to the HTTP verb. For example, in Flask you use decorators to specify the route and HTTP verb that will execute that function:

@app.route('/contacts', methods=['POST'])
def create_contact() -> Response:
pass
@app.route('/contacts', methods=['GET'])
def get_contacts() -> Response:
pass

Both functions serve a single route but they are differentiated by the HTTP verb. In the Golang HTTP package, there is no simple way to do this, instead, the HTTP handler should use other functions to extend its behavior, like this:

func ContactsRouter() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
handler.GetContacts(w, r)
case "POST":
handler.CreateContact(w, r)
}
}
}
app := http.NewServeMux()
app.HandleFunc("/contacts", ContactsRouter)

Not the most elegant way if you ask me. But nothing is lost, this kind of behavior is added in other third-party packages like the Gorilla mux, coming in the easy way of:

router := mux.NewRouter()
router.HandleFunc("/contacts", getContacts).Methods("GET")

β€” β€” β€” β›° Final Thoughts β›° β€” β€” β€”

Learning multiple programming languages gives me a different perspective of what or how should I implement something, some languages have advantages over others and weaknesses too, but it is important to notice how something in another language may be an advantage to implement, like what the Gorilla mux team made adding the HTTP verb support into the Handler functions.

Thank you for reading until here!

See you! πŸ‘‹πŸ½

--

--

Gibran Herrera

Software Engineer πŸ‘¨β€πŸ’» β€” Pythonist 🐍 β€” Linux lover 🐧 β€” Learning πŸ¦€πŸ‹