4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GolangでstructからJSONに変換して標準出力するサンプルコード

Last updated at Posted at 2019-09-28
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"}
4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?