{"id":2610,"date":"2023-10-15T17:22:29","date_gmt":"2023-10-15T09:22:29","guid":{"rendered":"https:\/\/white-night.club\/?p=2610"},"modified":"2023-11-09T11:07:49","modified_gmt":"2023-11-09T03:07:49","slug":"pg8","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/10\/15\/pg8\/","title":{"rendered":"MIT6.5830 Lab0-Go tutorial\u5b9e\u9a8c\u8bb0\u5f55\uff08\u4e00\uff09"},"content":{"rendered":"\n<p>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684http server\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u524d\u8a00<\/p>\n<cite>MIT\u6570\u636e\u5e93\u7cfb\u7edf\u5b9e\u9a8c<\/cite><\/blockquote>\n\n\n\n<p>\u5728\u7f51\u4e0a\u770b\u5230\u4e86\u8fd9\u4e48\u4e2a\u5b9e\u9a8c\uff0c\u521a\u597d\u5bf9golang\u548c\u6570\u636e\u5e93\u6bd4\u8f83\u611f\u5174\u8da3\uff0c\u4e8e\u662f\u5f00\u59cb\u8ddf\u7740\u505a\u5b9e\u9a8c\u3002\u4e0b\u9762\u653e\u4e24\u4e2a\u94fe\u63a5\uff0c\u524d\u8005\u662f\u8bfe\u7a0b\u5b98\u7f51\uff1b\u540e\u8005\u662f\u6211fork\u7684\u4ee3\u7801\u4ed3\u5e93\uff0c\u53ef\u4ee5\u53c2\u8003\u6211\u7684\u4ee3\u7801\u6216\u8005\u76f4\u63a5\u53bb\u770b\u6e90\u4ed3\u5e93\u3002<\/p>\n\n\n\n<p><a href=\"http:\/\/db.lcs.mit.edu\/6.5830\/\" data-type=\"URL\" data-id=\"http:\/\/db.lcs.mit.edu\/6.5830\/\"><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\">\u8bfe\u7a0b\u5730\u5740<\/mark><\/a><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\">  <\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\"> <\/mark><a href=\"https:\/\/github.com\/WhiteNight-SZTU\/lab0\"><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\">\u5b9e\u9a8c\u4ee3\u7801\u53c2\u8003<\/mark><\/a><\/p>\n\n\n\n<p>\u6211\u81ea\u5df1\u662fgo\u548c\u6570\u636e\u5e93\u7684\u521d\u5b66\u8005\uff0c\u6240\u4ee5\u90fd\u662f\u9760\u81ea\u5df1\u6478\u7d22\u3002\u53cd\u6b63\u6700\u540e\u80fd\u5b9e\u73b0\u5b9e\u9a8c\u8981\u6c42\u7684\u529f\u80fd\u5c31OK\u4e86\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u5b9e\u9a8c\u8981\u6c42<\/p>\n<cite><em>\u6ce2\u58eb\u987fMBTA\uff08MIT\u6240\u5728\u5730\u7684\u4e00\u4e2a\u4ea4\u901a\u8fd0\u8f93\u7ba1\u7406\u5c40\uff09\u7684\u6570\u636e\u53ef\u89c6\u5316<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u7ed9\u4e86\u4e00\u4e2a800mb\u7684sqlite\u6570\u636e\u5e93\u6587\u4ef6\u3002\u73b0\u5728\u9700\u8981\u4f60\u628a\u201c\u4e58\u5ba2\u91cf\u201d\u8fd9\u4e2a\u6570\u636e\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u603b\u7684\u6765\u8bf4\u5c31\u662f\u9700\u8981\u8ba9\u4f60\u7f16\u5199http server\u4ee5\u8ba9\u540e\u7aef\u7684\u6570\u636e\u5448\u73b0\u5728\u524d\u7aef\u7684\u9875\u9762\u4e0a\uff0c\u5176\u4e2d\u8fd8\u5305\u62ec\u4e86\u5904\u7406\u6570\u636e\u7b49\u7b49\u3002<\/p>\n\n\n\n<p><s>\u4f60\u5df2\u7ecf\u5b66\u4f1a\u4e86GO\u7684\u6570\u636e\u7c7b\u578b\uff0c\u63a5\u4e0b\u6765\u5c31\u5f00\u59cb\u5b9e\u9a8c\u5427\uff01\u8bf7\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684http server\u3002<\/s>\u5728\u505a\u5b9e\u9a8c\u524d\u8fd8\u662f\u5f97\u4e86\u89e3GO\u7684\u57fa\u7840\u77e5\u8bc6\u7684\uff0c\u4e0d\u8fc7\u4e0d\u4e86\u89e3\u4e5f\u6ca1\u95ee\u9898\uff0c\u672c\u6587\u4f1a\u8bb2\u7684\u975e\u5e38\u975e\u5e38\u8be6\u7ec6\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u5b9e\u9a8c\u6b65\u9aa4<\/p>\n<cite><em>\u521b\u5efahttp server<\/em><\/cite><\/blockquote>\n\n\n\n<p>clone\u5b8c\u4ed3\u5e93\u540e\uff0c\u522b\u5fd8\u4e86\u4e0b\u8f7dmbta.sqlite\u3002\u5177\u4f53\u7684\u6587\u4ef6\u7ed3\u6784\u5df2\u7ecf\u5728md\u91cc\u9762\u5199\u4e86\uff0c\u8fd9\u91cc\u5c31\u4e0d\u518d\u8bf4\u4e86\u3002<\/p>\n\n\n\n<p>\u6253\u5f00main.go\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7b2c\u4e00\u4e2aTODO<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nfunc main() {\n\t\/\/ TODO: some code goes here\n\t\/\/ Fill out the HomeHandler function in handlers\/handlers.go which handles the user's GET request.\n\t\/\/ Start an http server using http.ListenAndServe that handles requests using HomeHandler.\n\t\n}\n<\/code><\/pre>\n\n\n\n<p>\u8981\u6c42\u6211\u4eec\u5148\u8865\u5168HomeHandler\uff0cHomeHandler\u662f\u4ec0\u4e48\uff0c\u4e0d\u77e5\u9053\u3002\u5148\u4e0d\u7ba1\u3002\u4f46\u6211\u4eec\u77e5\u9053\u5b83\u662f\u4e00\u4e2ahandler\u3002<\/p>\n\n\n\n<ul>\n<li>Handler\uff1a\u53c8\u53eb\u5904\u7406\u5668\uff0c\u5728\u8fd9\u91cc\u7528\u4e8e\u5904\u7406HTTP\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6839\u636eGO\u7684\u5b98\u65b9\u6587\u6863\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd8\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u521b\u5efahttp server\u7684\u4ee3\u7801\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http.Handle(\"\/foo\", fooHandler)\n\nhttp.HandleFunc(\"\/bar\", func(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintf(w, \"Hello, %q\", html.EscapeString(r.URL.Path))\n})\n\nlog.Fatal(http.ListenAndServe(\":8080\", nil))<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5217\u51fa\u4e86\u4e24\u79cd\u6ce8\u518cHandler\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul>\n<li>http.Handle(A,B)\uff1aA\u4e3aHTTP\u8def\u7531\u3002B\u4e3a\u5df2\u6709\u7684Handle\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>http.HandleFunc(A,B)\uff1aA\u4e3aHTTP\u8def\u7531\u3002B\u4e3a\u5904\u7406\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u5f53\u7528\u6237\u8bf7\u6c42\u8bbf\u95ee\u5bf9\u5e94\u7684\u8def\u7531\u65f6\uff08\u6bd4\u5982https:\/\/baidu.com\/hello\u4e2d\u7684\/hello)\uff0c\u5c31\u4f1a\u8c03\u7528Handler\u7684\u65b9\u6cd5\u6765\u54cd\u5e94\u7528\u6237\u7684\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e0bHomeHandler\uff0c\u8fd9\u91cc\u5df2\u7ecf\u8bf4\u660e\u4e86\u5b83\u662f\u4e2afunction\uff0c\u90a3\u6211\u4eec\u5c31\u7528HandleFunc\u3002\u800cListenAndServer\u540c\u6837\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u524d\u8005\u4e3a\u8981\u76d1\u542c\u7684\u7aef\u53e3\uff0c\u540e\u8005\u9ed8\u8ba4nil\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n\t\"main\/handlers\"\n\t\"net\/http\"\n)\n\nfunc main() {\n\t\/\/ TODO: some code goes here\n\t\/\/ Fill out the HomeHandler function in handlers\/handlers.go which handles the user's GET request.\n\t\/\/ Start an http server using http.ListenAndServe that handles requests using HomeHandler.\n\n\thttp.HandleFunc(\"\/\", handlers.HomeHandler)\n\tport := 8080\n\thttp.ListenAndServe(fmt.Sprintf(\":%d\", port), nil)\n}\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8c03\u8bd5\u524d\uff0c\u7531\u4e8e<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">GO\u8bed\u8a00\u4e0d\u5141\u8bb8\u201c\u53d8\u91cf\u5b9a\u4e49\u4f46\u4e0d\u4f7f\u7528<\/mark>\u201d\uff0c\u4f60\u8fd8\u9700\u8981\u201c<strong>\u4f7f\u7528<\/strong>\u201dhandlers\u6587\u4ef6\u5939\u4e0b\u7684handlers.go\u4e2d\u7684db,tmpl,data\u3002\u6bd5\u7adf\u4f60\u73b0\u5728\u8fd8\u4e0d\u77e5\u9053\u8fd9\u51e0\u4e2a\u53d8\u91cf\u80fd\u5e72\u4ec0\u4e48\uff0c\u6240\u4ee5\u7528\u7a7a\u767d\u6807\u8bc6\u7b26\u201c_\u201d\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_=db;\n_=tmpl;\n_=data;<\/code><\/pre>\n\n\n\n<p>\u540c\u65f6\u4e3a\u4e86\u65b9\u4fbf\u8c03\u8bd5\uff0c\u53ef\u4ee5\u52a0\u4e2a\u6253\u5370\u770b\u770b\u6548\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\/\/ TODO: some code goes here\n\/\/ Get the chart data from RidershipDB<\/mark>\n_ = db\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#ffdf00\" class=\"has-inline-color\">fmt.Fprintln(w, \"Hello, this is the home page!\")<\/mark>\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\">\/\/ TODO: some code goes here\n\/\/ Plot the bar chart using utils.GenerateBarChart. The function will return the bar chart\n\/\/ as PNG byte slice. Convert the bytes to a base64 string, which is used to embed images in HTML.<\/mark>\n<\/code><\/pre>\n\n\n\n<p>\u6309f5\u8c03\u8bd5\uff0c\u6d4f\u89c8\u5668\u8bbf\u95eelocalhost:8080\u3002\u6b63\u5e38\u663e\u793a\u754c\u9762<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full has-custom-border\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"165\" src=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/10\/5f2470a0be4afce1570ff8d798f9850.png\" alt=\"\" class=\"wp-image-2621\" style=\"border-style:none;border-width:0px;border-radius:0px\" srcset=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/10\/5f2470a0be4afce1570ff8d798f9850.png 350w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/10\/5f2470a0be4afce1570ff8d798f9850-300x141.png 300w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/figure>\n\n\n\n<p>\u4e0b\u4e00\u7bc7\u6587\u7ae0\u6211\u4eec\u5c06\u5b9e\u73b0\u4eceRidershipDB\u4e2d\u62ff\u5230\u56fe\u50cf\u6570\u636e\u7b49\u64cd\u4f5c\u3002\u672c\u6587\u5c31\u5148\u5230\u8fd9\u91cc\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lab0\u5b9e\u9a8c\u8bb0\u5f55\uff1a\u7f16\u5199http server<\/p>\n","protected":false},"author":1,"featured_media":2621,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[44,33],"tags":[34,80],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2610"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=2610"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2610\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2621"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=2610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=2610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=2610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}