\u6253\u5370\u590d\u6742\u7ed3\u6784\u4f53<\/strong><\/div>\n\u5f53\u7ed3\u6784\u4f53\u4e2d\u7684\u5b57\u6bb5\u662f\u6307\u9488\u7c7b\u578b\u65f6\uff0c\u7528\u5360\u4f4d\u7b26\u76f4\u63a5\u6253\u5370\u51fa\u6765\u7684\u662f\u600e\u6837\u7684\u5462\uff1f<\/p>\n
\u8fd8\u662f\u4ee5\u524d\u9762\u7684\u4f8b\u5b50\u4e3a\u57fa\u7840\uff0c\u6211\u4eec\u7ed9\u201c\u5f20\u4e09\u201d\u52a0\u4e00\u6761\u72d7\uff0c\u5176\u4e2d User \u7ed3\u6784\u4f53\u4e2d\u5f15\u5165\u7684 Dog \u662f\u6307\u9488\u7c7b\u578b\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
type Dog struct {\r\n Name string\r\n Age int\r\n}\r\ntype User struct {\r\n Name string\r\n Age int\r\n Dog *Dog\r\n}\r\nfunc main() {\r\n dog := Dog{\r\n Name: \"\u65fa\u8d22\",\r\n Age: 2,\r\n }\r\n user := User{\r\n Name: \"\u5f20\u4e09\",\r\n Age: 95,\r\n Dog: &dog,\r\n }\r\n fmt.Println(user)\r\n fmt.Printf(\"%v\\n\", user)\r\n fmt.Printf(\"%+v\\n\", user)\r\n fmt.Printf(\"%#v\\n\", user)\r\n}<\/pre>\n\u8fd9\u65f6\u8fd8\u80fd\u628a\u6240\u6709\u503c\u6253\u5370\u51fa\u6765\u5417\uff1f<\/p>\n
{\u5f20\u4e09 95 0xc000004078} \r\n{Name:\u5f20\u4e09 Age:95 Dog:0xc000004078} \r\nmain.User{Name:\"\u5f20\u4e09\", Age:95, Dog:(*main.Dog)(0xc000004078)}<\/pre>\n\u8fd9\u65f6\u53ef\u4ee5\u770b\u5230Dog\u5b57\u6bb5\u6253\u5370\u7684\u4e0d\u662fDog\u7ed3\u6784\u4f53\u5185\u90e8\u7684\u503c\uff0c\u800c\u662f\u4e00\u4e2a\u5730\u5740\u503c\u3002\u5f88\u663e\u7136\uff0c\u8fd9\u4e2a\u4e0d\u662f\u6211\u4eec\u9700\u8981\u5728\u65e5\u5fd7\u4e2d\u770b\u5230\u7684\uff0c\u6211\u4eec\u9700\u8981\u770b\u7684\u662f\u7ed3\u6784\u4f53\u5177\u4f53\u7684\u503c\uff0c\u90a3\u8fd9\u4e2a\u503c\u53c8\u600e\u4e48\u6253\u5370\u5462\uff1f<\/p>\n
\u65b9\u6848\u4e00<\/strong><\/div>\n\u5b9e\u73b0 String() \u6216GoString() \u65b9\u6cd5<\/p>\n
Golang \u4e2d\u7684 fmt \u5305\u4e2d\u6709\u4e00\u4e2a Stringer \u63a5\u53e3\uff0c\u63a5\u53e3\u4e2d\u53ea\u6709\u4e00\u4e2a String() \u65b9\u6cd5<\/p>\n
\/\/ Stringer is implemented by any value that has a String method,\r\n\/\/ which defines the ``native'' format for that value.\r\n\/\/ The String method is used to print values passed as an operand\r\n\/\/ to any format that accepts a string or to an unformatted printer\r\n\/\/ such as Print.\r\ntype Stringer interface {\r\n String() string\r\n}<\/pre>\n\u6211\u4eec\u53ef\u4ee5\u8ba9 User \u548c Dog \u7ed3\u6784\u4f53\u5206\u522b\u5b9e\u73b0 String() \u65b9\u6cd5\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7c7b\u4f3c\u4e8e Java \u4e2d\u7684 toString() \u65b9\u6cd5\u3002\u57fa\u4e8e\u524d\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u589e\u52a0\u5982\u4e0b String() \u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n
func (d *Dog) String() string {\r\n return \"{\\\"name\" + \"\\\": \\\"\" + d.Name + \"\\\",\" + \"\\\"\" + \"age\\\": \\\"\" + strconv.Itoa(d.Age) + \"\\\"}\"\r\n}\r\nfunc (u *User) String() string {\r\n return \"{\\\"name\" + \"\\\": \\\"\" + u.Name + \"\\\", \\\"\" + \"age\\\": \\\"\" + strconv.Itoa(u.Age) + \"\\\", \\\"dog\\\": \" + u.Dog.String() + \"}\"\r\n}<\/pre>\n\u8fd0\u884c\u540e\uff0c\u6253\u5370\u7684\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
{\u5f20\u4e09 95 {\"name\": \"\u65fa\u8d22\",\"age\": \"2\"}} \r\n{Name:\u5f20\u4e09 Age:95 Dog:{\"name\": \"\u65fa\u8d22\",\"age\": \"2\"}} \r\nmain.User{Name:\"\u5f20\u4e09\", Age:95, Dog:(*main.Dog)(0xc000004078)}<\/pre>\n\u53d1\u73b0\uff0c\u5b9e\u73b0 String() \u65b9\u6cd5\u53ea\u5bf9 %v \u548c %+v \u5360\u4f4d\u7b26\u6709\u6548\uff0c\u5bf9\u4e8e%#v \u5360\u4f4d\u7b26\uff0c\u5176\u6253\u5370\u7684\u7ed3\u6784\u4f53\u6307\u9488\u7c7b\u578b\u8fd8\u662f\u4e00\u4e2a\u5730\u5740\u503c\u3002<\/p>\n
\u5176\u5b9e\u5728 fmt \u5305\u4e2d\uff0cStringer \u63a5\u53e3 \u4e0b\u9762\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u770b\u5230\u53e6\u5916\u4e00\u4e2a GoStringer \u63a5\u53e3\uff1a<\/p>\n
\/\/ GoStringer is implemented by any value that has a GoString method,\r\n\/\/ which defines the Go syntax for that value.\r\n\/\/ The GoString method is used to print values passed as an operand\r\n\/\/ to a %#v format.\r\ntype GoStringer interface {\r\n GoString() string\r\n}\r\nThe GoString method is used to print values passed as an operand to a %#v format. (GoString \u65b9\u6cd5\u7528\u4e8e\u6253\u5370\u4f5c\u4e3a\u64cd\u4f5c\u6570\u4f20\u9012\u7ed9 %#v \u683c\u5f0f\u7684\u503c)<\/pre>\n\u627e\u5230\u4e86\uff0c\u6211\u4eec\u518d\u5b9e\u73b0 GoString() \u65b9\u6cd5\uff0c\u5c31\u53ef\u4ee5\u7528 %#v \u5360\u4f4d\u7b26\u6253\u5370\u7ed3\u6784\u4f53\u6307\u9488\u7c7b\u578b\u4e2d\u7684\u503c\u4e86\u3002<\/p>\n
\u57fa\u4e8e\u4e4b\u524d\u4ee3\u7801\u589e\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n
func (d *Dog) GoString() string {\r\n return \"{\\\"name\" + \"\\\": \\\"\" + d.Name + \"\\\",\" + \"\\\"\" + \"age\\\": \\\"\" + strconv.Itoa(d.Age) + \"\\\"}\"\r\n}\r\nfunc (u *User) GoString() string {\r\n return \"{\\\"name\" + \"\\\": \\\"\" + u.Name + \"\\\", \\\"\" + \"age\\\": \\\"\" + strconv.Itoa(u.Age) + \"\\\", \\\"dog\\\": \" + u.Dog.String() + \"}\"\r\n}<\/pre>\n\u8fd0\u884c\u540e\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff0c\u8fd9\u4e0b\u5b50\u5c31\u90fd\u53ef\u4ee5\u6253\u5370\u4e86\uff1a<\/p>\n
{\u5f20\u4e09 95 {\"name\": \"\u65fa\u8d22\",\"age\": \"2\"}} \r\n{Name:\u5f20\u4e09 Age:95 Dog:{\"name\": \"\u65fa\u8d22\",\"age\": \"2\"}} \r\nmain.User{Name:\"\u5f20\u4e09\", Age:95, Dog:{\"name\": \"\u65fa\u8d22\",\"age\": \"2\"}}<\/pre>\n\u5230\u8fd9\u91cc\uff0c\u6211\u611f\u89c9\u8fd9\u79cd\u65b9\u6848\u6709\u70b9\u9ebb\u70e6\u5462\uff0c\u8fd8\u6709\u6ca1\u6709\u5176\u4ed6\u4e0d\u7528\u7ef4\u62a4 String() \u6216 GoString() \u7684\u65b9\u6cd5\u5462\uff1f<\/p>\n
\u65b9\u6848\u4e8c<\/strong><\/div>\n\u8f6c\u6362\u6210 json \u683c\u5f0f<\/p>\n
func main() {\r\n dog := Dog{\r\n Name: \"\u65fa\u8d22\",\r\n Age: 2,\r\n }\r\n user := User{\r\n Name: \"\u5f20\u4e09\",\r\n Age: 95,\r\n Dog: &dog,\r\n }\r\n byteUser, _ := json.Marshal(&user)\r\n fmt.Println(string(byteUser))\r\n}<\/pre>\n\u6253\u5370\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff0c\u5982\u679c\u4f7f\u7528 json \u5e93\u7684\u8bdd\uff0c\u662f\u53ef\u4ee5\u76f4\u63a5\u628a\u7ed3\u6784\u4f53\u6307\u9488\u7c7b\u578b\u7684\u5177\u4f53\u503c\u90fd\u6253\u5370\u51fa\u6765\u7684\uff0c\u6bd4\u8f83\u65b9\u4fbf\uff1a<\/p>\n
{\"Name\":\"\u5f20\u4e09\",\"Age\":95,\"Dog\":{\"Name\":\"\u65fa\u8d22\",\"Age\":2}}<\/pre>\n\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eGolang\u6253\u5370\u590d\u6742\u7ed3\u6784\u4f53\u4e24\u79cd\u65b9\u6cd5\u8be6\u89e3\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728Golang\u4e2d\u6709\u539f\u751f\u7684 fmt \u683c\u5f0f\u5316\u5de5\u5177\u53bb\u6253\u5370\u7ed3\u6784\u4f53\uff0c\u53ef\u4ee5\u901a\u8fc7\u5360\u4f4d\u7b26%v\u3001%+v\u3001%#v\u53bb\u5b9e\u73b0\uff0c\u8fd93\u79cd\u7684\u533a […]<\/p>\n","protected":false},"author":1479,"featured_media":234173,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-262912","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=262912"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262912\/revisions"}],"predecessor-version":[{"id":263117,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262912\/revisions\/263117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/234173"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=262912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=262912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=262912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}