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

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

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