Go Defer

The function is waiting until the surrounding function returns. Note: the deferred call’s arguments are evaluated immediately, but executed at the end func main() { defer fmt.Println("defer 1") fmt.Println("hello") } sh-3.2$ go run switch.go hello defer 1 We can stack deferred function calls - calls are executed in last-in-first-out order. Useful when something needs to be done at the end of the function. sh-3.2$ go run switch.go hello defer 4 defer 3 defer 2 defer 1 Nice blog about Defer, Panic and Recover....

November 22, 2022 · 1 min · Dmitry Golovach

Go Loops

There is For loop in Go, pretty easy:) while True = just for for { fmt.Printf("Slice time:\n") } For each in = range (similar to enumerate() in Python) is used to go over slices or maps a := []string{"a1", "b1", "b1", "b1", "b1", "b1"} for index, value := range a { fmt.Printf("Index %v: %vn", index, value) } Use _ if you don’t need to use index or value a := []string{"a1", "b1", "b1", "b1", "b1", "b1"} for _, value := range a { fmt....

November 17, 2022 · 1 min · Dmitry Golovach

Go Switch

I like switch in Go. func main() { all := []string{"Denver", "London", "NY", "SF"} for _, city := range all { switch city { case "Denver": // code for Denver fmt.Println("Denver") case "London", "SF": // London OR SF the same logic // code for London/SF fmt.Println("London/SF") case "NY": // code for NY fmt.Println("NY") default: // code default fmt.Println("default") } } } Very easy to use, we just create cases and what to do by default:

November 16, 2022 · 1 min · Dmitry Golovach

Go Slog

Who does not love logging? Heard of a lightweight, extensible, configurable logging library written in Golang - slog. Quick review on how to use it for my reference. package main import ( "github.com/gookit/slog" ) func main() { slog.Debug("This is DEBUG message") slog.Info("This is INFO message") slog.Notice("This is NOTICE message") slog.Warn("This is WARNING message") slog.Error("This is ERROR message") slog.Fatal("This is FATAL message") } Here is the output: Color could be disabled (enabled by default):...

November 15, 2022 · 1 min · Dmitry Golovach

Go Slices

If we need a dynamic arrays - dynamically sized- go has Slices which are much more common than arrays. Same concept, but just with no size. []type{} A slice does not store any data, it just describes a section of an underlying array. To add or change an element in an array - need to know the index, use append() - for slices func main() { empty_slice := []string{} // empty slice s := []string{"b1", "b2", "b3", "b4", "b5"} // Append returns the updated slice....

November 8, 2022 · 1 min · Dmitry Golovach

Go Arrays

Arrays have size - a fixed size, and cannot be resized (length is part of its type). How to declare: [size]type{} To create an empty array: var a [2]string an array of 2 values of type string. Can not mix types func main() { var a [2]int // if need just empty array b := [2]string{"one"} // with some elements in the array b[1] = "two" a[0] = 0 a[1] = 1 fmt....

November 7, 2022 · 1 min · Dmitry Golovach

Markdown Syntax Highlighting

I just discovered that we can add syntax highlighting to the markdown file. Very nice! ```<language> code```

November 5, 2022 · 1 min · Dmitry Golovach

🐍 FastAPI built-in API docs

FastAPI has two documentations: by swagger-ui (yourfastapiurl:port/docs) by redoc (yourfastapiurl:port/redoc) from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"result": "It's working"} @app.get("/ise/{mac_address}") async def ise_search_mac_address(mac_address): return {"Search MAC": mac_address} @app.get("/{mode}/ise") async def ise_mode(mode): if mode == 'dev': return {'result': "Working with Dev ISE"} return {'result': "Working with Prod ISE"} http://127.0.0.1:8089/docs/ Also you can try it right there: http://127.0.0.1:8089/redoc/ And for sure, great FastAPI Documentation itself: here

November 4, 2022 · 1 min · Dmitry Golovach

Go Web App

Learning Go is exciting. Decided to see how to set up a Web Application or API using Go language along with FastAPI implementation. package main import ( "log" "net/http" ) // Define a home handler function func home(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello from Goooooo")) } func main() { // http.NewServeMux() function to initialize a new servemux mux := http.NewServeMux() // match "/" URL pattern with home handler function mux.HandleFunc("/", home) // http....

November 3, 2022 · 1 min · Dmitry Golovach

🐍 FastAPI Parameters

A quick note about how to pass parameters @app.get("/ise/{mac_address}") async def ise_search_mac_address(mac_address): return {"Search MAC": mac_address} Another example: @app.get("/{mode}/ise") async def ise_mode(mode): if mode == 'dev': return {'result': "Working with Dev ISE"} return {'result': "Working with Prod ISE"}

November 1, 2022 · 1 min · Dmitry Golovach