{"id":10006905,"date":"2022-11-24T03:37:08","date_gmt":"2022-11-23T19:37:08","guid":{"rendered":"https:\/\/ityet.com\/?p=10006905"},"modified":"2022-11-24T03:37:08","modified_gmt":"2022-11-23T19:37:08","slug":"gin-gorm-%e5%85%a5%e9%97%a8%e5%88%b0%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006905","title":{"rendered":"Gin + GORM \u5165\u95e8\u5230\u5b9e\u6218"},"content":{"rendered":"<p>\u672c\u6587\u53c2\u4e0e\u4e86<a href=\"https:\/\/segmentfault.com\/a\/1190000042741155\">\u601d\u5426\u6280\u672f\u5f81\u6587<\/a>\uff0c\u6b22\u8fce\u6b63\u5728\u9605\u8bfb\u7684\u4f60\u4e5f\u52a0\u5165\u3002<\/p>\n<p>Hello\uff0c\u5927\u5bb6\u597d\uff0c\u6211\u662f\u6d77\u519b\uff0c\u6700\u8fd1\u4e00\u76f4\u5728\u5b66\u4e60Go\uff0c\u76ee\u524d\u5728\u505a\u9879\u76ee\u719f\u6089Go \u9636\u6bb5\u3002 \u672c\u6587\u6765\u5206\u4eab\u4e00\u4e0b Gin + GORM \u7684\u4e00\u4e9b \u5f00\u53d1\u4f53\u9a8c\uff0c\u6709\u559c\u6b22Go \u65b9\u5411\u7684\u670b\u53cb\uff0c\u6b22\u8fce\u4e00\u8d77\u4ea4\u6d41\u5b66\u4e60\u5440\uff01<br \/>\u540e\u7eed\u4f1a\u66f4\u65b0\u5b9e\u6218\u9879\u76ee\uff0c\u76ee\u524d\u5728\u5b9e\u73b0\u4e00\u4e2a \u6280\u672f\u8bba\u575b\u9879\u76ee\uff0c\u7ed3\u5c3e\u6709\u6548\u679c\u56fe\uff0c\u524d\u7aef\u90e8\u5206\u5b8c\u6210\u4e86\uff0c\u73b0\u5728\u5728\u5b8c\u5584\u540e\u7aef\u63a5\u53e3\u548c\u524d\u7aef\u8054\u8c03\u7684\u8fc7\u7a0b\uff0c\u6ca1\u591a\u4e45\u5c31\u4f1a\u53d1\u5e03\u4e86\u3002 \u540e\u7eed\uff0c\u4f1a\u518d\u5199\u4e00\u7bc7\u9879\u76ee\u6587\u7ae0\u3002 <\/p>\n<p>\u5e72\u5c31\u5b8c\u4e86 \ud83e\udd14<\/p>\n<h2>\u5bfc\u8bfb\u76ee\u5f55<\/h2>\n<p><strong>Gin<\/strong><\/p>\n<ul>\n<li>Gin \u5b66\u4e60\u8def\u7ebf<\/li>\n<li>Gin \u5165\u95e8\u5b89\u88c5<\/li>\n<li>Gin \u4e2d\u95f4\u4ef6\u4f7f\u7528<\/li>\n<li>Gin \u83b7\u53d6\u8bf7\u6c42\u53c2\u6570<\/li>\n<li>Gin \u83b7\u53d6JSON\u6570\u636e<\/li>\n<li>Gin Cookie<\/li>\n<li>Gin Session<\/li>\n<li>Gin \u4e0a\u4f20\u6587\u4ef6<\/li>\n<\/ul>\n<p><strong>GORM<\/strong><\/p>\n<ul>\n<li>\u4ec0\u4e48\u662fGORM<\/li>\n<li>\u5982\u4f55\u5efaModel<\/li>\n<\/ul>\n<h2>Gin<\/h2>\n<h3>\u5b66\u4e60\u8def\u7ebf<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042882761\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<h3>\u5165\u95e8\u5b89\u88c5<\/h3>\n<h4>\u5b89\u88c5Gin<\/h4>\n<ol>\n<li>\u4e0b\u8f7d\u4f9d\u8d56<\/li>\n<\/ol>\n<pre><code class=\"css\">$ go get -u github.com\/gin-gonic\/gin<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5c06 gin \u5f15\u5165\u5230\u4ee3\u7801\u4e2d\uff1a<\/li>\n<\/ol>\n<pre><code class=\"css\">import &quot;github.com\/gin-gonic\/gin&quot;<\/code><\/pre>\n<ol start=\"3\">\n<li>\uff08\u53ef\u9009\uff09\u5982\u679c\u4f7f\u7528\u8bf8\u5982 http.StatusOK \u4e4b\u7c7b\u7684\u5e38\u91cf\uff0c\u5219\u9700\u8981\u5f15\u5165 net\/http \u5305\uff1a<\/li>\n<\/ol>\n<pre><code class=\"css\">import &quot;net\/http&quot;<\/code><\/pre>\n<p>demo<\/p>\n<pre><code class=\"go\">package main\n\nimport (\n    &quot;fmt&quot;\n    &quot;github.com\/gin-gonic\/gin&quot;\n)\n\nfunc main() {\n    fmt.Println(&quot;Gin \u5165\u95e8\u5b66\u4e60&quot;)\n\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u7684\u8def\u7531\u5f15\u64ce\n    r := gin.Default()\n    \/\/ GET\uff1a\u8bf7\u6c42\u65b9\u5f0f\uff1b\/hello\uff1a\u8bf7\u6c42\u7684\u8def\u5f84\n    \/\/ \u5f53\u5ba2\u6237\u7aef\u4ee5GET\u65b9\u6cd5\u8bf7\u6c42\/hello\u8def\u5f84\u65f6\uff0c\u4f1a\u6267\u884c\u540e\u9762\u7684\u533f\u540d\u51fd\u6570\n    r.GET(&quot;\/hello&quot;, func(c *gin.Context) {\n        \/\/ c.JSON\uff1a\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6570\u636e\n        c.JSON(200, gin.H{\n            &quot;message&quot;: &quot;Hello world!&quot;,\n        })\n    })\n    \/\/ \u542f\u52a8HTTP\u670d\u52a1\uff0c\u9ed8\u8ba4\u57280.0.0.0:8080\u542f\u52a8\u670d\u52a1\n    r.Run(&quot;:8098&quot;)\n}\n<\/code><\/pre>\n<h3>Gin \u4e2d\u95f4\u4ef6\u4f7f\u7528<\/h3>\n<h4>\u76ee\u5f55<\/h4>\n<ul>\n<li>\u4ec0\u4e48\u662f \u4e2d\u95f4\u4ef6\uff1f<\/li>\n<li>\u4e2d\u95f4\u4ef6\u4f5c\u7528\u57df<\/li>\n<li>\u4e2d\u95f4\u4ef6\u6570\u636e\u5171\u4eab<\/li>\n<li>\u4e2d\u95f4\u4ef6\u6ce8\u610f<\/li>\n<\/ul>\n<h4>\u4ec0\u4e48\u662f\u4e2d\u95f4\u4ef6\uff1f<\/h4>\n<p>\u4e2d\u95f4\u4ef6\u662f\u5728\u5f53\u5ba2\u6237\u7aef\u8bbf\u95ee\u670d\u52a1\u7aef\u63a5\u53e3\u4e4b\u524d\u548c\u4e4b\u540e\u4f1a\u505a\u4e00\u4e9b\u4e8b\u60c5\u3002 <br \/><strong>\u4f5c\u7528<\/strong>\uff1a  \u767b\u9646\u8ba4\u8bc1\uff0c  \u6743\u9650\u6821\u9a8c \uff0c \u8bb0\u5f55\u65e5\u5fd7\uff0c \u8017\u65f6\u7edf\u8ba1 &#8230;..<\/p>\n<h3>\u4e2d\u95f4\u4ef6\u4f5c\u7528\u57df<\/h3>\n<h4>\u5168\u5c40\u4f5c\u7528\u57df<\/h4>\n<p>\u5728 <code>main<\/code> \u5165\u53e3\u6587\u4ef6\u4e2d\uff0c  \u901a\u8fc7 \u521b\u5efa\u7684\u9ed8\u8ba4\u8def\u7531\u5f15\u64ce. use(\u4e2d\u95f4\u4ef61,\u4e2d\u95f42,&#8230;.) \u5373\u53ef\u6ce8\u518c\u4f7f\u7528\u5168\u5c40\u4e2d\u95f4\u4ef6\u4e86\u3002<\/p>\n<pre><code class=\"go\">import (\n   &quot;Gin\/middleware&quot;\n   &quot;Gin\/router&quot;\n   &quot;fmt&quot;\n   &quot;github.com\/gin-gonic\/gin&quot;\n   &quot;net\/http&quot;\n)\n\nfunc mian() {\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u7684\u8def\u7531\u5f15\u64ce\nvar r = gin.Default()  \n\n\/\/ \u5168\u5c40\u4f7f\u7528 \u4e2d\u95f4\u4ef7,\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\/\u591a\u4e2a\u4e2d\u95f4\u4ef6\nr.Use(middleware.GetHttpHost, middleware.GetHttpMethod)\n}<\/code><\/pre>\n<p><strong>\u521b\u5efa\u4e24\u4e2a\u4e2d\u95f4\u4ef6\uff0c\u5206\u522b\u83b7\u53d6 Host\uff0c Method<\/strong><\/p>\n<pre><code class=\"go\">package middleware\n\nimport (\n    &quot;fmt&quot;\n    &quot;github.com\/gin-gonic\/gin&quot;\n)\n\nfunc GetHttpHost(r *gin.Context) {\n    fmt.Printf(&quot;\u2139\ufe0f : \u8bf7\u6c42\u7684\u5730\u5740\u4e3a------%s &quot;, r.Request.Host)\n    r.Set(&quot;token&quot;, &quot;SDASD12312rtywe&quot;)\n    fmt.Println()\n    r.Next()\n}\n\nfunc GetHttpMethod(r *gin.Context) {\n    fmt.Printf(&quot;\u2139\ufe0f : \u8bf7\u6c42\u65b9\u6cd5\u4e3a------%s &quot;, r.Request.Method)\n    r.Next()\n}\n<\/code><\/pre>\n<h4>\u5c40\u90e8\u4f5c\u7528\u57df<\/h4>\n<p>\u901a\u8fc7\u5728\uff0c\u8def\u7531\u5206\u7ec4\u53c2\u6570\u8def\u5f84\u540e\u9762\uff0c\u6dfb\u52a0\u4e2d\u95f4\u4ef6\u5373\u53ef\uff0c\u652f\u6301\u6dfb\u52a0\u591a\u4e2a\u4e2d\u95f4\u4ef6<\/p>\n<pre><code class=\"go\">package router\n\nimport &quot;github.com\/gin-gonic\/gin&quot;\nimport &quot;Gin\/controller\/BookStore&quot;\nimport &quot;Gin\/middleware&quot;\n\n\nfunc BookRouter(r *gin.Engine) {\n    bookRouter := r.Group(&quot;\/book&quot;, middleware.GetLogInfo)  \/\/\u6dfb\u52a0\u5c40\u90e8\u4e2d\u95f4\u4ef6\n    {\n        bookRouter.GET(&quot;\/&quot;, BookStore.BookController{}.Index)\n\n        bookRouter.GET(&quot;\/:bookName&quot;, BookStore.BookController{}.SearchBookName)\n\n        bookRouter.POST(&quot;\/add&quot;, BookStore.BookController{}.Add)\n\n    }\n}<\/code><\/pre>\n<h3>\u4e2d\u95f4\u4ef6\u6570\u636e\u5171\u4eab<\/h3>\n<ul>\n<li>c.set(name, value)      c*gin.Context<\/li>\n<li>c.get(name)<\/li>\n<\/ul>\n<pre><code class=\"go\">package middleware\n\nimport (\n    &quot;fmt&quot;\n    &quot;github.com\/gin-gonic\/gin&quot;\n)\n\nfunc GetHttpHost(r *gin.Context) {\n    fmt.Printf(&quot;\u2139\ufe0f : \u8bf7\u6c42\u7684\u5730\u5740\u4e3a------%s &quot;, r.Request.Host)\n    r.Set(&quot;token&quot;, &quot;SDASD12312rtywe&quot;)\n    fmt.Println()\n    r.Next()\n}<\/code><\/pre>\n<pre><code class=\"go\">package middleware\n\nimport (\n    &quot;fmt&quot;\n    &quot;github.com\/gin-gonic\/gin&quot;\n    &quot;time&quot;\n)\n\nfunc GetLogInfo(c *gin.Context) {\n    receiveToken, _ := c.Get(&quot;token&quot;)\n    fmt.Println(&quot;\u8fdb\u5165\u4e2d\u95f4\u4ef6------&quot;)\n    fmt.Printf(&quot;\u6d4b\u8bd5\u83b7\u53d6\u4e2d\u95f4\u4ef6\u4e2d\u5171\u4eab\u7684\u6570\u636e---token:  %s&quot;, receiveToken)\n    fmt.Println()\n    c.Next()\n}\n<\/code><\/pre>\n<h3>\u83b7\u53d6\u8bf7\u6c42\u53c2\u6570<\/h3>\n<h4>\u83b7\u53d6querystring\u53c2\u6570<\/h4>\n<p>querystring\u6307\u7684\u662fURL\u4e2d?\u540e\u9762\u643a\u5e26\u7684\u53c2\u6570\uff0c\u4f8b\u5982\uff1a\/user\/search?username=\u5c0f\u738b\u5b50&#038;address=\u6c99\u6cb3\u3002 \u83b7\u53d6\u8bf7\u6c42\u7684querystring\u53c2\u6570\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">func getLoginInfo() {\n    r.GET(&quot;\/login&quot;, func(c *gin.Context) {\n        username := c.Query(&quot;username&quot;)\n        password := c.Query(&quot;password&quot;)\n        \/\/ c.JSON\uff1a\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6570\u636e\n        c.JSON(http.StatusOK, gin.H{\n            &quot;info&quot;:     &quot;==========&quot;,\n            &quot;username&quot;: username,\n            &quot;password&quot;: password,\n        })\n    })\n}<\/code><\/pre>\n<p>\u26a0\ufe0f \u6ce8\u610f<\/p>\n<ul>\n<li>\u53ef\u4ee5\u6307\u5b9a\u9ed8\u8ba4\u7684query \u503c,\u901a\u8fc7 c.DefaultQuery<\/li>\n<\/ul>\n<pre><code class=\"go\">username := c.DefaultQuery(&quot;username&quot;, &quot;admin&quot;)<\/code><\/pre>\n<ul>\n<li>\u6307\u5b9a\u63a5\u6536\u7684 query  key  \uff0cc.Query(&#8220;username&#8221;)<\/li>\n<\/ul>\n<pre><code class=\"go\">username := c.Query(&quot;username&quot;)<\/code><\/pre>\n<p>\u4e0a\u9762\u8bbf\u95ee &#8220;<a href=\"https:\/\/link.segmentfault.com\/?enc=7hr%2FLQHtRs8Yy0%2Fiw2edUA%3D%3D.H%2BU4Gh0EncTYIQDybiz7oKgSA%2FXvrjFrMTB1VzW9USI55LFB7296HFzbltfXKD8QYHmxujKGHS6L1ObfRnihng%3D%3D\" rel=\"nofollow\">http:\/\/127.0.0.1:8098\/login?username=admin&#038;password=1123123<\/a>&#8221; \u5373\u53ef\u8fd4\u56de\u6307\u5b9a\u7684json \u503c<\/p>\n<pre><code class=\"go\">{\n    &quot;info&quot;: &quot;==========&quot;,\n    &quot;password&quot;: &quot;1123123&quot;,\n    &quot;username&quot;: &quot;admin&quot;\n}<\/code><\/pre>\n<h4>\u83b7\u53d6 form \u53c2\u6570<\/h4>\n<p>\u8bf7\u6c42\u7684\u6570\u636e\u901a\u8fc7form\u8868\u5355\u6765\u63d0\u4ea4\u7684\uff0c\u901a\u8fc7 PostForm\uff08\uff09 \u63a5\u6536<\/p>\n<pre><code class=\"go\">func getFormParams() {\n    r.POST(&quot;\/saveBookInfo&quot;, func(c *gin.Context) {\n        bookName := c.PostForm(&quot;bookName&quot;)\n        author := c.PostForm(&quot;author&quot;)\n        \/\/ c.JSON\uff1a\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6570\u636e\n        c.JSON(http.StatusOK, gin.H{\n            &quot;bookName&quot;: bookName,\n            &quot;author&quot;:   author,\n        })\n        fmt.Println(&quot;\u6d4b\u8bd5Post&quot;)\n        fmt.Printf(&quot;\u8bbf\u95ee\u7684\u63a5\u53e3\u4e3a--%s,\u53c2\u6570\u4e3a\uff1a bookName--- %s, author----%s&quot;, &quot;\/saveBookInfo&quot;, bookName, author)\n    })\n}<\/code><\/pre>\n<h4>\u83b7\u53d6path\u53c2\u6570<\/h4>\n<p>\u83b7\u53d6URl \u7684 path\uff0c \u53ef\u4ee5\u901a\u8fc7 c.Param\uff08\uff09 \u63a5\u6536<\/p>\n<pre><code class=\"go\">func getRouterPathParams() {\n    r.GET(&quot;\/bookStore\/:bookName\/:author&quot;, func(c *gin.Context) {\n        bookName := c.Param(&quot;bookName&quot;)\n        author := c.Param(&quot;author&quot;)\n        \/\/ c.JSON\uff1a\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6570\u636e\n        c.JSON(http.StatusOK, gin.H{\n            &quot;bookName&quot;: bookName,\n            &quot;author&quot;:   author,\n        })\n        fmt.Println(&quot;\u6d4b\u8bd5Post&quot;)\n        fmt.Printf(&quot;\u8bbf\u95ee\u7684\u63a5\u53e3\u4e3a--%s,\u53c2\u6570\u4e3a\uff1a bookName--- %s, author----%s&quot;, &quot;\/saveBookInfo&quot;, bookName, author)\n    })\n}<\/code><\/pre>\n<p>\u8bbf\u95ee\u94fe\u63a5\ud83d\udd17 <a href=\"https:\/\/link.segmentfault.com\/?enc=f%2FAF6o7HfwfZxVg%2BqiikkA%3D%3D.ZPuTFpBrIxMMg1LLawbftyFh8b7d1in35u3WqOD6QiWCT3GOWkjpBNPouLkF0VWz\" rel=\"nofollow\">http:\/\/127.0.0.1:8098\/bookStore\/Go\/<\/a>\u6d77\u519b \uff0c\u8fd4\u56de<\/p>\n<pre><code class=\"go\">{\n    &quot;author&quot;: &quot;\u6d77\u519b&quot;,\n    &quot;bookName&quot;: &quot;Go&quot;\n}<\/code><\/pre>\n<p>\u4e3a\u4e86\u80fd\u591f\u66f4\u65b9\u4fbf\u7684\u83b7\u53d6\u8bf7\u6c42\u76f8\u5173\u53c2\u6570\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u8bf7\u6c42\u7684Content-Type\u8bc6\u522b\u8bf7\u6c42\u6570\u636e\u7c7b\u578b\u5e76\u5229\u7528\u53cd\u5c04\u673a\u5236\u81ea\u52a8\u63d0\u53d6\u8bf7\u6c42\u4e2dQueryString\u3001form\u8868\u5355\u3001JSON\u3001XML\u7b49\u53c2\u6570\u5230\u7ed3\u6784\u4f53\u4e2d\u3002 \u4e0b\u9762\u7684\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u4e86.ShouldBind()\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u80fd\u591f\u57fa\u4e8e\u8bf7\u6c42\u81ea\u52a8\u63d0\u53d6JSON\u3001form\u8868\u5355\u548cQueryString\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5e76\u628a\u503c\u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u7ed3\u6784\u4f53\u5bf9\u8c61\u3002<\/p>\n<h3>Cookie<\/h3>\n<p>Cookie \u662f\u5b58\u50a8\u5728\u5ba2\u6237\u7aef\u7684\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\u7684\uff0c \u5b83\u53ef\u4ee5 \u5728\u8bbf\u95ee\u540c\u4e00\u4e2a\u57df\u4e0b\u7684\u7f51\u7ad9 \u6570\u636e\u5171\u4eab\u3002 \u5b83\u89e3\u51b3\u4e86HTTP \u65e0\u72b6\u6001\u6570\u636e\u4e0d\u5171\u4eab\u95ee\u9898\u3002<\/p>\n<h4>Cookie \u529f\u80fd<\/h4>\n<p>:::info<\/p>\n<ul>\n<li>\u4fdd\u5b58\u7528\u6237\u767b\u9646\u72b6\u6001<\/li>\n<li>\u4fdd\u5b58\u7528\u6237\u6d4f\u89c8\u5668\u8bb0\u5f55<\/li>\n<li>\u731c\u4f60\u559c\u6b22\uff0c\u667a\u80fd\u63a8\u8350\u7b49<\/li>\n<li>\u8d2d\u7269\u8f66\u7b49<br \/>:::<\/li>\n<\/ul>\n<h4>\u8bbe\u7f6eCookie<\/h4>\n<pre><code class=\"go\">SetCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool)<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>base<\/th>\n<th> <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>key<\/td>\n<td>cookie &#8211; key<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>value<\/td>\n<td>cookie &#8211; value<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>maxAge<\/td>\n<td>cookie \u8fc7\u671f\u65f6\u95f4<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>path<\/td>\n<td>cookie \u8def\u5f84<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>domain<\/td>\n<td>cookie \u8def\u5f84\u7684\u4f5c\u7528\u57df\uff0c \u672c\u5730 localhost \uff0c\u7ebf\u4e0a\u4e3a\u57df\u540d<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>secure<\/td>\n<td>\u5f53 secure \u4e3a true \u65f6\uff0ccookie \u5728 HTTP \u662f\u65e0\u6548\u7684\uff0c HTTPS \u624d\u6709\u6548<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>httpOnly<\/td>\n<td>\u5fae\u8f6f\u5bf9cookie \u7684\u6269\u5c55\uff0c\u5982\u679c\u8bbe\u7f6e\u4e86 httpOnly\uff0c \u5219\u65e0\u6cd5\u83b7\u53d6cookie\u4fe1\u606f\uff0c\u9632\u6b62\u4e86XSS \u653b\u51fb<\/td>\n<td> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"go\">c.SetCookie(&quot;loginStatus&quot;, &quot;\u767b\u9646\u6210\u529f\u72b6\u6001&quot;, 5600, &quot;\/&quot;, &quot;localhost&quot;, false, true)<\/code><\/pre>\n<h4>\u83b7\u53d6 cookie<\/h4>\n<pre><code class=\"go\">c.Cookie(name)<\/code><\/pre>\n<h3>Session<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042882762\" alt=\"wallhaven-1keo6g.jpeg\" title=\"wallhaven-1keo6g.jpeg\" loading=\"lazy\"><\/p>\n<h4>session \u57fa\u672c\u539f\u7406<\/h4>\n<blockquote>\n<p>http\u534f\u8bae\u662f\u65e0\u72b6\u6001\u7684\uff0c\u5c31\u662f\u8bf4\u4f60\u5728\u8bf7\u6c42\u670d\u52a1\u5668\u540c\u65f6\uff0c\u670d\u52a1\u5668\u4e0d\u77e5\u9053\u54ea\u4e2a\u662f\u4f60\u8bbf\u95ee\u7684\uff0c\u600e\u4e48\u8ba9\u670d\u52a1\u5668\u77e5\u9053\u54ea\u4e2a\u662f\u4f60\u8bbf\u95ee\u7684\uff0c\u90a3\u4e48session \u5c31\u51fa\u6765\u4e86\u3002<br \/>session \u548c cookie \u4e0d\u5206\u5bb6\uff0c\u6bcf\u6b21\u8bf4 session \u5176\u5b9e\u5c31\u662f\u8bf4 cookie\u3002<\/p>\n<p>\u670d\u52a1\u7aef \u548c \u5ba2\u6237\u7aef \u6709\u72b6\u6001\u901a\u4fe1\u539f\u7406\uff1a<br \/>\u7b2c\u4e00\u6b21\u767b\u5f55\u65f6\uff0c\u670d\u52a1\u5668\u7ed9\u5ba2\u6237\u7aef\u9881\u53d1\u4e00\u4e2a\u552f\u4e00\u7684sessionId, \u5e76\u901a\u8fc7http\u7684\u54cd\u5e94\u5934\u8fd4\u56de\u3002\u5ba2\u6237\u7aef\uff08\u6d4f\u89c8\u5668\uff09\u53d1\u73b0\u8fd4\u56de\u7684\u6570\u636e\u4e2d\u6709cookie\u6570\u636e\u5c31\u628a\u8fd9\u4e2acookie\u6570\u636e\u5b58\u653e\u5230\u5185\u5b58\u3002\u4e0b\u6b21\u518d\u53d1\u9001http\u8bf7\u6c42\u65f6\uff0c\u628a\u5185\u5b58\u4e2d\u7684cookie\u6570\u636e\u518d\u585e\u5230http\u8bf7\u6c42\u5934\u4e2d\uff0c\u4e00\u5e76\u53d1\u7ed9\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u5728\u89e3\u6790\u8bf7\u6c42\u65f6\uff0c\u53d1\u73b0\u8bf7\u6c42\u5934\u4e2d\u6709cookie\uff0c\u5c31\u5f00\u59cb\u8bc6\u522bcookie\u4e2d\u7684sessionId\uff0c\u62ff\u5230sessionId\uff0c\u6211\u4eec\u5c31\u77e5\u9053\u8fd9\u4e2a\u8bf7\u6c42\u65f6\u7531\u54ea\u4e2a\u5ba2\u6237\u7aef\u53d1\u9001\u6765\u7684\u4e86\u3002<\/p>\n<\/blockquote>\n<h4>Gin \u4e2d\u4f7f\u7528 session<\/h4>\n<p>Gin \u672c\u8eab\u662f\u6ca1\u6709\u63d0\u4f9b session \u7684\uff0c\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u4f9d\u8d56\u3002<\/p>\n<ol>\n<li>\u5b89\u88c5\u4f9d\u8d56<\/li>\n<\/ol>\n<pre><code class=\"go\">go get github.com\/gin-contrib\/sessions<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5bfc\u5165\u4f9d\u8d56 \u4f7f\u7528<\/li>\n<\/ol>\n<pre><code class=\"go\">import (\n&quot;github.com\/gin-contrib\/sessions&quot;\n&quot;github.com\/gin-contrib\/sessions\/cookie&quot;\n&quot;github.com\/gin-gonic\/gin&quot;\n)\n\nfunc main(){\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u7684\u8def\u7531\u5f15\u64ce\nvar r = gin.Default()\n\n\/\/ 1. \u521b\u5efa\u57fa\u4e8ecookie\u7684\u5b58\u50a8\u5f15\u64ce\uff0chaijun \u53c2\u6570\u662f\u7528\u4e8e\u52a0\u5bc6\u7684\u5bc6\u94a5\uff0c\u53ef\u4ee5\u968f\u4fbf\u586b\u5199\nstore := cookie.NewStore([]byte(&quot;haijun&quot;))\n\n\/\/ 2. \u8bbe\u7f6esession\u4e2d\u95f4\u4ef6\uff0c\u53c2\u6570mysession\uff0c\u6307\u7684\u662fsession\u7684\u540d\u5b57\uff0c\u4e5f\u662fcookie\u7684\u540d\u5b57\n\/\/ store\u662f\u524d\u9762\u521b\u5efa\u7684\u5b58\u50a8\u5f15\u64ce\nr.Use(sessions.Sessions(&quot;mysession&quot;, store))\n\n\nr.GET(&quot;\/test&quot;, func(c *gin.Context) {\n        \/\/ \u521d\u59cb\u5316session\u5bf9\u8c61\n        session := sessions.Default(c)\n                \n        \/\/ session\u662f\u952e\u503c\u5bf9\u683c\u5f0f\u6570\u636e\uff0c\u56e0\u6b64\u9700\u8981\u901a\u8fc7key\u67e5\u8be2\u6570\u636e\n        session.Set(&quot;token&quot;, &quot;haijun23123&quot;)\n        session.Save()\n\n                \n        c.JSON(200, gin.H{&quot;token&quot;: session.Get(&quot;token&quot;)})\n    })\n\n}\n\n\n<\/code><\/pre>\n<h2>Session \u64cd\u4f5c<\/h2>\n<h4>\u8bbe\u7f6e session<\/h4>\n<pre><code class=\"go\">session.Set(&quot;kay&quot;, value)<\/code><\/pre>\n<h4>\u83b7\u53d6session<\/h4>\n<pre><code class=\"go\">session.Get(&quot;key&quot;)<\/code><\/pre>\n<h4>\u5220\u9664session<\/h4>\n<pre><code class=\"go\">session.Delete(&quot;key&quot;)  \/\/\u5220\u9664\u5355\u4e2a session\n\nsession.Clear() \/\/\u5220\u9664\u5168\u90e8session<\/code><\/pre>\n<h4>\u4fdd\u5b58session<\/h4>\n<pre><code class=\"go\">ssession.Save()<\/code><\/pre>\n<h4>\u26a0\ufe0f\u6ce8\u610f<\/h4>\n<ol>\n<li>session \u4ed3\u5e93\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a map[interface]interface \u5bf9\u8c61\uff0c\u6240\u6709 session\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u6570\u636e<\/li>\n<li>session \u4f7f\u7528\u7684\u7f16\u89e3\u7801\u5668\u662f\u81ea\u5e26\u7684gob\uff0c\u6240\u4ee5\u5b58\u50a8\u7c7b\u4f3c\uff1a struct\u3001map \u8fd9\u4e9b\u5bf9\u8c61\u65f6\u9700\u8981\u5148\u6ce8\u518c\u5bf9\u8c61\uff0c\u4e0d\u7136\u4f1a\u62a5\u9519 gob: type not registered for&#8230;<\/li>\n<li>session \u5b58\u50a8\u5f15\u64ce\u652f\u6301\uff1a cookie\u3001\u5185\u5b58\u3001mongodb\u3001redis\u3001postgres\u3001memstore\u3001memcached \u4ee5\u53ca gorm \u652f\u6301\u7684\u5404\u7c7b\u6570\u636e\u5e93\uff08mysql\u3001sqlite\uff09<\/li>\n<li>session \u5728\u521b\u5efa\u65f6\u6709\u4e00\u4e2a\u914d\u7f6e\u9879\uff0c\u53ef\u4ee5\u914d\u7f6esession\u8fc7\u671f\u65f6\u95f4\u3001cookie\u3001domain\u3001secure\u3001path\u7b49\u53c2\u6570<\/li>\n<li><strong>\u8c03\u7528 session \u65b9\u6cd5\uff1a Set()\u3001 Delete()\u3001 Clear()\u3001\u65b9\u6cd5\u540e\uff0c\u5fc5\u987b\u8c03\u7528\u4e00\u6b21 Save() \u65b9\u6cd5\u3002\u5426\u5219session\u6570\u636e\u4e0d\u4f1a\u66f4\u65b0<\/strong><\/li>\n<\/ol>\n<h3>\u4e0a\u4f20\u6587\u4ef6<\/h3>\n<h4>\u4e0a\u4f20\u5355\u6587\u4ef6<\/h4>\n<pre><code class=\"go\">func (bk BookController) UploadOne(c *gin.Context) {\n    \/\/\u83b7\u53d6\u6587\u4ef6\n    \/\/c.FormFile(&quot;\u6587\u4ef6\u53c2\u6570\u540d&quot;)\n    file, err := c.FormFile(&quot;file&quot;)\n    \/\/\u6587\u4ef6\u8def\u5f84\n    dst := path.Join(&quot;.\/static\/images&quot;, file.Filename)\n\n    \/\/ err == nil \u4ee3\u8868\u4e0a\u4f20\u6210\u529f\n    if err == nil {\n        c.SaveUploadedFile(file, dst)\n        c.JSON(200, gin.H{\n            &quot;msg&quot;: &quot;\u4e0a\u4f20\u6587\u4ef6\u6210\u529f&quot;,\n        })\n\n    } else {\n        c.JSON(400, gin.H{\n            &quot;msg&quot;: &quot;\u4e0a\u4f20\u6587\u4ef6\u5931\u8d25----- \ud83d\ude45&quot;,\n        })\n    }\n}<\/code><\/pre>\n<h4>\u4e0a\u4f20\u591a\u6587\u4ef6<\/h4>\n<pre><code class=\"go\">\/\/ \u4e0a\u4f20\u591a\u4e2a\u6587\u4ef6\nfunc (bk BookController) Upload(c *gin.Context) {\n    \/\/\u901a\u8fc7 c.MultipartForm() \u83b7\u53d6\u591a\u4e2a\u6587\u4ef6 \n    form, _ := c.MultipartForm()\n    filesArr := form.File[&quot;file[]&quot;]\n    fmt.Println(filesArr)\n    \/\/for range \u904d\u5386\u4fdd\u5b58\u6587\u4ef6\n    for _, file := range filesArr {\n        dst := path.Join(&quot;.\/static\/images&quot;, file.Filename)\n        c.SaveUploadedFile(file, dst)\n    }\n\n    c.JSON(200, gin.H{\n        &quot;msg&quot;:      &quot;\u4e0a\u4f20\u6587\u4ef6\u6210\u529f&quot;,\n        &quot;fileList&quot;: form.File[&quot;file[]&quot;],\n    })\n\n}<\/code><\/pre>\n<h4>\u5b9e\u6218<\/h4>\n<pre><code class=\"go\">\/\/ \u68c0\u9a8c\u4e0a\u4f20\u6587\u4ef6\u7684\u683c\u5f0f\nfunc (bk BookController) CheckFileFormat(c *gin.Context) {\n    \/\/1.\u83b7\u53d6\u6587\u4ef6\n    file, status := c.FormFile(&quot;companyInfo&quot;)\n    if status == nil {\n        \/\/2. \u83b7\u53d6\u6587\u4ef6\u683c\u5f0f\n        fileFormat := path.Ext(file.Filename)\n\n        allowFormat := map[string]bool{\n            &quot;.jpg&quot;: true,\n            &quot;.png&quot;: true,\n        }\n        _, ok := allowFormat[fileFormat]\n        fmt.Println(fileFormat)\n        fmt.Println(ok)\n        if ok == true {\n            \/\/3\u3002\u7b26\u5408\u63a5\u6536\u7684\u683c\u5f0f \u521b\u5efa\u4fdd\u5b58\u76ee\u5f55\n            mkDirStatus := os.MkdirAll(&quot;.\/static\/fileDir&quot;, 0777)\n\n            if mkDirStatus != nil {\n                c.String(400, &quot;\u521b\u5efa\u76ee\u5f55\u5931\u8d25&quot;)\n                return\n            }\n\n            \/\/4\u3002 \u751f\u6210\u6587\u4ef6\u540d\u8def\u5f84\n            newFilePath := path.Join(&quot;.\/static\/fileDir&quot;, file.Filename)\n\n            \/\/5. \u4fdd\u5b58\u6587\u4ef6\n            c.SaveUploadedFile(file, newFilePath)\n\n            c.String(200, &quot;\u4e0a\u4f20\u6587\u4ef6\u6210\u529f\ud83d\ude0a&quot;)\n        } else {\n            c.String(400, &quot;\u4e0a\u4f20\u6587\u4ef6\u5931\u8d25&quot;)\n        }\n    } else {\n        c.String(400, &quot;\u4e0a\u4f20\u6587\u4ef6\u5931\u8d25&quot;)\n    }\n}\n<\/code><\/pre>\n<h4>\u5c0f\u7ed3<\/h4>\n<ul>\n<li>\u4e0a\u4f20\u6587\u4ef6\u65f6\uff0c\u7c7b\u578b\u5fc5\u987b\u4e3a  multipart\/form-data<\/li>\n<li>c.FormFile(&#8220;file&#8221;)  \u83b7\u53d6\u5355\u4e2a\u6587\u4ef6<\/li>\n<li>c.MultipartForm()  \u83b7\u53d6\u591a\u4e2a\u6587\u4ef6 \uff0c \u591a\u4e2a\u6587\u4ef6\u7684\u53c2\u6570\u540d\u5fc5\u987b\u662f\u7edf\u4e00\u7684 \uff0c<\/li>\n<li>path.Join(&#8216;\u8def\u5f84&#8217;\uff0c\u6587\u4ef6\u540d)    \u62fc\u63a5\u6587\u4ef6\u8def\u5f84<\/li>\n<li>c.SaveUploadedFile(file, dst)   \u4fdd\u5b58\u6587\u4ef6\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a file \uff0c \u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u6587\u4ef6\u8def\u5f84<\/li>\n<li>path.Ext(file.Filename)   \u83b7\u53d6\u5230\u6587\u4ef6\u540e\u7f00\u540d<\/li>\n<li>os.MkdirAll(&#8220;\u521b\u5efa\u6587\u4ef6\u7684\u8def\u5f84&#8221;\uff0c\u6743\u9650code)   \u521b\u5efa\u6587\u4ef6<\/li>\n<\/ul>\n<h3>GORM<\/h3>\n<h4>\u4ec0\u4e48\u662fORM<\/h4>\n<p>\u4ec0\u4e48\u662fORM \uff1fO \uff1a Object  \u5bf9\u8c61Relationall:   \u5173\u7cfbMapping\uff1a  \u6620\u5c04<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042882763\" alt=\" title=\"Object]&amp;name=image.png&amp;originHeight=708&amp;originWidth=1141&amp;originalType=binary&amp;ratio=1&amp;rotation=0&amp;showTitle=false&amp;size=362510&amp;status=done&amp;style=none&amp;taskId=u0d201559-dec9-47a9-a6b3-8eec2f9af23&amp;title=&amp;width=578.5\"\" title=\" title=\"image.png&amp;originHeight=708&amp;originWidth=1141&amp;originalType=binary&amp;ratio=1&amp;rotation=0&amp;showTitle=false&amp;size=362510&amp;status=done&amp;style=none&amp;taskId=u0d201559-dec9-47a9-a6b3-8eec2f9af23&amp;title=&amp;width=578.5\"\"loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042882764\" alt=\" title=\"Object]&amp;name=image.png&amp;originHeight=165&amp;originWidth=494&amp;originalType=binary&amp;ratio=1&amp;rotation=0&amp;showTitle=false&amp;size=23763&amp;status=done&amp;style=none&amp;taskId=u8db13736-46a1-475d-b4f1-06c5dce8847&amp;title=&amp;width=594\"\" title=\" title=\"image.png&amp;originHeight=165&amp;originWidth=494&amp;originalType=binary&amp;ratio=1&amp;rotation=0&amp;showTitle=false&amp;size=23763&amp;status=done&amp;style=none&amp;taskId=u8db13736-46a1-475d-b4f1-06c5dce8847&amp;title=&amp;width=594\"\"loading=\"lazy\"><\/p>\n<p>\u200b\u76f8\u5f53\u4e8e\u5b9a\u4e49\u4e86\u4e00\u5f20 \u00a0\u6570\u636e\u5e93\u8868type Person struct {  Id \u00a0 \u00a0 \u00a0int  Name \u00a0 \u00a0string  Age \u00a0 \u00a0 int\u200b}\u200b\u200b\u7ed3\u6784\u4f53\u5b9e\u4f8b \u00a0\u76f8\u5f53\u4e8e \u6570\u636e\u884cstudent \uff1a= Person{1,&#8221;\u5c0f\u660e&#8221;,28}<\/p>\n<h4>ORM \u4f18\u7f3a\u70b9<\/h4>\n<p>\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li>\u63d0\u9ad8\u5f00\u53d1\u6548\u7387<\/li>\n<\/ul>\n<p>\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li>\u727a\u7272\u6267\u884c\u6027\u80fd<\/li>\n<li>\u727a\u7272\u7075\u6d3b\u6027<\/li>\n<li>\u5f31\u5316SQL \u80fd\u529b\uff0c \u624b\u5199SQL \u80fd\u964d\u4f4e<\/li>\n<\/ul>\n<h3>\u5982\u4f55\u5efaModel<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042882765\" alt=\"wallhaven-1k6ljv.jpeg\" title=\"wallhaven-1k6ljv.jpeg\" loading=\"lazy\"><\/p>\n<h4>GoRM \u9ed8\u8ba4\u6a21\u578b<\/h4>\n<p>GORM\u5185\u7f6e\u4e86\u4e00\u4e2agorm.Model\u7ed3\u6784\u4f53\u3002gorm.Model\u662f\u4e00\u4e2a\u5305\u542b\u4e86ID, CreatedAt, UpdatedAt, DeletedAt\u56db\u4e2a\u5b57\u6bb5\u7684Golang\u7ed3\u6784\u4f53\u3002<\/p>\n<pre><code class=\"go\">\/\/ gorm.Model \u5b9a\u4e49\ntype Model struct {\n  ID        uint `gorm:&quot;primary_key&quot;`\n  CreatedAt time.Time\n  UpdatedAt time.Time\n  DeletedAt *time.Time\n}<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u7ee7\u627f\u5230\u81ea\u5df1\u7684\u7ed3\u6784\u4f53\u4e2d<\/p>\n<pre><code class=\"go\">\/\/ \u5c06 `ID`, `CreatedAt`, `UpdatedAt`, `DeletedAt`\u5b57\u6bb5\u6ce8\u5165\u5230`Student`\u6a21\u578b\u4e2d\ntype Student struct {\n  gorm.Model\n  Name string\n  Age int\n}<\/code><\/pre>\n<h4>Model \u5b9a\u4e49<\/h4>\n<pre><code class=\"go\">type User struct {\n  gorm.Model\n  Name         string\n  Age          sql.NullInt64\n  Birthday     *time.Time\n  Email        string  `gorm:&quot;type:varchar(100);unique_index&quot;`\n  Role         string  `gorm:&quot;size:255&quot;` \/\/ \u8bbe\u7f6e\u5b57\u6bb5\u5927\u5c0f\u4e3a255\n  MemberNumber *string `gorm:&quot;unique;not null&quot;` \/\/ \u8bbe\u7f6e\u4f1a\u5458\u53f7\uff08member number\uff09\u552f\u4e00\u5e76\u4e14\u4e0d\u4e3a\u7a7a\n  Num          int     `gorm:&quot;AUTO_INCREMENT&quot;` \/\/ \u8bbe\u7f6e num \u4e3a\u81ea\u589e\u7c7b\u578b\n  Address      string  `gorm:&quot;index:addr&quot;` \/\/ \u7ed9address\u5b57\u6bb5\u521b\u5efa\u540d\u4e3aaddr\u7684\u7d22\u5f15\n  IgnoreMe     int     `gorm:&quot;-&quot;` \/\/ \u5ffd\u7565\u672c\u5b57\u6bb5\n}<\/code><\/pre>\n<p>User \u7ed3\u6784\u4f53\u7684\u5c5e\u6027 Birthday \u548c MemberNumber\uff0c\u4f7f\u7528\u6307\u9488\uff0c\u662f\u6709\u4ec0\u4e48\u542b\u4e49\u4e48\uff1f<\/p>\n<blockquote><p>\u9ed8\u8ba4\u6240\u6709\u5b57\u6bb5\u7684\u96f6\u503c, \u6bd4\u5982 0, &#8221;, false \u6216\u8005\u5176\u5b83 \u96f6\u503c\uff0c\u90fd\u4e0d\u4f1a\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u5185\uff0c\u4f7f\u7528\u6307\u9488\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002<\/p><\/blockquote>\n<h4>\u7ed3\u6784\u4f53\u6807\u8bb0(tags)<\/h4>\n<h3>\u7ed3\u6784\u4f53\u6807\u8bb0(tags)<\/h3>\n<p>\u4f7f\u7528\u7ed3\u6784\u4f53\u58f0\u660e\u6a21\u578b\u65f6\uff0c\u6807\u8bb0\uff08tags\uff09\u662f\u53ef\u9009\u9879\u3002<br \/>\u6253\u6807\u8bb0\u7684\u4f5c\u7528\uff1a<\/p>\n<blockquote><p>\u662f\u5bf9\u6570\u636e\u8868\u7684\u5b57\u6bb5\u505a\u4fee\u9970\uff0c\u4f8b\u5982\uff08\u81ea\u589e\uff0c\u4e3b\u952e\uff0c\u5927\u5c0f\uff0c\u7c7b\u578b\uff0c\u7d22\u5f15&#8230;&#8230;..\uff09<\/p><\/blockquote>\n<h4>\u7ed3\u6784\u4f53\u6807\u8bb0<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u7ed3\u6784\u4f53\u6807\u8bb0\uff08Tag\uff09<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Column<\/td>\n<td>\u6307\u5b9a\u5217\u540d<\/td>\n<\/tr>\n<tr>\n<td>Type<\/td>\n<td>\u6307\u5b9a\u5217\u6570\u636e\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>Size<\/td>\n<td>\u6307\u5b9a\u5217\u5927\u5c0f, \u9ed8\u8ba4\u503c255<\/td>\n<\/tr>\n<tr>\n<td>PRIMARY_KEY<\/td>\n<td>\u5c06\u5217\u6307\u5b9a\u4e3a\u4e3b\u952e<\/td>\n<\/tr>\n<tr>\n<td>UNIQUE<\/td>\n<td>\u5c06\u5217\u6307\u5b9a\u4e3a\u552f\u4e00<\/td>\n<\/tr>\n<tr>\n<td>DEFAULT<\/td>\n<td>\u6307\u5b9a\u5217\u9ed8\u8ba4\u503c<\/td>\n<\/tr>\n<tr>\n<td>PRECISION<\/td>\n<td>\u6307\u5b9a\u5217\u7cbe\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>NOT NULL<\/td>\n<td>\u5c06\u5217\u6307\u5b9a\u4e3a\u975e NULL<\/td>\n<\/tr>\n<tr>\n<td>AUTO_INCREMENT<\/td>\n<td>\u6307\u5b9a\u5217\u662f\u5426\u4e3a\u81ea\u589e\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>INDEX<\/td>\n<td>\u521b\u5efa\u5177\u6709\u6216\u4e0d\u5e26\u540d\u79f0\u7684\u7d22\u5f15, \u5982\u679c\u591a\u4e2a\u7d22\u5f15\u540c\u540d\u5219\u521b\u5efa\u590d\u5408\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td>UNIQUE_INDEX<\/td>\n<td>\u548c INDEX \u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u521b\u5efa\u7684\u662f\u552f\u4e00\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td>EMBEDDED<\/td>\n<td>\u5c06\u7ed3\u6784\u8bbe\u7f6e\u4e3a\u5d4c\u5165<\/td>\n<\/tr>\n<tr>\n<td>EMBEDDED_PREFIX<\/td>\n<td>\u8bbe\u7f6e\u5d4c\u5165\u7ed3\u6784\u7684\u524d\u7f00<\/td>\n<\/tr>\n<tr>\n<td>&#8211;<\/td>\n<td>\u5ffd\u7565\u6b64\u5b57\u6bb5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5173\u8054\u76f8\u5173\u6807\u8bb0<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7ed3\u6784\u4f53\u6807\u8bb0\uff08Tag\uff09<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>MANY2MANY<\/td>\n<td>\u6307\u5b9a\u8fde\u63a5\u8868<\/td>\n<\/tr>\n<tr>\n<td>FOREIGNKEY<\/td>\n<td>\u8bbe\u7f6e\u5916\u952e<\/td>\n<\/tr>\n<tr>\n<td>ASSOCIATION_FOREIGNKEY<\/td>\n<td>\u8bbe\u7f6e\u5173\u8054\u5916\u952e<\/td>\n<\/tr>\n<tr>\n<td>POLYMORPHIC<\/td>\n<td>\u6307\u5b9a\u591a\u6001\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>POLYMORPHIC_VALUE<\/td>\n<td>\u6307\u5b9a\u591a\u6001\u503c<\/td>\n<\/tr>\n<tr>\n<td>JOINTABLE_FOREIGNKEY<\/td>\n<td>\u6307\u5b9a\u8fde\u63a5\u8868\u7684\u5916\u952e<\/td>\n<\/tr>\n<tr>\n<td>ASSOCIATION_JOINTABLE_FOREIGNKEY<\/td>\n<td>\u6307\u5b9a\u8fde\u63a5\u8868\u7684\u5173\u8054\u5916\u952e<\/td>\n<\/tr>\n<tr>\n<td>SAVE_ASSOCIATIONS<\/td>\n<td>\u662f\u5426\u81ea\u52a8\u5b8c\u6210 save \u7684\u76f8\u5173\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>ASSOCIATION_AUTOUPDATE<\/td>\n<td>\u662f\u5426\u81ea\u52a8\u5b8c\u6210 update \u7684\u76f8\u5173\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>ASSOCIATION_AUTOCREATE<\/td>\n<td>\u662f\u5426\u81ea\u52a8\u5b8c\u6210 create \u7684\u76f8\u5173\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>ASSOCIATION_SAVE_REFERENCE<\/td>\n<td>\u662f\u5426\u81ea\u52a8\u5b8c\u6210\u5f15\u7528\u7684 save \u7684\u76f8\u5173\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>PRELOAD<\/td>\n<td>\u662f\u5426\u81ea\u52a8\u5b8c\u6210\u9884\u52a0\u8f7d\u7684\u76f8\u5173\u64cd\u4f5c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u4e3b\u952e.  \u8868\u540d. \u5217\u540d\u7ea6\u5b9a<\/h4>\n<h4>\u4e3b\u952e<\/h4>\n<p>GORM \u9ed8\u8ba4\u4f1a\u4f7f\u7528\u540d\u4e3aID\u7684\u5b57\u6bb5\u4f5c\u4e3a\u8868\u7684\u4e3b\u952e\u3002<\/p>\n<pre><code class=\"go\">type User struct {\n  ID   string \/\/ \u540d\u4e3a`ID`\u7684\u5b57\u6bb5\u4f1a\u9ed8\u8ba4\u4f5c\u4e3a\u8868\u7684\u4e3b\u952e\n  Name string\n}\n\n\/\/ \u4f7f\u7528`StudentlID`\u4f5c\u4e3a\u4e3b\u952e\ntype Student struct {\n  StudentID int64 `gorm:&quot;primary_key&quot;`\n  Name     string\n  Age      int64\n}<\/code><\/pre>\n<h4>\u8868\u540d<\/h4>\n<ol>\n<li>\u8868\u540d\u9ed8\u8ba4\u662f\u7ed3\u6784\u4f53\u540d\u79f0\u7684\u590d\u6570, \u4e5f\u53ef\u4ee5\u53d6\u6d88\u9ed8\u8ba4\u590d\u6570\u8868\u540d<\/li>\n<\/ol>\n<pre><code class=\"go\">type User struct {} \/\/ \u9ed8\u8ba4\u8868\u540d\u662f `users`\n\n\/\/ \u5c06 User \u7684\u8868\u540d\u8bbe\u7f6e\u4e3a `user`\nfunc (User) TableName() string {\n  return &quot;user&quot;\n}\n\n\n\n\/\/ \u7981\u7528\u9ed8\u8ba4\u8868\u540d\u7684\u590d\u6570\u5f62\u5f0f\uff0c\u5982\u679c\u7f6e\u4e3a true\uff0c\u5219 `User` \u7684\u9ed8\u8ba4\u8868\u540d\u662f `user`\ndb.SingularTable(true)\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u4e5f\u53ef\u4ee5\u901a\u8fc7Table()\u6307\u5b9a\u8868\u540d\uff1a<\/li>\n<\/ol>\n<pre><code class=\"go\">\/\/ \u4f7f\u7528User\u7ed3\u6784\u4f53\u521b\u5efa\u540d\u4e3a`student`\u7684\u8868\ndb.Table(&quot;student&quot;).CreateTable(&amp;User{})\n<\/code><\/pre>\n<ol start=\"3\">\n<li>GORM\u8fd8\u652f\u6301\u66f4\u6539\u9ed8\u8ba4\u8868\u540d\u79f0\u89c4\u5219\uff1a<\/li>\n<\/ol>\n<pre><code class=\"go\">gorm.DefaultTableNameHandler = func (db *gorm.DB, defaultTableName string) string  {\n  return &quot;prefix_&quot; + defaultTableName;\n}\n\n&quot;prefix_&quot; \u53ef\u4efb\u610f\u540d\u79f0<\/code><\/pre>\n<h3>\u5217\u540d<\/h3>\n<p>\u5217\u540d\u7531\u5b57\u6bb5\u540d\u79f0\u8fdb\u884c\u4e0b\u5212\u7ebf\u5206\u5272\u6765\u751f\u6210 ,  \u7ed3\u6784\u4f53\u5b57\u6bb5\u4e3a\u9a7c\u5cf0\u547d\u540d\u65f6\uff0c\u7b2c\u4e8c\u4e2a\u5355\u8bcd\u4f1a\u5728\u524d\u9762\u4ee5\u4e0b\u5212\u7ebf\u663e\u793a \uff0c\u4f8b\u5982\uff1a<br \/>AddressInfo  &#8212;&gt;  address_info<\/p>\n<pre><code class=\"go\">type User struct {\n  ID        uint      \/\/ column name is `id`\n  Name      string    \/\/ column name is `name`\n  Birthday  time.Time \/\/ column name is `birthday`\n  CreatedAt time.Time \/\/ column name is `created_at`\n}<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u7ed3\u6784\u4f53Tag \u6307\u5b9a\u5217\u540d<\/strong><\/p>\n<pre><code class=\"go\">type Animal struct {\n  AnimalId    int64     `gorm:&quot;column:beast_id&quot;`         \/\/ set column name to `beast_id`\n  Birthday    time.Time `gorm:&quot;column:day_of_the_beast&quot;` \/\/ set column name to `day_of_the_beast`\n  Age         int64     `gorm:&quot;column:age_of_the_beast&quot;` \/\/ set column name to `age_of_the_beast`\n}<\/code><\/pre>\n<h3>\u65f6\u95f4\u6233\u8ddf\u8e2a<\/h3>\n<h4>CreatedAt<\/h4>\n<p>\u5982\u679c\u6a21\u578b\u6709 CreatedAt\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u7684\u503c\u5c06\u4f1a\u662f\u521d\u6b21\u521b\u5efa\u8bb0\u5f55\u7684\u65f6\u95f4\u3002<\/p>\n<pre><code class=\"go\">db.Create(&amp;user) \/\/ `CreatedAt`\u5c06\u4f1a\u662f\u5f53\u524d\u65f6\u95f4 \n\/\/ \u53ef\u4ee5\u4f7f\u7528`Update`\u65b9\u6cd5\u6765\u6539\u53d8`CreateAt`\u7684\u503c \ndb.Model(&amp;user).Update(&quot;CreatedAt&quot;, time.Now()) <\/code><\/pre>\n<h4>UpdatedAt<\/h4>\n<p>\u5982\u679c\u6a21\u578b\u6709UpdatedAt\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u7684\u503c\u5c06\u4f1a\u662f\u6bcf\u6b21\u66f4\u65b0\u8bb0\u5f55\u7684\u65f6\u95f4\u3002<\/p>\n<pre><code class=\"go\">db.Save(&amp;user) \/\/ `UpdatedAt`\u5c06\u4f1a\u662f\u5f53\u524d\u65f6\u95f4 \ndb.Model(&amp;user).Update(&quot;name&quot;, &quot;jinzhu&quot;) \n\/\/ `UpdatedAt`\u5c06\u4f1a\u662f\u5f53\u524d\u65f6\u95f4 <\/code><\/pre>\n<h4>DeletedAt<\/h4>\n<p>\u5982\u679c\u6a21\u578b\u6709DeletedAt\u5b57\u6bb5\uff0c\u8c03\u7528Delete\u5220\u9664\u8be5\u8bb0\u5f55\u65f6\uff0c\u5c06\u4f1a\u8bbe\u7f6eDeletedAt\u5b57\u6bb5\u4e3a\u5f53\u524d\u65f6\u95f4\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5c06\u8bb0\u5f55\u4ece\u6570\u636e\u5e93\u4e2d\u5220\u9664\u3002<\/p>\n<h3>\u9879\u76ee\u5f00\u53d1\u4e2d<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc35VO\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc35VP\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc35VQ\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc35VS\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042882759\">https:\/\/segmentfault.com\/a\/1190000042882759<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u53c2\u4e0e\u4e86\u601d\u5426\u6280\u672f\u5f81\u6587\uff0c\u6b22\u8fce\u6b63\u5728\u9605\u8bfb\u7684\u4f60\u4e5f\u52a0\u5165\u3002 Hello\uff0c\u5927\u5bb6\u597d\uff0c\u6211\u662f\u6d77\u519b\uff0c\u6700\u8fd1\u4e00\u76f4\u5728\u5b66\u4e60Go\uff0c\u76ee\u524d\u5728\u505a\u9879 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006905","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006905"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006905\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}