{"id":553151,"date":"2022-01-25T23:04:47","date_gmt":"2022-01-25T15:04:47","guid":{"rendered":"http:\/\/4563.org\/?p=553151"},"modified":"2022-01-25T23:04:47","modified_gmt":"2022-01-25T15:04:47","slug":"%e5%9c%a8-go-%e7%9a%84-struct-%e4%b8%ad%e4%bd%bf%e7%94%a8%e6%8c%87%e9%92%88%e7%9a%84%e5%9d%91-2","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=553151","title":{"rendered":"\u5728 Go \u7684 struct \u4e2d\u4f7f\u7528\u6307\u9488\u7684\u5751"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1> \u5728 Go \u7684 struct \u4e2d\u4f7f\u7528\u6307\u9488\u7684\u5751 <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : WadeLaunch <\/span>  <span><i><\/i> 27<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>go version: go1.17.3 linux\/amd64 <\/code><\/pre>\n<pre><code>package main  import (  \"fmt\" )  \/\/ \u6ce8\u610f Row \u7684\u5b57\u6bb5\u662f\u6307\u9488\u7c7b\u578b type Row struct {  Id   *int  Open *bool }  type Source struct {  Id   int  Open bool }  func main() {  sourceList := []Source{   {Id: 1, Open: true},   {Id: 2, Open: false},  }   var rows []Row  for _, v := range sourceList {   fmt.Println(\"sourceList.v:\")   fmt.Printf(\"      &amp;v.%%p: %pn\", &amp;v)   fmt.Println(\"         &amp;v:\", &amp;v)   fmt.Println(\"          v:\", v)   fmt.Println(\"      &amp;v.Id:\", &amp;v.Id)   fmt.Println(\"       v.Id:\", v.Id)   fmt.Println(\"    &amp;v.Open:\", &amp;v.Open)   fmt.Println(\"     v.Open:\", v.Open)    rows = append(rows, Row{Id: &amp;v.Id, Open: &amp;v.Open})  }   fmt.Println(\"nrows, len\", rows, len(rows))   for _, v := range rows {   fmt.Println(\"rows.v:\")   fmt.Printf(\"     &amp;v.%%p: %pn\", &amp;v)   fmt.Println(\"      v.Id:\", v.Id)   fmt.Println(\"     *v.Id:\", *v.Id)   fmt.Println(\"    v.Open:\", v.Open)   fmt.Println(\"   *v.Open:\", *v.Open)  } } <\/code><\/pre>\n<p>\u5148\u4e0d\u770b\u4e0b\u9762\u7684\u7b54\u6848\uff0c\u731c\u731c rows \u7684\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u5148\u4e0d\u770b\u4e0b\u9762\u7684\u7b54\u6848\uff0c\u731c\u731c rows \u7684\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u5148\u4e0d\u770b\u4e0b\u9762\u7684\u7b54\u6848\uff0c\u731c\u731c rows \u7684\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u7b54\u6848\u63ed\u6653\uff1a<\/p>\n<pre><code>sourceList.v:       &amp;v.%p: 0xc0000aa000          &amp;v: &amp;{1 true}           v: {1 true}       &amp;v.Id: 0xc0000aa000        v.Id: 1     &amp;v.Open: 0xc0000aa008      v.Open: true sourceList.v:       &amp;v.%p: 0xc0000aa000          &amp;v: &amp;{2 false}           v: {2 false}       &amp;v.Id: 0xc0000aa000        v.Id: 2     &amp;v.Open: 0xc0000aa008      v.Open: false  rows, len [{0xc0000aa000 0xc0000aa008} {0xc0000aa000 0xc0000aa008}] 2 rows.v:      &amp;v.%p: 0xc00008c240       v.Id: 0xc0000aa000      *v.Id: 2     v.Open: 0xc0000aa008    *v.Open: false rows.v:      &amp;v.%p: 0xc00008c240       v.Id: 0xc0000aa000      *v.Id: 2     v.Open: 0xc0000aa008    *v.Open: false <\/code><\/pre>\n<p>\u95ee\u9898\u4e00\uff1a\u4e3a\u4ec0\u4e48 rows \u4e24\u884c\u503c\u662f\u4e00\u6837\u7684\uff1f\uff08\u6211\u81ea\u5df1\u6709\u4e00\u4e2a\u7b54\u6848\uff0c\u53ef\u80fd\u662f\u9519\u7684\uff0c\u5148\u4e0d\u5199\u51fa\u6765\uff0c\u60f3\u5148\u95ee\u95ee\u5927\u5bb6\u7684\u770b\u6cd5\uff09<\/p>\n<p>\u95ee\u9898\u4e8c\uff1a\u521d\u5b66 Go \uff0c\u8bf7\u95ee\u5728 struct \u4e2d\u7528\u6307\u9488\u662f\u4e0d\u63a8\u8350\u7684\u7528\u6cd5\u5417\uff1f\u8fd8\u662f\u6211\u4e0d\u4f1a\u7528\uff1f<\/p>\n<p>\u8fd9\u4e2a\u7528\u6cd5\u662f\u5728\u67d0\u4e2a\u6846\u67b6\u4e2d\u770b\u5230\u7684\uff0c\u7528\u6307\u9488\u53ef\u80fd\u662f\u4e3a\u4e86\u901a\u8fc7 if Row.Id != nil \u6765\u533a\u5206\u8bf7\u6c42\u53c2\u6570\u4e0d\u5b58\u5728(domain\/path?name=x)\u4e0e\u8bf7\u6c42\u53c2\u6570\u7684\u503c\u662f 0 (domain\/path?name=x&amp;id=0) \u7684\u60c5\u51b5\u3002<\/p>\n<p>\u95ee\u9898\u4e09\uff1a\u600e\u4e48\u533a\u5206\u8fd9\u79cd\u53c2\u6570\u4e0d\u5b58\u5728\u4e0e\u53c2\u6570\u503c\u662f 0 \u7684\u60c5\u51b5\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>) <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"7144955\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6ca1\u6709\u7ec6\u770b\uff0c\u76f2\u731c\u662f\u5728\u8bf4 \u4f8b\u5982 `for _, v := range sourceList`\u7684 `v` \u5171\u7528\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u4e86<br \/>\u5176\u5b9e\u8fd9\u5728\u5b9e\u73b0\u4e0a\u662f\u5408\u7406\u7684\uff0c\u4e0d\u7136 for \u5faa\u73af\u4f1a\u5bfc\u81f4\u5f88\u591a\u7684\u5185\u5b58\u5206\u914d\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7144956\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : soap520 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> for i, v := range sourceList \u7684 v \u662f sourceList \u7684\u4e00\u4efd copy<br \/>\u4e5f\u8bb8\u4f60\u60f3\u5199\u6210 rows = append(rows, Row{Id: &amp;sourceList[i].Id, Open: &amp;sourceList[i].Open}) <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7144957\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : soap520 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @soap520 * v \u662f sourceList[i] \u7684\u4e00\u4efd copy <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7144958\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : tiedan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u95ee\u9898\u4e00\uff1a\u8fd9\u5c31\u548c\u5b9a\u4e49\u4e86 []*Source \uff0cfor _, v := range sourceList \u5f80\u91cc\u9762 append &amp;v \u662f\u4e00\u7c7b\u95ee\u9898\u3002<br \/>for range \u662f\u5c06\u5143\u7d20\u7684\u526f\u672c\u590d\u5236\u7ed9 v \uff08\u503c\u4f20\u9012\uff09\uff0c\u4f46\u5faa\u73af\u53d8\u91cf\u662f\u590d\u7528\u7684\uff0c\u5730\u5740\u4e0d\u4f1a\u53d8<br \/>\u95ee\u9898\u4e8c\uff1astruct \u4e2d\u7528\u6307\u9488\u9700\u8981\u533a\u5206\u60c5\u51b5\uff0c\u6bd4\u5982\u5224\u65ad\u53c2\u6570\u662f\u96f6\u503c\u8fd8\u662f\u6ca1\u4f20\u3002<br \/>\u95ee\u9898\u4e09\uff1a\u5c31\u662f\u7528\u6307\u9488\uff0cgin \u6846\u67b6\u7684 issue \u91cc\u9762\u6709\u63d0\u5230\uff0c\u7528\u6307\u9488\u6765\u533a\u5206 0 \u503c\u548c\u53c2\u6570\u4e0d\u5b58\u5728 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7144959\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fgwmlhdkkkw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u662f\u4f60 for \u7684\u95ee\u9898\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7144960\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : WadeLaunch <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div>  @zzn @tiedan \u6211\u60f3\u7684\u4e5f\u662f\u5faa\u73af\u65f6 v \u5206\u914d\u5230\u4e86\u540c\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u5bfc\u81f4\u7684\u3002<br \/>@fgwmlhdkkkw \u8bf7\u95ee\u8fd9\u79cd\u60c5\u51b5\u8981\u600e\u4e48 \u201cfor\u201d \u624d\u4e0d\u4f1a\u5206\u914d\u5230\u4e86\u540c\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\uff1f<br \/>\u6211\u60f3\u5230\u7684\u5c31\u662f\u628a v \u8d4b\u503c\u5230\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u518d append \uff0c\u4f8b\u5982\uff1a<br \/>&#8220;`<br \/>vv := v<br \/>rows = append(rows, Row{Id: &amp;vv.Id, Open: &amp;vv.Open})<br \/>&#8220;`<br \/>\u8bf7\u95ee\u6709\u5176\u5b83\u65b9\u6cd5\u5417\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"7144961\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : djFFFFF <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> for _, v := range sourceList {<br \/>\u6539\u6210<br \/>for i := range sourceList {<br \/>\u7136\u540e\u4e4b\u524d\u7528 v \u7684\u5730\u65b9\u6539\u6210\u7528 sourceList[i]<br \/>\u5c31\u884c\u4e86\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u7684 struct \u4e2d\u4f7f\u7528&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/553151"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=553151"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/553151\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=553151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=553151"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=553151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}