Having come from the PHP community, we often have a handy debug function at our disposal dd() (part of Laravel) and var_dump() (native). This may not be perfect (compared with a full on debugging suite) but it is a quick and easy debugging method. This post gives an idea on how you can do the same, but in Go.
We’re helped out by being able to use both variable parameters (shown with ...) and the interface type to handle different types, both strings and integers in our examples.
The only real difference between varDump and dd is that with the die-dump version it also calls os.Exit to stop the program.
packagemainimport("fmt""os")funcmain(){varDump("testing","gophercoding","debug")// = [testing gophercoding debug]
myVar1:="gophercoding.com"myVar2:=1234dd(myVar1,myVar2)// = [gophercoding 1234]
// END
// This won't run, as dd() has run to stop program
fmt.Printf("end of file\n")}// varDump will print out any number of variables given to it
// e.g. varDump("test", 1234)
funcvarDump(myVar...interface{}){fmt.Printf("%v\n",myVar)}// dd will print out variables given to it (like varDump()) but
// will also stop execution from continuing.
funcdd(myVar...interface{}){varDump(myVar...)os.Exit(1)}
Edd is a PHP and Go developer who enjoys blogging about his experiences, mostly about creating and coding new things he's working on and is a big beliver in open-source and Linux.