{"id":10721,"date":"2022-11-23T16:34:11","date_gmt":"2022-11-23T08:34:11","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=10721"},"modified":"2023-06-30T17:57:36","modified_gmt":"2023-06-30T09:57:36","slug":"go-21","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2022\/11\/23\/go-21\/","title":{"rendered":"go\u5e38\u89c1\u95ee\u9898\u4e0e\u5904\u7406"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">1\u3001\u51fa\u73b0\u9519\u8bef\u5185\u5bb9reflect.Value.Set using unaddressable value\uff1f<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u822c\u51fa\u73b0 using unaddressable value \u9519\u8bef\uff0c\u8868\u793a\u4f20\u9012\u7684\u6307\u9488\u503c\u4e0d\u5bf9\uff0c\u6bd4\u5982\u9700\u8981\u4f20\u9012\u6307\u9488\u5730\u5740\u7684\uff0c\u4f46\u662f\u4f20\u4e86\u503c\uff0c\u6b64\u65f6\u53ef\u4ee5\u67e5\u770b\u4e0bgorm\u7684\u8bed\u53e5\u4e2dFind()\u4e2d\u7684\u503c\u662f\u5426\u4f20\u9012\u7684\u662f\u6307\u9488\uff0c\u5982\u679c\u4e0d\u662f\u6307\u9488\u5c31\u4f1a\u62a5\u9519<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">2\u3001go build\u7684\u6267\u884c\u6d41\u7a0b\u662f\u4ec0\u4e48\uff1f<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7go build -n \u6267\u884c\u4ee3\u7801(\u53ea\u6253\u5370\u8fc7\u7a0b\uff0c\u4e0d\u771f\u6b63\u6267\u884c)\uff0c\u6211\u4eec\u67e5\u770b\u4e0bgo build\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@VM-12-12-centos gong]# go build -n abc.go \n\n#\n# command-line-arguments\n#\n\nmkdir -p $WORK\/b001\/\ncat &gt;$WORK\/b001\/importcfg &lt;&lt; 'EOF' # internal\n# import config\npackagefile fmt=\/usr\/local\/go\/pkg\/linux_amd64\/fmt.a\npackagefile gong\/linshi=\/root\/.cache\/go-build\/f3\/f363fbc898cebc08cfae6d70ecf58677f7a2159812f31c28a5a507125a3643e8-d\npackagefile runtime=\/usr\/local\/go\/pkg\/linux_amd64\/runtime.a\nEOF\ncd \/data\/go\/src\/gong\n\/usr\/local\/go\/pkg\/tool\/linux_amd64\/compile -o $WORK\/b001\/_pkg_.a -trimpath \"$WORK\/b001=&gt;\" -p main -complete -buildid Tk7qQb47IMlDTHbYEzR2\/Tk7qQb47IMlDTHbYEzR2 -goversion go1.18.1 -c=2 -nolocalimports -importcfg $WORK\/b001\/importcfg -pack .\/abc.go\n\/usr\/local\/go\/pkg\/tool\/linux_amd64\/buildid -w $WORK\/b001\/_pkg_.a # internal\ncat &gt;$WORK\/b001\/importcfg.link &lt;&lt; 'EOF' # internal\npackagefile command-line-arguments=$WORK\/b001\/_pkg_.a\npackagefile fmt=\/usr\/local\/go\/pkg\/linux_amd64\/fmt.a\npackagefile gong\/linshi=\/root\/.cache\/go-build\/f3\/f363fbc898cebc08cfae6d70ecf58677f7a2159812f31c28a5a507125a3643e8-d\npackagefile runtime=\/usr\/local\/go\/pkg\/linux_amd64\/runtime.a\npackagefile errors=\/usr\/local\/go\/pkg\/linux_amd64\/errors.a\npackagefile internal\/fmtsort=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/fmtsort.a\npackagefile io=\/usr\/local\/go\/pkg\/linux_amd64\/io.a\npackagefile math=\/usr\/local\/go\/pkg\/linux_amd64\/math.a\npackagefile os=\/usr\/local\/go\/pkg\/linux_amd64\/os.a\npackagefile reflect=\/usr\/local\/go\/pkg\/linux_amd64\/reflect.a\npackagefile strconv=\/usr\/local\/go\/pkg\/linux_amd64\/strconv.a\npackagefile sync=\/usr\/local\/go\/pkg\/linux_amd64\/sync.a\npackagefile unicode\/utf8=\/usr\/local\/go\/pkg\/linux_amd64\/unicode\/utf8.a\npackagefile internal\/abi=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/abi.a\npackagefile internal\/bytealg=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/bytealg.a\npackagefile internal\/cpu=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/cpu.a\npackagefile internal\/goarch=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/goarch.a\npackagefile internal\/goexperiment=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/goexperiment.a\npackagefile internal\/goos=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/goos.a\npackagefile runtime\/internal\/atomic=\/usr\/local\/go\/pkg\/linux_amd64\/runtime\/internal\/atomic.a\npackagefile runtime\/internal\/math=\/usr\/local\/go\/pkg\/linux_amd64\/runtime\/internal\/math.a\npackagefile runtime\/internal\/sys=\/usr\/local\/go\/pkg\/linux_amd64\/runtime\/internal\/sys.a\npackagefile runtime\/internal\/syscall=\/usr\/local\/go\/pkg\/linux_amd64\/runtime\/internal\/syscall.a\npackagefile internal\/reflectlite=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/reflectlite.a\npackagefile sort=\/usr\/local\/go\/pkg\/linux_amd64\/sort.a\npackagefile math\/bits=\/usr\/local\/go\/pkg\/linux_amd64\/math\/bits.a\npackagefile internal\/itoa=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/itoa.a\npackagefile internal\/oserror=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/oserror.a\npackagefile internal\/poll=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/poll.a\npackagefile internal\/syscall\/execenv=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/syscall\/execenv.a\npackagefile internal\/syscall\/unix=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/syscall\/unix.a\npackagefile internal\/testlog=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/testlog.a\npackagefile internal\/unsafeheader=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/unsafeheader.a\npackagefile io\/fs=\/usr\/local\/go\/pkg\/linux_amd64\/io\/fs.a\npackagefile sync\/atomic=\/usr\/local\/go\/pkg\/linux_amd64\/sync\/atomic.a\npackagefile syscall=\/usr\/local\/go\/pkg\/linux_amd64\/syscall.a\npackagefile time=\/usr\/local\/go\/pkg\/linux_amd64\/time.a\npackagefile unicode=\/usr\/local\/go\/pkg\/linux_amd64\/unicode.a\npackagefile internal\/race=\/usr\/local\/go\/pkg\/linux_amd64\/internal\/race.a\npackagefile path=\/usr\/local\/go\/pkg\/linux_amd64\/path.a\nmodinfo \"0w\\xaf\\f\\x92t\\b\\x02A\\xe1\\xc1\\a\\xe6\\xd6\\x18\\xe6path\\tcommand-line-arguments\\ndep\\tgong\\t(devel)\\t\\nbuild\\t-compiler=gc\\nbuild\\tCGO_ENABLED=1\\nbuild\\tCGO_CFLAGS=\\nbuild\\tCGO_CPPFLAGS=\\nbuild\\tCGO_CXXFLAGS=\\nbuild\\tCGO_LDFLAGS=\\nbuild\\tGOARCH=amd64\\nbuild\\tGOOS=linux\\nbuild\\tGOAMD64=v1\\n\\xf92C1\\x86\\x18 r\\x00\\x82B\\x10A\\x16\\xd8\\xf2\"\nEOF\nmkdir -p $WORK\/b001\/exe\/\ncd .\n\/usr\/local\/go\/pkg\/tool\/linux_amd64\/link -o $WORK\/b001\/exe\/a.out -importcfg $WORK\/b001\/importcfg.link -buildmode=exe -buildid=_TzT60SqVuNzTxt-N6bw\/Tk7qQb47IMlDTHbYEzR2\/Tk7qQb47IMlDTHbYEzR2\/_TzT60SqVuNzTxt-N6bw -extld=gcc $WORK\/b001\/_pkg_.a\n\/usr\/local\/go\/pkg\/tool\/linux_amd64\/buildid -w $WORK\/b001\/exe\/a.out # internal\nmv $WORK\/b001\/exe\/a.out abc\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u9762\u770b\u51fa\uff0c\u6267\u884c\u6d41\u7a0b\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521b\u5efa\u4e34\u65f6\u76ee\u5f55\uff1amkdir -p $WORK\/b001\/<\/li>\n\n\n\n<li>\u67e5\u627e\u4f9d\u8d56\u4fe1\u606f\uff1acat &gt;$WORK\/b001\/importcfg &lt;&lt; &#8230;<\/li>\n\n\n\n<li>\u6267\u884c\u6e90\u4ee3\u7801\u7f16\u8bd1\uff1a\/usr\/local\/go\/pkg\/tool\/darwin_amd64\/compile &#8230;<\/li>\n\n\n\n<li>\u6536\u96c6\u94fe\u63a5\u5e93\u6587\u4ef6\uff1acat &gt;$WORK\/b001\/importcfg.link &lt;&lt; &#8230;<\/li>\n\n\n\n<li>\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1a\/usr\/local\/go\/pkg\/tool\/darwin_amd64\/link -o &#8230;<\/li>\n\n\n\n<li>\u79fb\u52a8\u53ef\u6267\u884c\u6587\u4ef6\uff0cmv $WORK\/b001\/exe\/a.out  abc<\/li>\n<\/ul>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">go run \u4e0ego build\u4e0d\u540c\u7684\u662f\uff0cgo run\u751f\u6210\u53ef\u6267\u884c\u7a0b\u5e8f\u540e\uff0c\u6ca1\u6709\u79fb\u52a8\uff0c\u800c\u662f\u76f4\u63a5\u6267\u884c\u4e86\u7a0b\u5e8f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3\u3001golang\u5728\u5c06json\u53cd\u5e8f\u5217\u5316\u5230\u7ed3\u6784\u4f53\u4e2d\u62a5\u9519invalid character &#8216;\\x00&#8217; after top-level value<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u56e0\uff1a\u7ed3\u6784\u4f53\u5207\u7247\u7684\u957f\u5ea6\u5927\u4e8e\u6211\u4eec\u5b9e\u9645\u8981\u5e8f\u5217\u5316\u6240\u9700\u8981\u7684\u957f\u5ea6\uff0c\u6bd4\u5982\u6211\u4eec\u5728\u901a\u8fc7bufio.NewReader\u8bfb\u53d6\u6587\u4ef6\u540e\uff0c\u5728\u901a\u8fc7Read(b []byte)\u5c06\u5185\u5bb9\u8bfb\u53d6\u5230\u5b57\u8282\u5207\u7247\u4e2db\u4e2d\uff0c\u6b64\u65f6\u6211\u4eec\u65e0\u6cd5\u9884\u6d4b\u6570\u636e\u6709\u591a\u5c11\uff0c\u56e0\u6b64\u5c31\u4f1a\u63d0\u524d\u521d\u59cb\u5316b\u8fd9\u4e2a\u5207\u7247\uff0cb := make([]byte,1024)\uff0c\u8fd9\u6761\u547d\u4ee4\u7ed9\u5207\u7247\u521d\u59cb\u7a7a\u95f4\u4e3a1024\u5b57\u8282\uff0c\u5982\u679c\u5b9e\u9645\u8bfb\u53d6\u5230\u5207\u7247\u4e2d\u7684\u53ea\u6709100\u5b57\u8282\uff0c\u90a3\u4e48\u6709\u76f8\u5f53\u4e00\u90e8\u5206\u6ca1\u6709\u4f7f\u7528\u5230\uff0c\u6b64\u65f6\u5982\u679c\u6211\u4eec\u5c06\u6b64\u5b57\u8282\u5207\u7247\u53cd\u5e8f\u5217\u5316\u5230\u7ed3\u6784\u4f53\u4e2d\u5c31\u4f1a\u62a5\u9519<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u4f8b\u5b50\u8868\u793a\u8bfb\u53d6\u672c\u5730file\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u5176\u53cd\u5e8f\u5217\u5316\u5230\u7ed3\u6784\u4f53\u4e2d\uff0c\u9996\u5148\u67e5\u770b\u6587\u4ef6\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"62\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/06\/5.png\" alt=\"\" class=\"wp-image-13643\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/06\/5.png 708w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/06\/5-300x26.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/06\/5-600x53.png 600w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bfb\u53d6\u6587\u4ef6\u7684\u811a\u672c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"bufio\"\n\t\"encoding\/json\"\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n)\n\ntype file struct {\n\tFileName string `json:\"filename\"`\n\tFileSize string `json:\"filesize\"`\n}\n\nfunc main() {\n\t\/\/\u6253\u5f00\u6587\u4ef6\n\tf, err := os.Open(\"file.txt\")\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t\/\/NewReader\u53c2\u6570\u4e3aio Reader\u63a5\u53e3,Open\u5b9e\u73b0\u4e86\u6b64\u63a5\u53e3\u56e0\u6b64\u53ef\u8d4b\u503c\n\tb := bufio.NewReader(f)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\tby := make(&#91;]byte, 100) \/\/\u521d\u59cb\u5316\u5b57\u8282\u5207\u7247\u5e76\u8bbe\u7f6e\u5bb9\u91cf\u4e3a100\n\ti, errs := b.Read(by)   \/\/\u5c06\u6570\u636e\u8bfb\u53d6\u5230\u5b57\u8282\u5207\u7247c\u4e2d\n\tif errs != nil {\n\t\tlog.Fatal(errs)\n\t}\n\tfmt.Println(\"\u8bfb\u5165\u5207\u7247\u4e2d\u7684\u5b57\u8282\u6570:\",i) \/\/44\n\tfile := &amp;file{}\n\terrs = json.Unmarshal(by&#91;:i], &amp;file)\n\tif errs != nil {\n\t\tlog.Fatal(errs)\n\t}\n\tfmt.Println(\"\u7ed3\u6784\u4f53\u5b57\u6bb5\u7684\u503c:\",file.FileName)\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"84\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/06\/6-1.png\" alt=\"\" class=\"wp-image-13644\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/06\/6-1.png 704w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/06\/6-1-300x36.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/06\/6-1-600x72.png 600w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u56fe\u770b\u51fa\uff0c\u5b9e\u9645\u5199\u5165\u5207\u7247\u4e2d\u7684\u5b57\u8282\u6570\u4e3a44\uff0c\u4f46\u662f\u5728\u521d\u59cb\u5316\u5207\u7247\u7684\u65f6\u5019\u7ed9\u7684\u5bb9\u91cf\u4e3a100\uff0c\u5b9e\u9645\u7a7a\u95f4\u8fc7\u591a\uff0c\u90a3\u4e48\u5728\u53cd\u5e8f\u5217\u5316\u7684\u65f6\u5019\uff0c\u5c31\u9700\u8981\u5148\u5c06\u591a\u4f59\u7684\u7a7a\u95f4\u6e05\u695a\uff0c\u56e0\u6b64\u901a\u8fc7by[:i]\u7684\u622a\u53d6\u5207\u7247\u65b9\u5f0f\u83b7\u53d6\u5b9e\u9645\u7684\u5207\u7247\u5927\u5c0f\u518d\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u5373\u53ef<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u51fa\u73b0\u9519\u8bef\u5185\u5bb9reflect.Value.Set using unaddressable value\uff1f \u4e00\u822c [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":10723,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111,23],"tags":[],"class_list":["post-10721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go","category-rong"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/10721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=10721"}],"version-history":[{"count":12,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/10721\/revisions"}],"predecessor-version":[{"id":13656,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/10721\/revisions\/13656"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/10723"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=10721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=10721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=10721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}