0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Go】fmt.Printfのフォーマット指定子まとめ&チートシート

Posted at

はじめに

Go言語でフォーマット付き出力を行う際に欠かせないのがfmt.Printfです。
しかし、使えるフォーマット指定子(バーブ)の種類が多く、どれを使えばいいのか迷うこともありますよね?

本記事では、fmt.Printfで使えるすべてのフォーマット指定子を説明+サンプルコード付きで分かりやすく解説します!
さらに、すぐに使えるチートシートも用意しているので、ぜひ参考にしてください 💡


各バーブの説明とサンプルコード

一般的なバーブ

バーブ 説明
%v デフォルトのフォーマットで値を表示
%+v 構造体のフィールド名を含めて表示
%#v Go の構文として有効な形で表示
%T 値の型を表示
%% リテラルの % を表示
package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "Alice", Age: 30}

    fmt.Printf("%v\n", p)   // {Alice 30}
    fmt.Printf("%+v\n", p)  // {Name:Alice Age:30}
    fmt.Printf("%#v\n", p)  // main.Person{Name:"Alice", Age:30}
    fmt.Printf("%T\n", p)   // main.Person
    fmt.Printf("%%\n")      // %
}

ブーリアン

バーブ 説明
%t true または false を表示
package main

import "fmt"

func main() {
    flag := true
    fmt.Printf("%t\n", flag)  // true
}

整数

バーブ 説明
%b 2進数
%c Unicodeコードポイントを文字で表示
%d 10進数
%o 8進数
%O 0o プレフィックス付きの8進数
%q 文字をシングルクォート付きで表示
%x 16進数(小文字)
%X 16進数(大文字)
%U Unicode 形式(U+XXXX)
package main

import "fmt"

func main() {
    num := 65

    fmt.Printf("%b\n", num)  // 1000001
    fmt.Printf("%c\n", num)  // A
    fmt.Printf("%d\n", num)  // 65
    fmt.Printf("%o\n", num)  // 101
    fmt.Printf("%O\n", num)  // 0o101
    fmt.Printf("%q\n", num)  // 'A'
    fmt.Printf("%x\n", num)  // 41
    fmt.Printf("%X\n", num)  // 41
    fmt.Printf("%U\n", num)  // U+0041
}

浮動小数点数と複素数

バーブ 説明
%b 2の指数形式
%e 科学技術表記(小文字)
%E 科学技術表記(大文字)
%f 小数点形式
%F %f のエイリアス
%g %e または %f を適用
%G %E または %F を適用
%x 16進浮動小数点
%X 16進浮動小数点(大文字)
package main

import "fmt"

func main() {
    f := 123.456

    fmt.Printf("%b\n", f)  // 64-bit 表現
    fmt.Printf("%e\n", f)  // 1.234560e+02
    fmt.Printf("%E\n", f)  // 1.234560E+02
    fmt.Printf("%f\n", f)  // 123.456000
    fmt.Printf("%F\n", f)  // 123.456000
    fmt.Printf("%g\n", f)  // 123.456
    fmt.Printf("%G\n", f)  // 123.456
}

文字列とバイトスライス

バーブ 説明
%s 文字列として表示
%q Go のクォート付きの形で表示
%x 16進数(小文字)
%X 16進数(大文字)
package main

import "fmt"

func main() {
    str := "Hello"

    fmt.Printf("%s\n", str)  // Hello
    fmt.Printf("%q\n", str)  // "Hello"
    fmt.Printf("%x\n", str)  // 48656c6c6f
    fmt.Printf("%X\n", str)  // 48656C6C6F
}

スライスとポインタ

バーブ 説明
%p ポインタのアドレス
package main

import "fmt"

func main() {
    arr := []int{1, 2, 3}
    fmt.Printf("%p\n", &arr)  // 0xc0000140f0 (メモリアドレス)
}

フォーマット指定子のチートシート

カテゴリ バーブ 説明
一般 %v デフォルトの表示形式
%+v フィールド名付き
%#v Go の構文で表示
%T 型を表示
%% % を表示
ブール %t true または false
整数 %b 2進数
%c Unicode文字
%d 10進数
%o 8進数
%O 0o 付き8進数
%q シングルクォート付き
%x 16進数(小文字)
%X 16進数(大文字)
%U Unicode 形式
浮動小数点数 %b 2の指数形式
%e 科学技術表記(小文字)
%E 科学技術表記(大文字)
%f 小数点表示
%g %e または %f
%G %E または %F
文字列 %s 文字列として表示
%q クォート付き表示
%x 16進数(小文字)
%X 16進数(大文字)
ポインタ %p メモリアドレス

まとめ

fmt.Printfには、多くのフォーマット指定子があり、用途に応じて適切に使い分けることが重要です。
本記事で紹介したチートシートを活用すれば、必要なバーブを素早く見つけることができます ✅

Goのフォーマット指定をしっかり理解し、効率的にコードを書いていきましょう! 🚀

公式ドキュメント

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?