{"id":417,"date":"2024-12-21T14:32:40","date_gmt":"2024-12-21T06:32:40","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=417"},"modified":"2024-12-21T14:32:40","modified_gmt":"2024-12-21T06:32:40","slug":"go%e4%b8%ad%e5%a6%82%e4%bd%95%e6%9c%89%e6%95%88%e5%9c%b0%e5%b0%86%e6%95%b4%e6%95%b0%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/21\/go%e4%b8%ad%e5%a6%82%e4%bd%95%e6%9c%89%e6%95%88%e5%9c%b0%e5%b0%86%e6%95%b4%e6%95%b0%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84%ef%bc%9f\/","title":{"rendered":"Go\u4e2d\u5982\u4f55\u6709\u6548\u5730\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u3001\u6587\u4ef6\u5904\u7406\u6216\u52a0\u5bc6\u7b49\u573a\u666f\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u3001\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u4f7f\u7528 <code>encoding\/binary<\/code> \u5305<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 <code>encoding\/binary<\/code> \u5305\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u5b57\u8282\u5e8f\u5217\u548c\u57fa\u7840\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u7cfb\u7edf\u7684\u5b57\u8282\u5e8f\uff08\u5927\u7aef\u6216\u5c0f\u7aef\uff09\u9009\u62e9\u6b63\u786e\u7684\u5b57\u8282\u987a\u5e8f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"bytes\"\n    \"encoding\/binary\"\n    \"fmt\"\n)\n\nfunc intToBytes(n int) &#91;]byte {\n    buf := new(bytes.Buffer)\n    err := binary.Write(buf, binary.LittleEndian, int32(n)) \/\/ \u5c0f\u7aef\u5b57\u8282\u5e8f\n    if err != nil {\n        fmt.Println(\"binary.Write failed:\", err)\n    }\n    return buf.Bytes()\n}\n\nfunc main() {\n    n := 12345\n    b := intToBytes(n)\n    fmt.Printf(\"Integer: %d, Bytes: %v\\n\", n, b)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>binary.Write<\/code> \u5c06\u6574\u6570\u5199\u5165 <code>bytes.Buffer<\/code> \u4e2d\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u9009\u62e9 <code>binary.LittleEndian<\/code> \u6216 <code>binary.BigEndian<\/code> \u6765\u63a7\u5236\u5b57\u8282\u5e8f\u3002<code>LittleEndian<\/code> \u662f\u9ed8\u8ba4\u7684\uff0c\u610f\u5473\u7740\u6700\u4f4e\u4f4d\u5b57\u8282\u5728\u524d\u3002<\/li>\n\n\n\n<li><code>int32<\/code> \u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a 4 \u5b57\u8282\u6570\u7ec4\uff08\u6839\u636e\u5b9e\u9645\u9700\u8981\u9009\u62e9\u5927\u5c0f\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>unsafe<\/code> \u5305\u8fdb\u884c\u65e0\u590d\u5236\u8f6c\u6362\uff08\u4ec5\u9002\u7528\u4e8e\u7279\u5b9a\u573a\u666f\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go \u7684 <code>unsafe<\/code> \u5305\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8f6c\u6362\u6765\u83b7\u53d6\u5185\u5b58\u4e2d\u7684\u539f\u59cb\u5b57\u8282\u8868\u793a\u3002\u6b64\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u6027\u80fd\u4f18\u5316\uff0c\u5e76\u4e14\u5177\u6709\u4e00\u4e9b\u6f5c\u5728\u7684\u5b89\u5168\u95ee\u9898\uff0c\u56e0\u6b64\u9700\u8981\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"unsafe\"\n)\n\nfunc intToBytesUnsafe(n int) &#91;]byte {\n    \/\/ \u5c06 int \u8f6c\u6362\u4e3a &#91;]byte\n    return *(*&#91;]byte)(unsafe.Pointer(&amp;struct {\n        _  &#91;unsafe.Sizeof(int(0))]byte\n        p *int\n    }{p: &amp;n}))\n}\n\nfunc main() {\n    n := 12345\n    b := intToBytesUnsafe(n)\n    fmt.Printf(\"Integer: %d, Bytes: %v\\n\", n, b)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>unsafe.Pointer<\/code> \u6765\u76f4\u63a5\u64cd\u4f5c\u5185\u5b58\u3002<\/li>\n\n\n\n<li>\u8fd9\u4e2a\u65b9\u6cd5\u662f\u975e\u5e38\u9ad8\u6548\u7684\uff0c\u4f46\u53ea\u9002\u7528\u4e8e\u5bf9\u6027\u80fd\u8981\u6c42\u6781\u9ad8\u7684\u573a\u666f\u3002\u5b83\u7ed5\u8fc7\u4e86 Go \u7684\u5185\u5b58\u5b89\u5168\u673a\u5236\uff0c\u56e0\u6b64\u9700\u8981\u975e\u5e38\u5c0f\u5fc3\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u4f7f\u7528 <code>strconv<\/code> \u5305\u8fdb\u884c\u7b80\u5355\u7684\u6570\u5b57\u8f6c\u5b57\u7b26\u518d\u8f6c\u5b57\u8282\uff08\u4ec5\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u8868\u793a\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u53ea\u9700\u8981\u5c06\u6574\u6570\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>strconv.Itoa<\/code> \u6216 <code>strconv.FormatInt<\/code>\uff0c\u7136\u540e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5207\u7247\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"strconv\"\n)\n\nfunc intToBytesStr(n int) &#91;]byte {\n    \/\/ \u5c06\u6574\u6570\u8f6c\u4e3a\u5b57\u7b26\u4e32\n    str := strconv.Itoa(n)\n    \/\/ \u8fd4\u56de\u5b57\u7b26\u4e32\u5bf9\u5e94\u7684\u5b57\u8282\u6570\u7ec4\n    return &#91;]byte(str)\n}\n\nfunc main() {\n    n := 12345\n    b := intToBytesStr(n)\n    fmt.Printf(\"Integer: %d, Bytes: %v\\n\", n, b)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u79cd\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5b57\u8282\u6570\u7ec4\u3002<\/li>\n\n\n\n<li>\u4e0e <code>binary<\/code> \u5305\u65b9\u6cd5\u4e0d\u540c\uff0c\u5b83\u4e0d\u4f1a\u8003\u8651\u5b57\u8282\u5e8f\uff0c\u4e14\u8f93\u51fa\u7684\u5b57\u8282\u8868\u793a\u4e3a\u6570\u5b57\u5b57\u7b26\uff08\u4f8b\u5982 <code>12345<\/code> \u4f1a\u53d8\u6210 <code>[]byte{'1', '2', '3', '4', '5'}<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u4f7f\u7528\u4f4d\u64cd\u4f5c\u548c\u4f4d\u79fb\uff08\u9002\u7528\u4e8e\u7b80\u5355\u7c7b\u578b\u8f6c\u6362\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u56fa\u5b9a\u5927\u5c0f\u7684\u6574\u6570\u7c7b\u578b\uff08\u5982 <code>int32<\/code> \u6216 <code>int64<\/code>\uff09\uff0c\u53ef\u4ee5\u624b\u52a8\u5c06\u5176\u62c6\u89e3\u4e3a\u5b57\u8282\u5e76\u5b58\u50a8\u5230\u5b57\u8282\u6570\u7ec4\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u624b\u52a8\u5904\u7406\u5b57\u8282\u5e8f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc intToBytesManual(n int) &#91;]byte {\n    \/\/ \u624b\u52a8\u5904\u7406\u5b57\u8282\u5e8f\u5217 (LittleEndian) \u4f8b\u5b50\n    b := make(&#91;]byte, 4) \/\/ \u5047\u8bbe\u662f int32\uff0c4 \u5b57\u8282\n    b&#91;0] = byte(n)\n    b&#91;1] = byte(n &gt;&gt; 8)\n    b&#91;2] = byte(n &gt;&gt; 16)\n    b&#91;3] = byte(n &gt;&gt; 24)\n    return b\n}\n\nfunc main() {\n    n := 12345\n    b := intToBytesManual(n)\n    fmt.Printf(\"Integer: %d, Bytes: %v\\n\", n, b)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7\u4f4d\u79fb\u64cd\u4f5c\uff0c\u5c06\u6574\u6570\u62c6\u89e3\u4e3a\u5404\u4e2a\u5b57\u8282\u5e76\u586b\u5145\u5230\u5b57\u8282\u6570\u7ec4\u4e2d\u3002<\/li>\n\n\n\n<li><code>n >> 8<\/code> \u8868\u793a\u5c06\u6574\u6570\u53f3\u79fb 8 \u4f4d\uff0c\u83b7\u53d6\u4e0b\u4e00\u4e2a\u5b57\u8282\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4f60\u9700\u8981\u9ad8\u6548\u4e14\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\uff0c\u63a8\u8350\u4f7f\u7528 <code>encoding\/binary<\/code> \u5305\uff0c\u5b83\u63d0\u4f9b\u4e86\u6e05\u6670\u4e14\u5b89\u5168\u7684\u5b57\u8282\u5e8f\u5217\u5904\u7406\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6027\u80fd\u662f\u5173\u952e\uff0c\u4e14\u53ef\u4ee5\u63a5\u53d7\u66f4\u590d\u6742\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 <code>unsafe<\/code> \u5305\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4f60\u53ea\u9700\u8981\u5904\u7406\u6574\u6570\u7684\u5b57\u7b26\u8868\u793a\uff0c\u4f7f\u7528 <code>strconv<\/code> \u5305\u5c06\u6574\u6570\u8f6c\u4e3a\u5b57\u7b26\u4e32\u518d\u8f6c\u4e3a\u5b57\u8282\u6570\u7ec4\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u7279\u5b9a\u573a\u666f\uff0c\u624b\u52a8\u5904\u7406\u4f4d\u79fb\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u505a\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u9009\u62e9\u5177\u4f53\u7684\u65b9\u5f0f\u5e94\u6839\u636e\u4f60\u7684\u9700\u6c42\uff0c\u6bd4\u5982\u662f\u5426\u9700\u8981\u8003\u8651\u5b57\u8282\u5e8f\u3001\u662f\u5426\u9700\u8981\u8de8\u5e73\u53f0\u517c\u5bb9\u7b49\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u901a\u5e38\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u3001\u6587\u4ef6\u5904\u7406\u6216\u52a0\u5bc6\u7b49\u573a\u666f\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-417","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":418,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/417\/revisions\/418"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}