{"id":207524,"date":"2025-07-08T08:49:27","date_gmt":"2025-07-08T00:49:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207524\/"},"modified":"2025-07-08T08:49:27","modified_gmt":"2025-07-08T00:49:27","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e4%bb%bb%e4%bd%95%e7%b1%bb%e5%9e%8b%e7%9a%84%e6%95%b0%e6%8d%ae%e5%80%bc%e4%bc%a0%e9%80%92%e5%88%b0-golang-%e4%b8%ad%e7%9a%84-grpc-protobuf-%e7%bb%93%e6%9e%84%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207524\/","title":{"rendered":"\u5982\u4f55\u5c06\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u503c\u4f20\u9012\u5230 Golang \u4e2d\u7684 GRPC Protobuf \u7ed3\u6784\u4e2d\uff1f"},"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>\u5982\u4f55\u5c06\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u503c\u4f20\u9012\u5230 Golang \u4e2d\u7684 GRPC Protobuf \u7ed3\u6784\u4e2d\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 12:12:40<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u503c\u4f20\u9012\u5230 Golang \u4e2d\u7684 GRPC Protobuf \u7ed3\u6784\u4e2d\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u901a\u8fc7 tonutils-go \u89e3\u6790 adnl \u5e93\u4e2d\u7684\u6570\u636e\u5e76\u901a\u8fc7 grpc \u4f20\u9012\u6570\u636e\u3002<\/p>\n<p>\u6211\u9700\u8981\u7684\u6570\u636e\u7ed3\u6784\u4e3a<\/p>\n<pre>\/\/ struct from tonutils-go\ntype transaction struct {\n    _           magic         `tlb:\"$0111\"`\n    accountaddr []byte        `tlb:\"bits 256\"`\n    lt          uint64        `tlb:\"## 64\"`\n    prevtxhash  []byte        `tlb:\"bits 256\"`\n    prevtxlt    uint64        `tlb:\"## 64\"`\n    now         uint32        `tlb:\"## 32\"`\n    outmsgcount uint16        `tlb:\"## 15\"`\n    origstatus  accountstatus `tlb:\".\"`\n    endstatus   accountstatus `tlb:\".\"`\n    io          struct {\n        in  *message      `tlb:\"maybe ^\"`\n        out *messageslist `tlb:\"maybe ^\"`\n    } `tlb:\"^\"`\n    totalfees   currencycollection     `tlb:\".\"`\n    stateupdate hashupdate             `tlb:\"^\"` \/\/ of account\n    description transactiondescription `tlb:\"^\"`\n\n    \/\/ not in scheme, but will be filled based on request data for flexibility\n    hash []byte `tlb:\"-\"`\n}\n\ntype transactiondescription struct {\n    description any `tlb:\".\"`\n}\n\n\ntype transactiondescriptionordinary struct {\n    _            magic         `tlb:\"$0000\"`\n    creditfirst  bool          `tlb:\"bool\"`\n    storagephase *storagephase `tlb:\"maybe .\"`\n    creditphase  *creditphase  `tlb:\"maybe .\"`\n    computephase computephase  `tlb:\".\"`\n    actionphase  *actionphase  `tlb:\"maybe ^\"`\n    aborted      bool          `tlb:\"bool\"`\n    bouncephase  *bouncephase  `tlb:\"maybe .\"`\n    destroyed    bool          `tlb:\"bool\"`\n}\n\ntype computephase struct {\n    phase any `tlb:\".\"`\n}\n\n\ntype computephasevm struct {\n    _                magic `tlb:\"$1\"`\n    success          bool  `tlb:\"bool\"`\n    msgstateused     bool  `tlb:\"bool\"`\n    accountactivated bool  `tlb:\"bool\"`\n    gasfees          coins `tlb:\".\"`\n    details          struct {\n        gasused          *big.int `tlb:\"var uint 7\"`\n        gaslimit         *big.int `tlb:\"var uint 7\"`\n        gascredit        *big.int `tlb:\"maybe var uint 3\"`\n        mode             int8     `tlb:\"## 8\"`\n        exitcode         int32    `tlb:\"## 32\"`\n        exitarg          *int32   `tlb:\"maybe ## 32\"`\n        vmsteps          uint32   `tlb:\"## 32\"`\n        vminitstatehash  []byte   `tlb:\"bits 256\"`\n        vmfinalstatehash []byte   `tlb:\"bits 256\"`\n    } `tlb:\"^\"`\n}\n<\/pre>\n<p>txtest\u7684protobuf\uff1a<\/p>\n<pre>message txtest {\n   int32 exitcode = 1;\n}<\/pre>\n<p>\u6211\u8981\u89e3\u6790\u7684\u6570\u636e\u662fexitcode\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>    list, err := api.listtransactions(context.background(), addr, 1, uint64(txinfo.txlt), data)\n    if err != nil {\n        log.printf(\"send err: %s\", err.error())\n        return nil, err\n    }\n    for _, t := range list {\n        a := t.description.description\n\n        var result tlb.transactiondescriptionordinary\n\n        b, err := json.marshalindent(a, \"\", \"  \")\n        if err != nil {\n            fmt.println(\"error:\", err)\n        }\n        json.unmarshal([]byte(string(b)), &amp;result)\n\n        var computephase tlb.computephasevm\n\n        c, err := json.marshalindent(result.computephase.phase, \"\", \"  \")\n        if err != nil {\n            fmt.println(\"error:\", err)\n        }\n\n        json.unmarshal([]byte(string(c)), &amp;computephase)\n\n\n        detail := &amp;pb.txtest{\n            exitcode: computephase.details.exitcode,\n        }\n        detail2 := struct {\n            exitcode int32 `json:\"exit_code\"`\n        }{\n            exitcode: computephase.details.exitcode,\n        }\n\n        fmt.printf(\"detail: %+v\\n\", detail)\n        fmt.printf(\"detail2: %+v\\n\", detail2)\n\n    }\n<\/pre>\n<p>\u4e00\u7b14\u4ea4\u6613\u7684\u6570\u636e\u7ed3\u6784\u4e3a\uff1a<\/p>\n<pre>{\n  \"accountaddr\": \"hymm1\/kk6gb2dlv3zvkiryepkohrtf\/jg8k7ttg91sq=\",\n  \"lt\": 11898016000001,\n  \"prevtxhash\": \"ikgjsxdt0gzijxgnilvxy0+a1ggeqded4f7zaj9dlmc=\",\n  \"prevtxlt\": 11897712000001,\n  \"now\": 1685602760,\n  \"outmsgcount\": 1,\n  \"origstatus\": \"active\",\n  \"endstatus\": \"active\",\n  \"io\": {\n    \"in\": {\n      \"msgtype\": \"external_in\",\n      \"msg\": {\n        \"srcaddr\": \"none\",\n        \"dstaddr\": \"eqadiyzx-qroyhymtxfnwqhhiskqgdfmx-mbwru1mb3wxn5d\",\n        \"importfee\": \"0\",\n        \"stateinit\": null,\n        \"body\": {}\n      }\n    },\n    \"out\": {\n      \"list\": {}\n    }\n  },\n  \"totalfees\": {\n    \"coins\": \"22324812\",\n    \"extracurrencies\": {}\n  },\n  \"stateupdate\": {\n    \"oldhash\": \"wukexyos8hswyqyrlhpmjhpfmusid8odtaq6fy20pyq=\",\n    \"newhash\": \"e8tn4cp4lakfwvdcgc\/vqbz7lzeb4mhjmbrfe8rpsqa=\"\n  },\n  \"description\": {\n    \"description\": {\n      \"creditfirst\": true,\n      \"storagephase\": {\n        \"storagefeescollected\": \"484\",\n        \"storagefeesdue\": null,\n        \"statuschange\": {\n          \"type\": \"unchanged\"\n        }\n      },\n      \"creditphase\": null,\n      \"computephase\": {\n        \"phase\": {\n          \"success\": true,\n          \"msgstateused\": false,\n          \"accountactivated\": false,\n          \"gasfees\": \"19862000\",\n          \"details\": {\n            \"gasused\": 19862,\n            \"gaslimit\": 0,\n            \"gascredit\": 10000,\n            \"mode\": 0,\n            \"exitcode\": 0,\n            \"exitarg\": null,\n            \"vmsteps\": 404,\n            \"vminitstatehash\": \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=\",\n            \"vmfinalstatehash\": \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=\"\n          }\n        }\n      },\n      \"actionphase\": {\n        \"success\": true,\n        \"valid\": true,\n        \"nofunds\": false,\n        \"statuschange\": {\n          \"type\": \"unchanged\"\n        },\n        \"totalfwdfees\": \"1000000\",\n        \"totalactionfees\": \"333328\",\n        \"resultcode\": 0,\n        \"resultarg\": null,\n        \"totalactions\": 1,\n        \"specactions\": 0,\n        \"skippedactions\": 0,\n        \"messagescreated\": 1,\n        \"actionlisthash\": \"fkoo2xx9ju4yxtgjlrujvayrrwtryhebkuywuyg9av4=\",\n        \"totalmsgsize\": {\n          \"cells\": 1,\n          \"bits\": 697\n        }\n      },\n      \"aborted\": false,\n      \"bouncephase\": null,\n      \"destroyed\": false\n    }\n  },\n  \"hash\": \"vmxfrkfietmaihm3brtvhskaide1ckgrw8rqbvarjtq=\"\n}<\/pre>\n<p><code>detail<\/code> \u548c <code>detail2<\/code> \u7684\u8f93\u51fa\u4e3a\uff1a<\/p>\n<pre>detail: \ndetail2: {ExitCode:0}<\/pre>\n<p>\u6211\u7684\u95ee\u9898\u662f\u4e3a\u4ec0\u4e48 <code>protoc<\/code> \u751f\u6210\u7684\u7ed3\u6784\u65e0\u6cd5\u4ece <code>transaction<\/code> \u89e3\u6790 <code>exitcode<\/code> \u4f46\u6211\u5b9a\u4e49\u7684\u7ed3\u6784\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff1f<\/p>\n<p>\u5982\u4f55\u5c06\u6570\u636e\u6ce8\u5165\u5230<code>protoc<\/code>\u751f\u6210\u7684struct <code>txtest<\/code>\u4e2d\uff0c\u8ba9\u6211\u901a\u8fc7<code>grpc<\/code>\u4f20\u8f93\u6570\u636e\uff1f<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u4f7f\u7528 0 \u4ee5\u5916\u7684\u9000\u51fa\u4ee3\u7801\u503c\u5c1d\u8bd5\u6b64\u64cd\u4f5c\uff0c\u6211\u731c\u60a8\u4f1a\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u770b\u770b\u60a8\u751f\u6210\u7684\u4ee3\u7801\u3002\u6211\u76f8\u4fe1\u53d1\u751f\u7684\u4e8b\u60c5\u662f \u6b63\u5728\u4e3a\u60a8\u7684\u539f\u578b\u7ed3\u6784\u8c03\u7528\u751f\u6210\u7684 <code>String()<\/code> \u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528 <code>prototext<\/code> \u5305\u6765\u6253\u5370\u6d88\u606f\u7684\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684 Go \u7ed3\u6784\u6253\u5370\u673a\u3002<\/p>\n<p>\u8fd9\u662f <code>proto3<\/code> \u7684\uff1b <code>int32<\/code> \u503c <code>0<\/code> \u548c\u7a7a\u5b57\u6bb5\u4e4b\u95f4\u6ca1\u6709\u533a\u522b\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207524","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207524","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=207524"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207524\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}