Go 문자열 포인터 Marshal

1 개요[ | ]

Go 문자열 포인터 Marshal
package main

import (
	"encoding/json"
	"fmt"
)

type MyObject struct {
	ID          int     `json:"id"`
	Name        string  `json:"name"`
	DisplayName *string `json:"displayName"`
}

func main() {
	displayName := "hello everyone"
	myObject1 := MyObject{ID: 1, Name: "hello", DisplayName: &displayName}
	myObject2 := MyObject{ID: 2, Name: "world", DisplayName: nil}

	fmt.Printf("%+v\n", myObject1) // {ID:1 Name:hello DisplayName:0xc000010350}
	fmt.Printf("%+v\n", myObject2) // {ID:0 Name: DisplayName:<nil>}

	bytes1, _ := json.Marshal(myObject1)
	bytes2, _ := json.Marshal(myObject2)

	fmt.Println(string(bytes1)) // {"id":1,"name":"hello","displayName":"hello everyone"}
	fmt.Println(string(bytes2)) // {"id":2,"name":"world","displayName":null}
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}