0
0

【Go】配列(スライス)などの値を標準出力でデバッグするときは、エスケープして出したほうがいいかもという話

Posted at

普通に出力した場合

arry := []string{"a b", "c", "d"}
fmt.Println(arry)
// [a b c d]

"a b" は一つのアイテムであるが、普通に出力するとアイテムごとに空白で区切られてしまうので分かりづらい。

エスケープして出してみる

arry := []string{"a b", "c", "d"}
fmt.Println(fmt.Sprintf("%#v", arry))
// []string{"a b", "c", "d"}

printf系の関数を使って、書式指定子を %#v とする。
#をつけることで、Go構文表現のままで出力してくれるので認識しやすい。

参考にしたもの
https://qiita.com/KEINOS/items/9bf8d33b0c616f85ba4a
https://pkg.go.dev/fmt

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