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

🐍 FastAPI Quick Start

Short and easy FastAPI startup for a quick playground and testing. pip install fastapi pip install uvicorn – main.py – from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/hello") def read_root(): return {"Hello": "Hello"} To start using uvicorn: uvicorn app.main:app --host 127.0.0.1 --port 8089

October 31, 2022 · 1 min · Dmitry Golovach

🐍 FastAPI Background Tasks

Nice, useful, and easy-to-use feature in FastAPI. It allows responding to the client and doing whatever needs to be done after the request, the client doesn’t have to wait for the operation to complete. For example, get an API request, respond to the client right away, do the job and send a notification once it’s done as a separate event.

October 28, 2022 · 1 min · Dmitry Golovach

Go meets Cisco using SSH

More of Go I have already migrated some scripts from python to go, mostly with API. Decided to check how to SSH into network devices and run some commands. Of course, I used the well-known cisco always-on sandbox. note: using some code as a core and as an example (link in the comments Everything is pretty straightforward here: var ( User string = "developer" Password string = "C1sco12345" hosts = []string{"sandbox-iosxe-latest-1....

July 6, 2022 · 3 min · Dmitry Golovach

How long did the function run in Go

Go (Golang) is fast. This phrase is everywhere. It’s always interesting how long it takes a function to finish its operation. Here is an example of a helper function, that could be used to check and log the timing: func TimeTrack(start time.Time, name string) { elapsed := time.Since(start) log.Printf("%s took %s", name, elapsed) } It takes a start time and the name (could be a function name) as parameters. Use it with the “defer” statement inside the functions:...

July 1, 2022 · 1 min · Dmitry Golovach

Postman Environment Variables

Using Environment in Postman is one of the best things that we can use during the testing and while playing around with API. Postman makes this development phase much easier. API is getting more to the token-centric authentication and Environment Variable becomes handy. I will use Cisco Sandbox for my examples, that is one of the great ways to get some practice with Cisco products API. Setting up Postman Environment Variables:...

February 3, 2022 · 1 min · Dmitry Golovach