package main
import (
"encoding/json"
"fmt"
)
type Task struct {
Id int32 `json:"id"` // `(...)`内にkey名を指定する
Name string `json:"name"`
}
func main() {
task := &Task{100, "Task name"}
bytes, _ := json.Marshal(task) // jsonのバイナリが出力される
fmt.Println(string(bytes)) // stringにキャストして標準出力に表示
}
// => {"id":100,"name":"Task name"}
More than 5 years have passed since last update.
GolangでstructからJSONに変換して標準出力するサンプルコード
Last updated at Posted at 2019-09-28
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme