{"id":1652,"date":"2020-10-03T02:54:13","date_gmt":"2020-10-02T18:54:13","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1652"},"modified":"2022-02-16T21:54:16","modified_gmt":"2022-02-16T13:54:16","slug":"go-iris-install-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/10\/03\/go-iris-install-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris \u5b89\u88dd"},"content":{"rendered":"<h1>iris<\/h1>\n<p>\u622a\u6b62\u76ee\u524d\u7684\u6587\u7ae0\u5df2\u7d93\u5c07 go \u8a9e\u8a00\u7684\u5167\u5bb9\u8aaa\u660e\u4e86\u4e94\u6210\u5de6\u53f3\u4e86\uff0c\u3002\u63a5\u4e0b\u4f86\u958b\u59cb\u8981\u9032\u5165 iris \u9019\u500b\u6846\u67b6\u7684\u5167\u5bb9\uff0c\u6240\u4ee5\u4eca\u5929\u9019\u7bc7\u5148\u4f86\u500b iris \u7684\u5b89\u88dd\u4ee5\u53ca\u521d\u9ad4\u9a57\u3002<\/p>\n<p><!--more--><\/p>\n<h2>iris \u5b89\u88dd<\/h2>\n<p>\u672c\u7bc7\u4e3b\u65e8\u5728\u65bc\u5354\u52a9\u5927\u5bb6\u914d\u7f6e\u597d iris \u7684\u74b0\u5883\uff0c\u6240\u4ee5\u5167\u5bb9\u5206\u6210\u5169\u90e8\u5206\uff0c\u5176\u4e00\u662f\u5354\u52a9\u5b89\u88dd\u3001\u5176\u4e8c\u662f\u9a57\u8b49\u5b89\u88dd\u7684\u72c0\u6cc1\u662f\u5426\u53ef\u4ee5\u958b\u767c\uff0c\u63a5\u4e0b\u4f86\u5148\u5354\u52a9\u5927\u5bb6\u5b89\u88dd\u3002<\/p>\n<h3>iris \u5b89\u88dd\u65b9\u5f0f<\/h3>\n<p>\u76f8\u4fe1\u6709\u770b\u904e\u524d\u4e00\u7bc7\u6587\u7ae0\u7684\u8b80\u8005\u5011\u6240\u7f3a\u7684\u662f iris \u7684\u7a0b\u5f0f\u5009\uff0c\u4e0d\u904e\u53ef\u80fd\u9084\u662f\u6703\u6709\u8b80\u8005\u9047\u5230\u554f\u984c\uff0c\u7e3d\u4e4b\u4eca\u5929\u9019\u7bc7\u5c31\u5e36\u5927\u5bb6\u6e96\u5099\u597d iris \u7684\u74b0\u5883\u3002<\/p>\n<p>\u9996\u5148\u5148\u6e96\u5099\u597d\u8cc7\u6599\u593e\u7d50\u69cb\uff0c\u9019\u88e1\u53ef\u4ee5\u5230<a href=\"https:\/\/github.com\/golang-standards\/project-layout\" title=\"project layout\" rel=\"nofollow noopener\" target=\"_blank\">project layout<\/a>\u4e0b\u8f09\u8cc7\u6599\u7d50\u69cb\uff0c\u63a5\u4e0b\u4f86\u5728\u8a72\u5c08\u6848\u76ee\u9304\u57f7\u884c\u4ee5\u4e0b\u6307\u4ee4<\/p>\n<pre><code class=\"language-bash\">go mod init [projectName]\ngo get github.com\/kataras\/iris\/v12@master\ngo mod vender<\/code><\/pre>\n<p>\u76f8\u4fe1\u7d93\u904e\u57f7\u884c\u9019\u547d\u4ee4\u5df2\u7d93\u5b89\u88dd\u597d\u7b2c\u4e09\u65b9\u51fd\u5f0f\u5eab\uff0c\u63a5\u4e0b\u4f86\u8981\u5beb\u4e9b iris \u7684\u7a0b\u5f0f\u78bc\u78ba\u5b9a\u5b89\u88dd\u5b8c\u6210\u3002<\/p>\n<h3>iris \u521d\u9ad4\u9a57<\/h3>\n<p>\u518d\u7d93\u904e\u4e0a\u8ff0\u6b65\u9a5f\u76f8\u4fe1\u5927\u5bb6\u5df2\u7d93\u5b89\u88dd\u597d iris\u7684\u51fd\u5f0f\u5eab\uff0c\u63a5\u4e0b\u4f86\u8981\u78ba\u8a8d\u74b0\u5883\u662f\u5426\u5b89\u88dd\u5b8c\u6574\u5c31\u662f\u5148\u5728\u5c08\u6848\u7684 <code>cmd<\/code> \u4e0b\u65b0\u589e\u4e00\u500b <code>main.go<\/code> \u4e26\u8cbc\u5165\u4ee5\u4e0b\u5167\u5bb9<\/p>\n<pre><code class=\"language-go\">package main\n\nimport &quot;github.com\/kataras\/iris\/v12&quot;\n\nfunc main() {\n    app := iris.New()\n\n    booksAPI := app.Party(&quot;\/books&quot;)\n    {\n        booksAPI.Use(iris.Compression)\n\n        \/\/ GET: http:\/\/localhost:8080\/books\n        booksAPI.Get(&quot;\/&quot;, list)\n        \/\/ POST: http:\/\/localhost:8080\/books\n        booksAPI.Post(&quot;\/&quot;, create)\n    }\n\n    app.Listen(&quot;:8080&quot;)\n}\n\n\/\/ Book example.\ntype Book struct {\n    Title string `json:&quot;title&quot;`\n}\n\nfunc list(ctx iris.Context) {\n    books := []Book{\n        {&quot;Mastering Concurrency in Go&quot;},\n        {&quot;Go Design Patterns&quot;},\n        {&quot;Black Hat Go&quot;},\n    }\n\n    ctx.JSON(books)\n    \/\/ TIP: negotiate the response between server&#039;s prioritizes\n    \/\/ and client&#039;s requirements, instead of ctx.JSON:\n    \/\/ ctx.Negotiation().JSON().MsgPack().Protobuf()\n    \/\/ ctx.Negotiate(books)\n}\n\nfunc create(ctx iris.Context) {\n    var b Book\n    err := ctx.ReadJSON(&amp;b)\n    \/\/ TIP: use ctx.ReadBody(&amp;b) to bind\n    \/\/ any type of incoming data instead.\n    if err != nil {\n        ctx.StopWithProblem(iris.StatusBadRequest, iris.NewProblem().\n            Title(&quot;Book creation failure&quot;).DetailErr(err))\n        \/\/ TIP: use ctx.StopWithError(code, err) when only\n        \/\/ plain text responses are expected on errors.\n        return\n    }\n\n    println(&quot;Received Book: &quot; + b.Title)\n\n    ctx.StatusCode(iris.StatusCreated)\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u4f8b\u5b50\u662f\u4f86\u81ea\u65bc <a href=\"https:\/\/www.iris-go.com\/docs#\/\" title=\"iris document\" rel=\"nofollow noopener\" target=\"_blank\">iris document<\/a> \u9019\u662f\u4e00\u500b\u57fa\u672c\u7684api \u63a5\u53e3\uff0c\u63a5\u4e0b\u4f86\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef<\/p>\n<pre><code class=\"language-bash\">go run .\/cmd\/main.go<\/code><\/pre>\n<p>\u7d93\u904e\u57f7\u884c\u9019\u547d\u4ee4\u76f8\u4fe1\u5927\u5bb6\u7684\u672c\u6a5f\u61c9\u8a72\u5df2\u7d93\u57f7\u884c\u8d77\u4e00\u500bapi\u670d\u52d9\u4e86\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u4eca\u5929\u9019\u7bc7\u5148\u5354\u52a9\u5927\u5bb6\u914d\u7f6e\u597d iris \u7684\u74b0\u5883\uff0c\u63a5\u4e0b\u4f86\u8981\u958b\u59cb\u9032\u5165 iris \u7684\u5167\u5bb9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>iris \u622a\u6b62\u76ee\u524d\u7684\u6587\u7ae0\u5df2\u7d93\u5c07 go \u8a9e\u8a00\u7684\u5167\u5bb9\u8aaa\u660e\u4e86\u4e94\u6210\u5de6\u53f3\u4e86\uff0c\u3002\u63a5\u4e0b\u4f86\u958b\u59cb\u8981\u9032\u5165 iris \u9019\u500b\u6846\u67b6\u7684\u5167\u5bb9 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/10\/03\/go-iris-install-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris \u5b89\u88dd\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81,84],"class_list":["post-1652","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go","tag-iris"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1652"}],"version-history":[{"count":10,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1652\/revisions"}],"predecessor-version":[{"id":2961,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1652\/revisions\/2961"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}