In Ruby, awesome_print provides a clean and structured way to display variables. Is there something similar in Go?
While there's no built-in equivalent, the json.MarshalIndent function offers a reasonable option.
x := map[string]interface{}{"a": 1, "b": 2} b, err := json.MarshalIndent(x, "", " ") if err != nil { fmt.Println("error:", err) } fmt.Print(string(b))
This will print the variable x as a formatted JSON string:
{ "a": 1, "b": 2 }
This solution does not require third-party packages, making it suitable for situations where external dependencies are undesirable.
The above is the detailed content of How Can I Pretty-Print Variables in Go Like Ruby's `awesome_print`?. For more information, please follow other related articles on the PHP Chinese website!