Last active
July 7, 2020 07:00
-
-
Save computerphysicslab/dc120f13c11f418bedfec64a0f3b4a2b to your computer and use it in GitHub Desktop.
goLang debugging through JSON MarshalIndent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IMPORTANT!!! You need to export the T1.id field so that the json package can see it. Rename the name field to Id. | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"reflect" | |
) | |
func myDebug(s string, i interface{}) { | |
pretty, err := json.MarshalIndent(i, "", " ") | |
if err != nil { | |
fmt.Println("error:", err) | |
} | |
fmt.Printf("\n\nmyDebug: %s (%s) => %s\n", s, reflect.TypeOf(i).String(), string(pretty)) | |
fmt.Printf("\n\nmyDebugPlain: %+v\n", i) | |
} | |
type M map[string]interface{} | |
type T1 struct { | |
Id int | |
Texto string | |
} | |
type T2 struct { | |
Id int | |
Texto string | |
Mapas []M | |
} | |
func main() { | |
tm := make(M) | |
tm["fruta"] = "manzana" | |
tm["verdura"] = "coliflor" | |
tm2 := make(M) | |
tm2["planas"] = "lentejas" | |
tm2["redondas"] = "garbanzos" | |
myDebug("tm", tm) | |
myDebug("tm2", tm2) | |
tm["legumbre"] = tm2 | |
myDebug("tm", tm) | |
var tm3 T2 | |
tm3.Id = 11 | |
tm3.Texto = "hello world" | |
myDebug("tm3", tm3) | |
tm3.Mapas = append(tm3.Mapas, tm) | |
myDebug("tm3", tm3) | |
tm4 := T1{12, "hi"} | |
myDebug("tm4", tm4) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment