LoginSignup
0
0

More than 5 years have passed since last update.

[Go]structをフィールド名付きで標準出力する

Last updated at Posted at 2018-12-25

fmt.Sprinf()でフォーマットでstructの中身を出力する際に、structのフィールド名も一緒に出力できるオプションがあるのを知ったので書いておく。

sample.go
package main

import "fmt"

func main() {
    type user struct {
        id   int
        name string
    }

    res := fmt.Sprintf("%+v", user{1, "k-kurikuri"})
    fmt.Println(res) // {id:1 name:kuri}
}
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