package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string
Age int
}
func main() {
myStruct := Person{"dvwy", 145}
fmt.Println("=======")
fmt.Println(myStruct)
fmt.Println(prettyPrint(myStruct))
}
func prettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
결과는 다음과 같습니다.
{dvwy 145}
{
"Name": "dvwy",
"Age": 145
}
출처 : https://stackoverflow.com/a/51270134
How to print struct variables in console?
How can I print (to the console) the Id, Title, Name, etc. of this struct in Golang? type Project struct { Id int64 `json:"project_id"` Title string `json:"title&qu...
stackoverflow.com
반응형
'Programming Language > golang' 카테고리의 다른 글
golang prviate repository에서 디펜던시 가져오는 방법 (0) | 2023.03.06 |
---|---|
goroutine 함수 여러번 실행 결과값 기다리는 2가지 방법 - js callback 처럼 (1) | 2023.03.03 |
go gin framework graceful shutdown 예제 (0) | 2023.03.03 |
intellij에서 golang 프로젝트 인식이 잘 안될때 (0) | 2023.02.22 |
golang 동시성 예제 (0) | 2021.03.22 |
Golang backend programming - Http get 호출 + timeout 처리 (0) | 2018.05.20 |