{"id":239438,"date":"2022-04-17T09:35:58","date_gmt":"2022-04-17T01:35:58","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=239438"},"modified":"2022-03-31T10:36:20","modified_gmt":"2022-03-31T02:36:20","slug":"erlang-explanation-record","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/erlang-explanation-record.html","title":{"rendered":"Erlang\u4e2d\u7684Record\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Erlang\u4e2d\u7684Record\u8be6\u89e3,\u672c\u6587\u8bb2\u89e3\u4e86\u5b9a\u4e49Record\u3001\u521b\u5efaRecord\u3001\u8bbf\u95eeRecord\u3001\u66f4\u65b0Record\u3001\u5339\u914dRecord\u548cGuard\u8bed\u53e5\u3001\u4f7f\u7528Record\u7b49\u5185\u5bb9,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728Erlang\u5185\u90e8\u53ea\u6709\u4e24\u79cd\u6df7\u5408\u7684\u6570\u636e\u7c7b\u578b\uff1aList\u548cTuple\uff0c\u800c\u8fd9\u4e24\u79cd\u90fd\u4e0d\u652f\u6301\u547d\u540d\u8bbf\u95ee\uff0c\u6240\u4ee5\u5982\u679c\u6ca1\u6709\u989d\u5916\u7684\u5e93\u7684\u8bdd\u60f3\u521b\u5efa\u50cfPHP\u3001Ruby\u6216Python\u4e2d\u7684\u5173\u8054\u6570\u7ec4\uff08Ruby\u4e2d\u7684Hash\uff09\u662f\u4e0d\u53ef\u80fd\u7684<\/p>\n

\u5728Ruby\u4e2d\u6211\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a
\n\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\nserver_opts = {:port => 8080, :ip => '127.0.0.1', :max_connections => 10} \r\n<\/pre>\n

\u5728Erlang\u7684\u8bed\u6cd5\u7ea7\u522b\u4e0d\u652f\u6301\u8fd9\u79cd\u8868\u8fbe<\/p>\n

\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u9650\u5236\uff0cErlang\u865a\u62df\u673a\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4f2a\u6570\u636e\u7c7b\u578b\uff0c\u79f0\u4e3aRecord
\nRecord\u652f\u6301\u547d\u540d\u8bbf\u95ee\uff0c\u540e\u9762\u6211\u4eec\u4f1a\u770b\u5230\u4e3a\u4ec0\u4e48\u6211\u4eec\u79f0\u4e4b\u4e3a\u201c\u4f2a\u201d\u6570\u636e\u7c7b\u578b<\/p>\n

\u5b9a\u4e49Record<\/strong><\/div>\n

Record\u66f4\u7c7b\u4f3c\u4e8eC\u4e2d\u7684struct\uff0c\u800c\u4e0d\u662f\u5173\u8054\u6570\u7ec4\uff0c\u540e\u8005\u5fc5\u987b\u4e00\u5f00\u59cb\u5c31\u5b9a\u4e49\u597d\u5185\u5bb9\u5e76\u4e14\u53ea\u80fd\u4fdd\u6301\u6570\u636e
\n\u8fd9\u91cc\u662f\u4e00\u4e2a\u670d\u52a1\u5668\u7684\u8fde\u63a5\u9009\u9879\u7684Record\u4f8b\u5b50\uff1a
\n\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\n-module(my_server). \r\n \r\n-record(server_opts, \r\n  {port, \r\n  ip=\"127.0.0.1\", \r\n  max_connections=10}). \r\n \r\n% The rest of your code goes here. \r\n<\/pre>\n

Record\u4f7f\u7528-record\u6307\u4ee4\u6765\u58f0\u660e\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662fRecord\u7684\u540d\u5b57\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2aTuple\uff0cTuple\u5305\u542b\u4e86Record\u91cc\u7684field\u548c\u9ed8\u8ba4\u503c
\n\u5728\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86server_opts\u8fd9\u4e2aRecord\uff0c\u5b83\u6709\u4e09\u4e2afield\uff1a\u7aef\u53e3\u3001IP\u548c\u6700\u5927\u8fde\u63a5\u6570
\n\u6ca1\u6709\u9ed8\u8ba4\u7684port\uff0cip\u9ed8\u8ba4\u503c\u4e3a\"127.0.0.1\"\uff0cmax_connections\u9ed8\u8ba4\u503c\u4e3a10<\/p>\n

\u521b\u5efaRecord<\/strong><\/div>\n

Record\u901a\u8fc7\u4f7f\u7528#\u7b26\u53f7\u6765\u521b\u5efa\uff0c\u4e0b\u9762\u662f\u521b\u5efaserver_opts\u8fd9\u4e2aRecord\u7684\u5b9e\u4f8b\u7684\u5408\u6cd5\u65b9\u5f0f\uff1a
\n\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\nOpts1 = #server_opts{port=80}. \r\n<\/pre>\n

\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2aserver_opts Record\uff0cport\u8bbe\u7f6e\u4e3a80\uff0c\u5176\u4ed6field\u4f7f\u7528\u9ed8\u8ba4\u503c<\/p>\n

\r\nOpts2 = #server_opts{port=80, ip=\"192.168.0.1\"}. \r\n<\/pre>\n

\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2aserver_opts Record\uff0c\u4f46\u662fip\u8bbe\u7f6e\u4e3a\"192.168.0.1\"<\/p>\n

\u7b80\u800c\u8a00\u4e4b\uff0c\u5f53\u521b\u5efa\u4e00\u4e2aRecord\u65f6\uff0c\u4f60\u53ef\u4ee5\u5305\u542b\u4efb\u4f55field\uff0c\u7701\u7565\u7684field\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c<\/p>\n

\u8bbf\u95eeRecord<\/strong><\/div>\n

Record\u7684\u8bbf\u95ee\u65b9\u5f0f\u5f88\u7b28\u62d9\uff0c\u5982\u679c\u6211\u60f3\u8bbf\u95eeport\u8fd9\u4e2afield\uff0c\u6211\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a
\n\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\nOpts = #server_opts{port=80, ip=\"192.168.0.1\"}, \r\nOpts#server_opts.port \r\n<\/pre>\n

\u6bcf\u6b21\u4f60\u60f3\u8bbf\u95ee\u4e00\u4e2aRecord\u65f6\u4f60\u90fd\u5fc5\u987b\u5305\u542bRecord\u7684\u540d\u5b57\uff0c\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\uff1f
\n\u56e0\u4e3aRecord\u4e0d\u662f\u771f\u6b63\u7684\u5185\u90e8\u6570\u636e\u7c7b\u578b\uff0c\u5b83\u53ea\u662f\u7f16\u8bd1\u5668\u7684\u5c0f\u628a\u620f\u3002
\n\u5728\u5185\u90e8\uff0cRecord\u662fTuple\uff0c\u5982\u4e0b\uff1a
\n\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\n{server_opts, 80, \"127.0.0.1\", 10} \r\n<\/pre>\n

\u7f16\u8bd1\u5668\u5c06Record\u7684\u540d\u5b57\u6620\u5c04\u5230Tuple\u91cc\u9762
\nErlang\u865a\u62df\u673a\u8bb0\u5f55\u4e86Record\u7684\u5b9a\u4e49\uff0c\u800c\u7f16\u8bd1\u5668\u5c06\u6240\u6709\u7684Record\u903b\u8f91\u7ffb\u8bd1\u4e3aTuple\u903b\u8f91
\n\u56e0\u6b64\uff0c\u6839\u672c\u5c31\u6ca1\u6709Record\u7c7b\u578b\uff0c\u6240\u4ee5\u6bcf\u6b21\u4f60\u8bbf\u95ee\u4e00\u4e2aRecord\u65f6\u4f60\u5fc5\u987b\u544a\u8bc9Erlang\u6211\u4eec\u5728\u7528\u54ea\u4e2aRecord\uff08\u4e3a\u4e86\u7f16\u8bd1\u5668\u723d\uff0c\u7a0b\u5e8f\u5458\u53d8\u7684\u5f88\u4e0d\u723d\uff09<\/p>\n

\u66f4\u65b0Record<\/strong><\/div>\n

\u66f4\u65b0Record\u548c\u521b\u5efaRecord\u5f88\u7c7b\u4f3c\uff1a
\n\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\nOpts = #server_opts{port=80, ip=\"192.168.0.1\"}, \r\nNewOpts = Opts#server_opts{port=7000}. \r\n<\/pre>\n

\u8fd9\u91cc\u9996\u5148\u521b\u5efa\u4e00\u4e2aserver_opts Record
\nNewOpts = Opts#{port=7000}\u521b\u5efa\u4e86\u4e00\u4e2aOpts\u7684\u526f\u672c\uff0c\u5e76\u6307\u5b9aport\u4e3a7000\u5e76\u7ed1\u5b9a\u5230NewOpts<\/p>\n

\u5339\u914dRecord\u548cGuard\u8bed\u53e5<\/strong><\/div>\n

\u4e0d\u8c08\u6a21\u5f0f\u5339\u914d\u5c31\u4e0d\u7b97Erlang
\n\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a
\n\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\nhandle(Opts=#server_opts{port=8000}) -> \r\n  % do special port 8080 stuff \r\nhandle(Opts=#server_opts{} -> \r\n  % default stuff \r\n<\/pre>\n

Guard\u8bed\u53e5\u548c\u4e0a\u9762\u7684\u7c7b\u4f3c\uff0c\u4f8b\u5982\u7ed1\u5b9a\u5c0f\u4e8e1024\u7684\u7aef\u53e3\u901a\u5e38\u9700\u8981root\u6743\u9650\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a
\n\u590d\u5236\u4ee3\u7801\u4ee3\u7801\u5982\u4e0b:<\/p>\n

\r\nhandle(Opts) when Opts#server_opts.port <= 1024 -> \r\n  % requires root access \r\nhandle(Opts=#server_opts{}) -> \r\n  % Doesn't require root access \r\n<\/pre>\n
<\/strong><\/div>\n

\u4f7f\u7528Record[\/yiji[<\/p>\n

\u5728\u6211\u4f7f\u7528Erlang\u7684\u6709\u9650\u7684\u65f6\u95f4\u91cc\uff0c\u6211\u53d1\u73b0Record\u4e3b\u8981\u7528\u5728\u4e24\u79cd\u573a\u666f.\u9996\u5148\uff0cRecord\u7528\u6765\u4fdd\u5b58\u72b6\u6001\uff0c\u7279\u522b\u662f\u5728\u4f7f\u7528gen_server\u7684behaviour\u65f6\uff0c\u7531\u4e8eErlang\u4e0d\u80fd\u5168\u5c40\u4fdd\u6301\u72b6\u6001\uff0c\u6240\u4ee5\u72b6\u6001\u5fc5\u987b\u5728\u65b9\u6cd5\u4e4b\u524d\u4f20\u6765\u4f20\u53bb\u3002\u7136\u540e\uff0cRecord\u53ef\u4ee5\u7528\u6765\u4fdd\u5b58\u914d\u7f6e\u9009\u9879\uff0c\u8fd9\u53ef\u4ee5\u8ba4\u4e3a\u662f\u7b2c\u4e00\u70b9\u7684\u5b50\u96c6\u3002\u5c3d\u7ba1\u5982\u6b64\uff0cRecord\u4e5f\u6709\u4e00\u4e9b\u9650\u5236\uff0c\u6700\u660e\u663e\u7684\u662f\u4e0d\u80fd\u5728\u8fd0\u884c\u65f6\u6dfb\u52a0\u548c\u5220\u9664field\uff0c\u8fd9\u548cC\u7684struct\u4e00\u6837\uff0cRecord\u7684\u7ed3\u6784\u5fc5\u987b\u9884\u5148\u5b9a\u4e49\uff0c\u5982\u679c\u4f60\u60f3\u5728\u8fd0\u884c\u65f6\u6dfb\u52a0\u548c\u5220\u9664field\uff0c\u6216\u8005\u4f60\u5728\u8fd0\u884c\u65f6\u624d\u80fd\u786e\u5b9a\u6709\u54ea\u4e9bfield\uff0c\u8fd9\u65f6\u4f60\u5e94\u8be5\u4f7f\u7528dict\u800c\u4e0d\u662fRecord\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728Erlang\u5185\u90e8\u53ea\u6709\u4e24\u79cd\u6df7\u5408\u7684\u6570\u636e\u7c7b\u578b\uff1aList\u548cTuple\uff0c\u800c\u8fd9\u4e24\u79cd\u90fd\u4e0d\u652f\u6301\u547d\u540d\u8bbf\u95ee\uff0c\u6240\u4ee5\u5982\u679c\u6ca1\u6709\u989d\u5916\u7684\u5e93 […]<\/p>\n","protected":false},"author":1903,"featured_media":239440,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-239438","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=239438"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239438\/revisions"}],"predecessor-version":[{"id":239439,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239438\/revisions\/239439"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/239440"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=239438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=239438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=239438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}