0
0

はじめに

こんにちは、H×Hのセンリツ大好きエンジニアです。(同担OKです😉)

今回は複雑な構造体や、ログなどを見やすく整形してくれるGoのライブラリであるprettyをご紹介します!

prettyの魅力

こちらの The Go Playground でも体験できるように、pretty.Formatter()を使うことで整形された結果を確認することが出来ます!

main.go
package main

import (
	"fmt"

	"github.com/kr/pretty"
)

func main() {
	type myType struct {
		a, b int
	}
	var x = []myType{{1, 2}, {3, 4}, {5, 6}}
	fmt.Printf("%# v\n", x)
	fmt.Printf("%# v", pretty.Formatter(x))
}
[]main.myType{main.myType{a: 1, b: 2}, main.myType{a: 3, b: 4}, main.myType{a: 5, b: 6}}
[]main.myType{
    {a:1, b:2},
    {a:3, b:4},
    {a:5, b:6},
}

prettyを使用した時としていないときで、ログの見やすさがかなり変わったと思います!
(他の言語でも似たようなライブラリはあるので体験済みかも知れませんが😇)

このような簡単な構造体ならまだしも、複雑な出力結果の時に効力を発揮します!

main.go
package main

import (
	"encoding/json"
	"fmt"

	"github.com/kr/pretty"
)

func main() {
	// レスポンスとして返ってくるJSONのモック用意
	jsonResponse := `{
		"location": {
			"city": "Fukuoka",
			"area": "Kyushu",
			"prefecture": "Fukuoka"
		},
		"forecasts": [
			{
				"dateLabel": "今日",
				"telop": "曇のち雨",
				"date": "2024-07-02",
				"temperature": {
					"min": {
						"celsius": "25",
						"fahrenheit": "77"
					},
					"max": {
						"celsius": "30",
						"fahrenheit": "86"
					}
				}
			}
		]
	}`

	// JSONを構造体にアンマーシャル
	var data interface{}
	err := json.Unmarshal([]byte(jsonResponse), &data)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Printf("%# v\n", data)
	fmt.Printf("%# v", pretty.Formatter(jsonResponse))
}

受け取ったJSONを見てみると、

map[string]interface {}{"forecasts":[]interface {}{map[string]interface {}{"date":"2024-07-02", "dateLabel":"今日", "telop":"曇のち雨", "temperature":map[string]interface {}{"max":map[string]interface {}{"celsius":"30", "fahrenheit":"86"}, "min":map[string]interface {}{"celsius":"25", "fahrenheit":"77"}}}}, "location":map[string]interface {}{"area":"Kyushu", "city":"Fukuoka", "prefecture":"Fukuoka"}}
map[string]interface {}{
    "forecasts": []interface {}{
        map[string]interface {}{
            "date":        "2024-07-02",
            "dateLabel":   "今日",
            "telop":       "曇のち雨",
            "temperature": map[string]interface {}{
                "max": map[string]interface {}{
                    "celsius":    "30",
                    "fahrenheit": "86",
                },
                "min": map[string]interface {}{
                    "celsius":    "25",
                    "fahrenheit": "77",
                },
            },
        },
    },
    "location": map[string]interface {}{
        "area":       "Kyushu",
        "city":       "Fukuoka",
        "prefecture": "Fukuoka",
    },
}

複雑な構造体であれば、さらに見栄えが良くなるのでおすすめです!

ただ一点注意がありまして、文字列は整形できませんので、JSON形式の文字列を構造体にアンマーシャルするという工程を挟む必要があります。

prettyは文字列を整形できない
そのため、構造体にアンマーシャルする必要あり

おわりに

今回は整形ライブラリであるprettyについて簡単に紹介しました!

最後までご覧いただきありがとうございました!
以上、センリツでした。🤓

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0