Skip to content

Instantly share code, notes, and snippets.

@computerphysicslab
Last active July 31, 2020 07:03
Show Gist options
  • Save computerphysicslab/936308e0d8b743c029840300cce5f13b to your computer and use it in GitHub Desktop.
Save computerphysicslab/936308e0d8b743c029840300cce5f13b to your computer and use it in GitHub Desktop.
/*
When debugging your code in goLang you may need a pretty print function not just for int or strings,
but capable to render any kind of data structure. Here it is, simple and useful. Enjoy it!
*/
package main
import (
"encoding/json"
"fmt"
"reflect"
)
func interface2string(i interface{}) string {
pretty, err := json.MarshalIndent(i, "", " ")
if err != nil {
fmt.Println("error: ", err)
}
return string(pretty)
}
func myDebug_static(s string, i interface{}) {
pretty := interface2string(i)
fmt.Printf("\nmyDebug: %s (%s) => %s\n", s, reflect.TypeOf(i).String(), string(pretty))
// fmt.Printf("\n\nmyDebug: %s RAW => %s", s, i)
// fmt.Printf("\n\nmyDebugPlain: %+v\n", i)
}
// Debugging function to show a data structure w/ pretty style
// If called with 1 parameter, it just show its value
// If call with 2 parameters, the first one should be the name of the structure to enhance debug visibility
func myDebug(params ...interface{}) {
if len(params) == 0 {
fmt.Println("\nError: not enough parameters when calling myDebug function")
} else if len(params) == 1 {
myDebug_static("", params[0])
} else if len(params) == 2 {
switch params[0].(type) {
case string:
myDebug_static(params[0].(string), params[1])
default:
myDebug_static("", params[0])
myDebug_static("", params[1])
}
} else {
fmt.Println("\nError: too many parameters when calling myDebug function")
}
}
func main() {
link1 := "https://hn.algolia.com/?dateRange=pastWeek&page=0&prefix=false&query=golang&sort=byDate&type=story"
link2 := "https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-configure-proxy-on-ubuntu-18-04/"
link3 := "https://www.expressvpn.com/what-is-my-ip?gclid=CjwKCAjw34n5BRA9EiwA2u9k3-hTxM73D-v-MsEX21RKlAucrnrXkJ5aWR-xduY4V-_i-gcJEdjKCBoCgsMQAvD_BwE"
type ALink struct {
Url string
Count int
Visited bool
}
var LPool []ALink
LPool = append(LPool, ALink{Url: link1, Count: 0, Visited: false})
LPool = append(LPool, ALink{Url: link2, Count: 0, Visited: false})
LPool = append(LPool, ALink{Url: link3, Count: 0, Visited: false})
// myDebug("LPool", LPool)
myDebug(LPool)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment