{"id":207546,"date":"2025-07-08T09:51:33","date_gmt":"2025-07-08T01:51:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207546\/"},"modified":"2025-07-08T09:51:33","modified_gmt":"2025-07-08T01:51:33","slug":"%e5%b0%86-go-%e7%9a%84-putuint16-%e8%bd%ac%e6%8d%a2%e4%b8%ba-python","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207546\/","title":{"rendered":"\u5c06 Go \u7684 PutUint16 \u8f6c\u6362\u4e3a Python"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5c06 Go \u7684 PutUint16 \u8f6c\u6362\u4e3a Python<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 15:21:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5c06 Go \u7684 PutUint16 \u8f6c\u6362\u4e3a Python\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u83b7\u5f97\u4e0e\u4e0b\u9762\u7ed9\u51fa\u7684 python \u4e2d\u7684 go \u4ee3\u7801\u7b49\u6548\u7684\u5185\u5bb9\uff1a<\/p>\n<pre>func make(op opcode, operands ...int) []byte {\n    def, ok := definitions[op]\n    if !ok {\n        return []byte{}\n    }\n\n    instructionlen := 1\n    for _, w := range def.operandwidths {\n        instructionlen += w\n    }\n\n    instruction := make([]byte, instructionlen)\n    instruction[0] = byte(op)\n\n    offset := 1\n    for i, o := range operands {\n        width := def.operandwidths[i]\n        switch width {\n        case 2:\n            binary.bigendian.putuint16(instruction[offset:], uint16(o))\n        case 1:\n            instruction[offset] = byte(o)\n        }\n        offset += width\n    }\n\n    return instruction\n}\n\nfunc readoperands(def *definition, ins instructions) ([]int, int) {\n    operands := make([]int, len(def.operandwidths))\n    offset := 0\n\n    for i, width := range def.operandwidths {\n        switch width {\n        case 2:\n            operands[i] = int(readuint16(ins[offset:]))\n        case 1:\n            operands[i] = int(readuint8(ins[offset:]))\n        }\n\n        offset += width\n    }\n\n    return operands, offset\n}<\/pre>\n<p>\u4e0a\u9762\u7684 <code>op<\/code> \u662f\u4ee5\u4e0b\u4efb\u610f\u4e00\u4e2a\uff1a<\/p>\n<pre>type Opcode byte\n\nconst (\n    OpConstant Opcode = iota\n\n    OpAdd\n\n    OpPop\n\n    OpSub\n    OpMul\n    OpDiv\n)<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6765\u81ea\u300awriting a compiler in go\u300b\u4e00\u4e66\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230<\/p>\n<p>\u6211\u4e0d\u592a\u786e\u5b9a\u5b57\u8282\u8f6c\u6362\u548c\u6253\u5305\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48\uff0c\u4f46\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5b83\uff0c\u6211\u7528 python \u7f16\u5199\u4e86\u6574\u4e2a\u8fc7\u7a0b\u3002\u6709\u4eba\u53ef\u4ee5\u5e2e\u6211\u7528 python \u7ffb\u8bd1\u8fd9\u4e24\u4e2a\u51fd\u6570\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u6574\u6570\u7684\u65b9\u6cd5\u3002 <code>o.to_bytes(2, byteorder='big')<\/code> \u5c06\u63d0\u4f9b\u4e0e <code>PutUint16<\/code> \u76f8\u540c\u7684\u6548\u679c\u3002\u540c\u6837\uff0c \u4e5f\u53ef\u7528\u4e8e\u8bfb\u53d6\u3002\u8fd8\u6709 \u4ee5\u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u5904\u7406\u7c7b\u4f3c\u7684\u4e8b\u60c5\u3002<\/p>\n<p>\u4e0e Go \u4ee3\u7801\u4e2d\u90a3\u6837\u6784\u5efa\u7f13\u51b2\u533a\u5e76\u5199\u5165\u504f\u79fb\u91cf\u4e0d\u540c\uff0c\u7b80\u5355\u5730\u4f7f\u7528 <code>+<\/code> \u9644\u52a0\u5230\u4ee5\u7a7a\u5f00\u5934\u7684 <code>bytes<\/code> \u66f4\u6709\u610f\u4e49\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5c06 Go \u7684 PutUint16 \u8f6c\u6362\u4e3a Python\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207546","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207546"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207546\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}