{"id":568,"date":"2018-12-12T15:00:42","date_gmt":"2018-12-12T06:00:42","guid":{"rendered":"https:\/\/weseek.co.jp\/tech\/?p=568"},"modified":"2023-04-17T10:30:16","modified_gmt":"2023-04-17T01:30:16","slug":"beego-%e3%81%a7%e5%ae%9f%e8%a3%85%e3%81%97%e3%81%9f%e6%8e%b2%e7%a4%ba%e6%9d%bf%e3%82%b5%e3%83%bc%e3%83%93%e3%82%b9%e3%81%ae-go%e8%a8%80%e8%aa%9e%e3%82%92%e8%aa%ad%e3%82%93%e3%81%a7%e3%81%bf%e3%82%8b","status":"publish","type":"post","link":"https:\/\/weseek.co.jp\/tech\/568\/","title":{"rendered":"beego\u3067\u5b9f\u88c5\u3057\u305f\u63b2\u793a\u677f\u30b5\u30fc\u30d3\u30b9\u306eGo\u8a00\u8a9e\u3092\u8aad\u3093\u3067\u307f\u308b|\u305d\u306e2"},"content":{"rendered":"<p>\u5b8c\u5168\u306b<a href=\"https:\/\/weseek.co.jp\/tech\/366\/\">beego\u3067\u5b9f\u88c5\u3057\u305f\u63b2\u793a\u677f\u30b5\u30fc\u30d3\u30b9\u306e Go\u8a00\u8a9e\u3092\u8aad\u3080|\u305d\u306e1<\/a>\u306e\u7d9a\u304d\u3067\u3001\u5f15\u304d\u7d9a\u304d<a href=\"https:\/\/weseek.co.jp\/tech\/484\/\">go\u8a00\u8a9e+beego\u3067\u63b2\u793a\u677f\u3092\u4f5c\u308b<\/a> \u3067\u4f5c\u6210\u3057\u305f <a href=\"https:\/\/github.com\/haruhikonyan\/beegotest\">\u63b2\u793a\u677f\u98a8\u30a2\u30d7\u30ea<\/a> \u3067 beego \u306e scaffold \u6a5f\u80fd\u3092\u4f7f\u3063\u3066\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f go \u306e\u30b3\u30fc\u30c9\u3092\u8aad\u307f\u9032\u3081\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u524d\u56de\u306e\u91cd\u8907\u306b\u306f\u306a\u308a\u307e\u3059\u304c \u7b46\u8005\u306f <a href=\"https:\/\/go-tour-jp.appspot.com\/\">A Tour of Go<\/a> \u3092\u4e00\u5468\u3057\u305f\u7a0b\u5ea6\u3067\u3042\u308a\u3001\u521d\u5fc3\u8005\u306e\u521d\u5fc3\u8005\u306b\u3088\u308b\u521d\u5fc3\u8005\u306e\u305f\u3081\u306b\u308f\u304b\u308a\u3084\u3059\u304f\u8aac\u660e\u3092\u884c\u3046\u3053\u3068\u3092\u5fc3\u639b\u3051\u3066\u3044\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u5f15\u304d\u7d9a\u304d<a href=\"https:\/\/go-tour-jp.appspot.com\/\">A Tour of Go<\/a> \u306e\u30ea\u30f3\u30af\u306a\u3069\u3092\u4ea4\u3048\u3066\u89e3\u8aac\u3057\u3066\u3044\u3053\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n\n<h2>\/models\/post.go <span class=\"revision-head-edit-button\"><\/span><\/h2>\n<h3>\u5404\u7a2eCRUD <span class=\"revision-head-edit-button\"><\/span><\/h3>\n<p><a href=\"https:\/\/github.com\/haruhikonyan\/beegotest\/blob\/master\/haruch\/models\/post.go#L41-L143\"><\/a><a href=\"https:\/\/github.com\/haruhikonyan\/beegotest\/blob\/master\/haruch\/models\/post.go#L41-L143\">https:\/\/github.com\/haruhikonyan\/beegotest\/blob\/master\/haruch\/models\/post.go#L41-L143<\/a><br \/>\n\u524d\u56de\u306e\u7d9a\u304d\u3067\u3053\u3053\u304b\u3089\u30b9\u30bf\u30fc\u30c8\u3067\u3059\u3002<\/p>\n<ul>\n<li>GetAllPost<\/li>\n<\/ul>\n<pre><code>&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ GetAllPost retrieves all Post matches certain condition. Returns empty list if&lt;\/span&gt;\n&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ no records exist&lt;\/span&gt;\n&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;func&lt;\/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;GetAllPost&lt;\/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(query &lt;span class=&quot;hljs-keyword&quot;&gt;map&lt;\/span&gt;[&lt;span class=&quot;hljs-keyword&quot;&gt;string&lt;\/span&gt;]&lt;span class=&quot;hljs-keyword&quot;&gt;string&lt;\/span&gt;, fields []&lt;span class=&quot;hljs-keyword&quot;&gt;string&lt;\/span&gt;, sortby []&lt;span class=&quot;hljs-keyword&quot;&gt;string&lt;\/span&gt;, order []&lt;span class=&quot;hljs-keyword&quot;&gt;string&lt;\/span&gt;,\noffset &lt;span class=&quot;hljs-keyword&quot;&gt;int64&lt;\/span&gt;, limit &lt;span class=&quot;hljs-keyword&quot;&gt;int64&lt;\/span&gt;)&lt;\/span&gt; &lt;span class=&quot;hljs-params&quot;&gt;(ml []&lt;span class=&quot;hljs-keyword&quot;&gt;interface&lt;\/span&gt;{}, err error)&lt;\/span&gt;&lt;\/span&gt; {\no := orm.NewOrm()\nqs := o.QueryTable(&lt;span class=&quot;hljs-built_in&quot;&gt;new&lt;\/span&gt;(Post))\n&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ query k=v&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; k, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; query {\n    &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ rewrite dot-notation to Object__Attribute&lt;\/span&gt;\n    k = strings.Replace(k, &lt;span class=&quot;hljs-string&quot;&gt;&quot;.&quot;&lt;\/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;__&quot;&lt;\/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;-1&lt;\/span&gt;)\n    qs = qs.Filter(k, v)\n}\n&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ order by:&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;\/span&gt; sortFields []&lt;span class=&quot;hljs-keyword&quot;&gt;string&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(sortby) != &lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt; {\n    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(sortby) == &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) {\n        &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ 1) for each sort field, there is an associated order&lt;\/span&gt;\n        &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; i, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; sortby {\n            orderby := &lt;span class=&quot;hljs-string&quot;&gt;&quot;&quot;&lt;\/span&gt;\n            &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; order[i] == &lt;span class=&quot;hljs-string&quot;&gt;&quot;desc&quot;&lt;\/span&gt; {\n                orderby = &lt;span class=&quot;hljs-string&quot;&gt;&quot;-&quot;&lt;\/span&gt; + v\n            } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; order[i] == &lt;span class=&quot;hljs-string&quot;&gt;&quot;asc&quot;&lt;\/span&gt; {\n                orderby = v\n            } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; {\n                &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, errors.New(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Error: Invalid order. Must be either [asc|desc]&quot;&lt;\/span&gt;)\n            }\n            sortFields = &lt;span class=&quot;hljs-built_in&quot;&gt;append&lt;\/span&gt;(sortFields, orderby)\n        }\n        qs = qs.OrderBy(sortFields...)\n    } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(sortby) != &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) &amp;&amp; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) == &lt;span class=&quot;hljs-number&quot;&gt;1&lt;\/span&gt; {\n        &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ 2) there is exactly one order, all the sorted fields will be sorted by this order&lt;\/span&gt;\n        &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; _, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; sortby {\n            orderby := &lt;span class=&quot;hljs-string&quot;&gt;&quot;&quot;&lt;\/span&gt;\n            &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; order[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;] == &lt;span class=&quot;hljs-string&quot;&gt;&quot;desc&quot;&lt;\/span&gt; {\n                orderby = &lt;span class=&quot;hljs-string&quot;&gt;&quot;-&quot;&lt;\/span&gt; + v\n            } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; order[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;] == &lt;span class=&quot;hljs-string&quot;&gt;&quot;asc&quot;&lt;\/span&gt; {\n                orderby = v\n            } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; {\n                &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, errors.New(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Error: Invalid order. Must be either [asc|desc]&quot;&lt;\/span&gt;)\n            }\n            sortFields = &lt;span class=&quot;hljs-built_in&quot;&gt;append&lt;\/span&gt;(sortFields, orderby)\n        }\n    } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(sortby) != &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) &amp;&amp; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) != &lt;span class=&quot;hljs-number&quot;&gt;1&lt;\/span&gt; {\n        &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, errors.New(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Error: &#039;sortby&#039;, &#039;order&#039; sizes mismatch or &#039;order&#039; size is not 1&quot;&lt;\/span&gt;)\n    }\n} &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; {\n    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) != &lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt; {\n        &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, errors.New(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Error: unused &#039;order&#039; fields&quot;&lt;\/span&gt;)\n    }\n}\n\n&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;\/span&gt; l []Post\nqs = qs.OrderBy(sortFields...).RelatedSel()\n&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; _, err = qs.Limit(limit, offset).All(&amp;l, fields...); err == &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt; {\n    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(fields) == &lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt; {\n        &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; _, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; l {\n            ml = &lt;span class=&quot;hljs-built_in&quot;&gt;append&lt;\/span&gt;(ml, v)\n        }\n    } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; {\n        &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ trim unused fields&lt;\/span&gt;\n        &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; _, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; l {\n            m := &lt;span class=&quot;hljs-built_in&quot;&gt;make&lt;\/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;map&lt;\/span&gt;[&lt;span class=&quot;hljs-keyword&quot;&gt;string&lt;\/span&gt;]&lt;span class=&quot;hljs-keyword&quot;&gt;interface&lt;\/span&gt;{})\n            val := reflect.ValueOf(v)\n            &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; _, fname := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; fields {\n                m[fname] = val.FieldByName(fname).Interface()\n            }\n            ml = &lt;span class=&quot;hljs-built_in&quot;&gt;append&lt;\/span&gt;(ml, m)\n        }\n    }\n    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; ml, &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;\n}\n&lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, err\n}<\/code><\/pre>\n<p>\u9577\u3044\u3002\u3002\u3002<br \/>\n\u3084\u308d\u3046\u3068\u3057\u3066\u3044\u308b\u3053\u3068\u306f\u6307\u5b9a\u3055\u308c\u305f\u5f15\u6570\u306b\u57fa\u3065\u3044\u3066\u4e00\u81f4\u3059\u308b Post \u3092 DB \u304b\u3089\u3059\u3079\u3066\u53d6\u5f97\u3059\u308b\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<h4>\u5f15\u6570 <span class=\"revision-head-edit-button\"><\/span><\/h4>\n<ul>\n<li>query map[string]string<br \/>\n<code>map[string]string<\/code> \u3068\u306f string \u304c key \u3067 value \u3082 string \u3067\u3042\u308b <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/19\">map<\/a> \u306e\u3053\u3068\u3067\u3059\u3002<br \/>\n\u3053\u306e query \u306b\u306f\u7d5e\u308a\u8fbc\u307f\u5bfe\u8c61\u306e\u30d5\u30a3\u30fc\u30eb\u30c9 key \u306b\u5bfe\u3057\u3066\u3001\u7d5e\u308a\u8fbc\u307f\u6587\u5b57\u5217\u306e value \u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<li>fields []string<br \/>\nDB \u304b\u3089\u5f15\u3063\u5f35\u3063\u3066\u304f\u308b\u30ab\u30e9\u30e0\u3092\u5236\u9650\u3059\u308b\u969b\u306b\u3053\u3053\u306e\u5f15\u6570\u306b\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<li>sortby []string<br \/>\n\u30bd\u30fc\u30c8\u3092\u3059\u308b\u969b\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u3092\u3053\u3053\u306b\u6307\u5b9a\u3057\u3001\u8907\u6570\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/li>\n<li>order []string<br \/>\nsortby \u3067\u6307\u5b9a\u3057\u305f\u30bd\u30fc\u30c8\u3059\u308b\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u6607\u9806\u306b\u3059\u308b\u304b\u964d\u9806\u306b\u3059\u308b\u304b\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002<br \/>\n\u578b\u81ea\u4f53\u306f\u305f\u3060\u306e string \u306e<a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/7\">\u30b9\u30e9\u30a4\u30b9<\/a>\u3067\u3059\u304c\u3001\u4f55\u3092<a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/7\">\u30b9\u30e9\u30a4\u30b9<\/a>\u306b\u5165\u308c\u3066\u3082\u826f\u3044\u3068\u3046\u8a33\u3067\u306f\u306a\u304f\u3001\u8a8d\u3081\u3089\u308c\u3066\u3044\u308b\u306e\u306f\u6607\u9806\u3067\u3042\u308b <code>asc<\/code> \u3068\u3001\u964d\u9806\u3067\u3042\u308b <code>desc<\/code> \u306e\u3069\u3061\u3089\u304b\u306e\u6587\u5b57\u5217\u306e<a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/7\">\u30b9\u30e9\u30a4\u30b9<\/a>\u3067\u3059\u3002<br \/>\n\u5f8c\u8ff0\u306e\u51e6\u7406\u3092\u898b\u3066\u3044\u305f\u3060\u3051\u308c\u3070\u308f\u304b\u308a\u307e\u3059\u304c sortby \u306e<a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/7\">\u30b9\u30e9\u30a4\u30b9<\/a>\u3068\u5bfe\u5fdc\u3057\u3066\u304a\u308a\u3001\u6570\u3082\u4e00\u81f4\u3055\u305b\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u4f8b\u5916\u7684\u306b\u5168\u3066\u540c\u3058 order \u3067\u30bd\u30fc\u30c8\u3059\u308b\u5834\u5408\u306b\u306f\u8907\u6570\u306e <code>sortby<\/code> \u306b\u5bfe\u3057\u30661\u3064\u3060\u3051\u306e\u6307\u5b9a\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n<li>offset int64<br \/>\n\u3044\u308f\u3086\u308b<a href=\"https:\/\/wa3.i-3-i.info\/word11923.html\">\u30aa\u30d5\u30bb\u30c3\u30c8<\/a>\u3067\u3001\u6307\u5b9a\u3057\u305f\u6570\u6587\u5f8c\u308d\u306e\u3082\u306e\u304b\u3089\u53d6\u5f97\u3057\u307e\u3059\u3002<br \/>\npaging \u3068\u304b\u306e\u5b9f\u88c5\u3067\u3088\u304f\u4f7f\u308f\u308c\u308b\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002<\/li>\n<li>limit int64<br \/>\n\u3053\u308c\u3082\u6587\u5b57\u901a\u308a\u3067\u3001\u6700\u5927\u4f55\u4ef6\u53d6\u5f97\u3059\u308b\u304b\u3067\u3059\u3002<\/li>\n<\/ul>\n<h4>\u51e6\u7406\u5185\u5bb9 <span class=\"revision-head-edit-button\"><\/span><\/h4>\n<ul>\n<li>\u30af\u30a8\u30ea\u4f5c\u6210\u307e\u3067<\/li>\n<\/ul>\n<pre><code>o := orm.NewOrm()\nqs := o.QueryTable(&lt;span class=&quot;hljs-built_in&quot;&gt;new&lt;\/span&gt;(Post))\n&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ query k=v&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; k, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; query {\n&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ rewrite dot-notation to Object__Attribute&lt;\/span&gt;\nk = strings.Replace(k, &lt;span class=&quot;hljs-string&quot;&gt;&quot;.&quot;&lt;\/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;__&quot;&lt;\/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;-1&lt;\/span&gt;)\nqs = qs.Filter(k, v)\n}<\/code><\/pre>\n<p>\u307e\u305a <code>orm<\/code> \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057\u3066 <code>o<\/code> \u3068\u3044\u3046\u5909\u6570\u306b\u53d6\u308a\u307e\u3059\u3002\u305d\u306e\u3042\u3068\u3001<code>orm<\/code> \u306e<a href=\"https:\/\/beego.me\/docs\/mvc\/model\/query.md#advanced-queries\">QueryTable()<\/a>\u30e1\u30bd\u30c3\u30c9\u3067\u4e00\u756a\u4e0a\u306e\u4f8b\u306e2\u3064\u76ee\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u81ea\u4f53\u3092\u30c6\u30fc\u30d6\u30eb\u540d\u3068\u3057\u3066\u4f7f\u3044\u3001 <code>QuerySeter<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002(QuerySeter \u3063\u3066\u306a\u3093\u3060\u308d\u3046\u3063\u3066\u524d\u56de\u306e\u8a18\u4e8b\u3067\u306f\u8a00\u3063\u3066\u3044\u305f\u3093\u3067\u3059\u304c\u3001\u5225\u306e\u4eba\u304b\u3089 setter \u306a\u3093\u3058\u3083\u306a\u3044\u304b\u3068\u306e\u52a9\u8a00\u3092\u3044\u305f\u3060\u304d\u3001\u3084\u3063\u3068\u610f\u5473\u304c\u901a\u308a\u307e\u3057\u305f\uff01\u30af\u30a8\u30ea\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3082\u306e\u306a\u3093\u3067\u3059\u306d(\u307e\u3093\u307e))<br \/>\n\u305d\u306e\u5f8c <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/16\">range<\/a> \u3067 \u5f15\u6570\u3067\u53d7\u3051\u53d6\u3063\u305f query \u3059\u3079\u3066\u3092\u51e6\u7406\u3057\u3066\u3044\u304d\u307e\u3059\u3002<br \/>\n<code>k<\/code> \u3064\u307e\u308a\u306f key \u3092 <a href=\"http:\/\/golang.jp\/pkg\/strings#Replace\">strings.Replace<\/a> \u3067 \u7b2c4\u5f15\u6570\u306b <code>-1<\/code> \u3092\u4e0e\u3048\u3066\u3044\u308b\u306e\u3067\u6587\u5b57\u5217\u5024\u306e\u3059\u3079\u3066\u306e<code>.<\/code> \u3092 <code>__<\/code> \u306b\u7f6e\u63db\u3057\u3066 <code>k<\/code> \u3078\u518d\u4ee3\u5165\u3057\u3066\u3044\u307e\u3059\u3002\u305d\u3057\u3066 <a href=\"https:\/\/beego.me\/docs\/mvc\/model\/query.md#filter\">qs.Filter<\/a> \u3078\u6e21\u3057\u3066\u3042\u3052\u307e\u3059\u3002\u3059\u308b\u3068\u3044\u308f\u3086\u308b SQL \u306e where \u3067\u306e\u7d5e\u308a\u8fbc\u307f\u304c\u3067\u304d\u308b\u308f\u3051\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u30bd\u30fc\u30c8\u51e6\u7406<\/li>\n<\/ul>\n<pre><code>&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ order by:&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;\/span&gt; sortFields []&lt;span class=&quot;hljs-keyword&quot;&gt;string&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(sortby) != &lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt; {\n&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(sortby) == &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) {\n    &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ 1) for each sort field, there is an associated order&lt;\/span&gt;\n    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; i, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; sortby {\n        orderby := &lt;span class=&quot;hljs-string&quot;&gt;&quot;&quot;&lt;\/span&gt;\n        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; order[i] == &lt;span class=&quot;hljs-string&quot;&gt;&quot;desc&quot;&lt;\/span&gt; {\n            orderby = &lt;span class=&quot;hljs-string&quot;&gt;&quot;-&quot;&lt;\/span&gt; + v\n        } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; order[i] == &lt;span class=&quot;hljs-string&quot;&gt;&quot;asc&quot;&lt;\/span&gt; {\n            orderby = v\n        } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; {\n            &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, errors.New(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Error: Invalid order. Must be either [asc|desc]&quot;&lt;\/span&gt;)\n        }\n        sortFields = &lt;span class=&quot;hljs-built_in&quot;&gt;append&lt;\/span&gt;(sortFields, orderby)\n    }\n    qs = qs.OrderBy(sortFields...)\n} &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(sortby) != &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) &amp;&amp; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) == &lt;span class=&quot;hljs-number&quot;&gt;1&lt;\/span&gt; {\n    &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ 2) there is exactly one order, all the sorted fields will be sorted by this order&lt;\/span&gt;\n    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; _, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; sortby {\n        orderby := &lt;span class=&quot;hljs-string&quot;&gt;&quot;&quot;&lt;\/span&gt;\n        &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; order[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;] == &lt;span class=&quot;hljs-string&quot;&gt;&quot;desc&quot;&lt;\/span&gt; {\n            orderby = &lt;span class=&quot;hljs-string&quot;&gt;&quot;-&quot;&lt;\/span&gt; + v\n        } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; order[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;] == &lt;span class=&quot;hljs-string&quot;&gt;&quot;asc&quot;&lt;\/span&gt; {\n            orderby = v\n        } &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; {\n            &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, errors.New(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Error: Invalid order. Must be either [asc|desc]&quot;&lt;\/span&gt;)\n        }\n        sortFields = &lt;span class=&quot;hljs-built_in&quot;&gt;append&lt;\/span&gt;(sortFields, orderby)\n    }\n} &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(sortby) != &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) &amp;&amp; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) != &lt;span class=&quot;hljs-number&quot;&gt;1&lt;\/span&gt; {\n    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, errors.New(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Error: &#039;sortby&#039;, &#039;order&#039; sizes mismatch or &#039;order&#039; size is not 1&quot;&lt;\/span&gt;)\n}\n} &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; {\n&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(order) != &lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt; {\n    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, errors.New(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Error: unused &#039;order&#039; fields&quot;&lt;\/span&gt;)\n}\n}<\/code><\/pre>\n<p>\u9577\u3044\u3067\u3059\u304c if \u6587\u3067\u5834\u5408\u5206\u3051\u3055\u308c\u3066\u3044\u308b\u3060\u3051\u306a\u306e\u3067\u51b7\u9759\u306b\u8aad\u307f\u89e3\u3044\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<br \/>\n\u307e\u305a\u306f <code>var sortFields []string<\/code> \u3067<code>sortFields<\/code> \u3068\u3044\u3046\u7a7a\u306e string \u306e<a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/7\">\u30b9\u30e9\u30a4\u30b9<\/a>\u3092\u5ba3\u8a00\u3057\u307e\u3059\u3002\u3053\u3053\u306b\u306f\u30bd\u30fc\u30c8\u3059\u308b\u5bfe\u8c61\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u3092\u683c\u7d0d\u3057\u3066\u3044\u304d\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306f\u5206\u5c90\u306b\u5165\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>\n<p><code>if len(sortby) != 0 {} else {}<\/code><br \/>\n\u5f15\u6570 <code>sortby<\/code> \u306e <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/11\">length<\/a> \u304c 0 \u3067\u306a\u3044\u5834\u5408\u3068 1 \u4ee5\u4e0a\u3067\u3042\u308b\u5834\u5408\u3067\u308f\u3051\u3089\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>\n<p><code>sortby<\/code> \u304c1\u3064\u4ee5\u4e0a\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408<br \/>\n\u3055\u3089\u306b\u5834\u5408\u5206\u3051<\/p>\n<ul>\n<li><code>if len(sortby) == len(order)<\/code><br \/>\n\u5f15\u6570\u3067\u6e21\u3055\u308c\u305f <code>sortby<\/code> \u3068 <code>order<\/code> \u306e<a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/7\">\u30b9\u30e9\u30a4\u30b9<\/a>\u306e\u30b5\u30a4\u30ba\u304c\u4e00\u81f4\u3057\u3066\u3044\u308b\u5834\u5408<br \/>\n<code>for i, v := range sortby<\/code> \u3067 <code>sortby<\/code> \u3092 <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/16\">range<\/a> \u3092\u4f7f\u3063\u3066\u9806\u756a\u306b\u51e6\u7406\u3057\u3066\u3044\u304d\u307e\u3059\u3002<br \/>\n\u307e\u305a\u7d50\u679c\u3092\u683c\u7d0d\u3059\u308b <code>orderby := &quot;&quot;<\/code> \u3092\u5b9a\u7fa9\u3002\u6b21\u306b\u5206\u5c90\u3067 <code>if order[i] == &quot;desc&quot;<\/code> <code>sortby<\/code> \u3068\u540c\u3058 index \u306e <code>order<\/code>\u304c <code>&quot;desc&quot;<\/code>(\u964d\u9806)\u3067\u3042\u3063\u305f\u5834\u5408\u306f <code>orderby = &quot;-&quot; + v<\/code> <code>sortby<\/code> \u306b <code>-<\/code> \u3092\u4ed8\u3051\u3066 <code>orderby<\/code> \u306b\u683c\u7d0d\u3057\u307e\u3059\u3002<br \/>\n\u306f\u305f\u307e\u305f <code>else if order[0] == &quot;asc&quot;<\/code> <code>&quot;asc&quot;<\/code> (\u6607\u9806)\u3067\u3042\u3063\u305f\u5834\u5408\u306f\u305d\u306e\u307e\u307e <code>sortby<\/code> \u3092 <code>orderby<\/code> \u306b\u683c\u7d0d\u3057\u307e\u3059\u3002<br \/>\n\u305d\u308c\u4ee5\u5916\u306e\u30d1\u30bf\u30fc\u30f3\u306f <code>order<\/code> \u306b <code>&quot;asc&quot;<\/code> <code>&quot;desc&quot;<\/code> \u4ee5\u5916\u306e\u5024\u304c<br \/>\n\u4ee3\u5165\u3055\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u30a8\u30e9\u30fc\u3092\u51fa\u529b\u3057\u307e\u3059\u3002<br \/>\n\u5206\u5c90\u3092\u629c\u3051\u305f\u3089\u6700\u5f8c\u306b\u4e00\u756a\u6700\u521d\u306b\u5b9a\u7fa9\u3057\u305f<code>sortFields<\/code> \u3078 <code>orderby<\/code> \u3092\u65b0\u305f\u306b <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/15\">append<\/a> \u3057\u307e\u3059\u3002<\/li>\n<li><code>if len(sortby) != len(order) &amp;&amp; len(order) == 1<\/code><br \/>\n\u5f15\u6570\u3067\u6e21\u3055\u308c\u305f <code>sortby<\/code> \u3068 <code>order<\/code> \u306e<a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/7\">\u30b9\u30e9\u30a4\u30b9<\/a>\u306e\u30b5\u30a4\u30ba\u304c\u4e00\u81f4\u3057\u3066\u304a\u3089\u305a\u3001<code>order<\/code> \u304c1\u3064\u3060\u3051\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408<br \/>\n\u57fa\u672c\u7684\u306b\u4e0a\u8a18\u306e <code>order<\/code> \u3092 <code>sortby<\/code> \u3068\u540c\u3058\u6570\u3060\u3051\u5b9a\u7fa9\u3057\u305f\u969b\u3068\u51e6\u7406\u306f\u5909\u308f\u3089\u305a\u3001\u3053\u3061\u3089\u306f <code>order<\/code> \u3092\u4e00\u3064\u3057\u304b\u6307\u5b9a\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u5e38\u306b<code>order[0]<\/code> \u3092\u6307\u5b9a\u3057\u3066\u3059\u3079\u3066\u306e <code>sortby<\/code> \u306b\u9069\u7528\u3057\u3001<code>orderby<\/code> \u306b\u683c\u7d0d\u3001\u6700\u5f8c\u306b <code>sortFields<\/code> \u306b <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/15\">append<\/a> \u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n<li><code>if len(sortby) != len(order) &amp;&amp; len(order) != 1<\/code><br \/>\n\u5f15\u6570\u3067\u6e21\u3055\u308c\u305f <code>sortby<\/code> \u3068 <code>order<\/code> \u306e<a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/7\">\u30b9\u30e9\u30a4\u30b9<\/a>\u306e\u30b5\u30a4\u30ba\u304c\u4e00\u81f4\u3057\u3066\u304a\u3089\u305a\u3001<code>order<\/code> \u306e\u6307\u5b9a\u304c1\u3064\u3067\u7121\u3044\u5834\u5408 error \u3092\u751f\u6210\u3057\u3066\u8fd4\u3057\u307e\u3059\u3002<br \/>\n<code>sortby<\/code> \u306b\u5bfe\u3057\u3066\u6570\u304c\u4e00\u81f4\u3057\u3066\u306a\u3044\u3068\u3053\u308d\u306b <code>order<\/code> \u304c2\u3064\u4ee5\u4e0a\u3042\u3063\u3066\u3082\u3069\u3046\u8fd4\u3057\u3066\u3044\u3044\u306e\u304b\u308f\u304b\u3089\u306a\u3044\u306e\u3067\u3042\u305f\u308a\u307e\u3067\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><code>sortby<\/code> \u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408<\/p>\n<pre><code>if len(order) != 0 {\n return nil, errors.New(&quot;Error: unused &#039;order&#039; fields&quot;)\n}<\/code><\/pre>\n<p>\u3053\u3053\u3082\u3055\u3089\u306b\u5206\u5c90\u304c\u3042\u308a\u3001<code>order<\/code> \u304c1\u3064\u4ee5\u4e0a\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f error \u3092\u751f\u6210\u3057\u3066\u8fd4\u3057\u307e\u3059\u3002<br \/>\n<code>sortby<\/code> \u304c\u7121\u3044\u306b\u3082\u304b\u304b\u308f\u3089\u305a <code>order<\/code> \u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u306e\u306f\u304a\u304b\u3057\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5b9f\u969b\u306b\u5024\u3092\u8fd4\u3059\u307e\u3067<\/p>\n<pre><code>&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;\/span&gt; l []Post\nqs = qs.OrderBy(sortFields...).RelatedSel()\n&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; _, err = qs.Limit(limit, offset).All(&amp;l, fields...); err == &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt; {\n&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; &lt;span class=&quot;hljs-built_in&quot;&gt;len&lt;\/span&gt;(fields) == &lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt; {\n    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; _, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; l {\n        ml = &lt;span class=&quot;hljs-built_in&quot;&gt;append&lt;\/span&gt;(ml, v)\n    }\n} &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;\/span&gt; {\n    &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ trim unused fields&lt;\/span&gt;\n    &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; _, v := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; l {\n        m := &lt;span class=&quot;hljs-built_in&quot;&gt;make&lt;\/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;map&lt;\/span&gt;[&lt;span class=&quot;hljs-keyword&quot;&gt;string&lt;\/span&gt;]&lt;span class=&quot;hljs-keyword&quot;&gt;interface&lt;\/span&gt;{})\n        val := reflect.ValueOf(v)\n        &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;\/span&gt; _, fname := &lt;span class=&quot;hljs-keyword&quot;&gt;range&lt;\/span&gt; fields {\n            m[fname] = val.FieldByName(fname).Interface()\n        }\n        ml = &lt;span class=&quot;hljs-built_in&quot;&gt;append&lt;\/span&gt;(ml, m)\n    }\n}\n&lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; ml, &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;\n}\n&lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt;, err<\/code><\/pre>\n<p>\u307e\u305a <code>var l []Post<\/code> \u3067 <code>Post<\/code> struct \u306e\u30b9\u30e9\u30a4\u30b9\u3092\u5b9a\u7fa9\u3057\u3066\u304a\u304d\u307e\u3059\u3002<br \/>\n\u6b21\u306b\u4e00\u756a\u6700\u521d\u306e\u307b\u3046\u3067\u53d6\u5f97\u3057\u3066\u304a\u3044\u305f <code>qs<\/code> (QuerySeter(Setter)) \u306e <a href=\"https:\/\/beego.me\/docs\/mvc\/model\/query.md#orderby\">OrderBy<\/a>\u306b <code>sortFields<\/code>... \u3092\u6e21\u3057\u3001\u30bd\u30fc\u30c8\u51e6\u7406\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002(\u4f8b\u3067\u306f\u4e00\u3064\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u5bfe\u3057\u3066\u3067\u3059\u304c\u53ef\u5909\u9577\u5f15\u6570\u3067\u8907\u6570\u306e\u5024\u3092\u6e21\u305b\u308b\u307f\u305f\u3044\u3067\u3059\u306d)\u305d\u308c\u306b\u5bfe\u3057\u3066<a href=\"https:\/\/beego.me\/docs\/mvc\/model\/query.md#relatedsel\">RelatedSel()<\/a> \u3067\u3001\u4f55\u3082\u5f15\u6570\u3092\u6307\u5b9a\u3057\u306a\u3044\u3053\u3068\u3067\u3001\u95a2\u9023\u30c6\u30fc\u30d6\u30eb\u3092\u3059\u3079\u3066 Join \u3057\u307e\u3059\u3002<br \/>\n\u3061\u306a\u307f\u306b <a href=\"https:\/\/golang.org\/ref\/spec#Passing_arguments_to_..._parameters\">...<\/a>\u3068\u306f\u53ef\u5909\u9577\u5f15\u6570\u306b\u5bfe\u3057\u3066\u914d\u5217\u3084\u30b9\u30e9\u30a4\u30b9\u3092\u5c55\u958b\u3057\u3066\u6e21\u305b\u308b\u3082\u306e\u3067\u3059\u3002<br \/>\n<code>qs.Limit(limit, offset).All(&amp;l, fields...)<\/code> \u3053\u3061\u3089\u3067\u306f <a href=\"https:\/\/beego.me\/docs\/mvc\/model\/query.md#limit\">qs.Limit<\/a> \u306e\u7b2c\u4e00\u5f15\u6570\u306b\u6700\u5927\u53d6\u5f97\u6570 <code>limit<\/code> \u3068 \u30aa\u30d5\u30bb\u30c3\u30c8 <code>offset<\/code> \u3092\u6307\u5b9a\u3057\u3066\u53d6\u5f97\u6570\u7b49\u3092\u7d5e\u308a\u307e\u3059\u3002\u305d\u3057\u3066 <a href=\"https:\/\/beego.me\/docs\/mvc\/model\/query.md#all\">All<\/a> \u306b\u6700\u521d\u306b\u5b9a\u7fa9\u3060\u3051\u3057\u305f <code>l<\/code> \u306e<a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/1\">\u30dd\u30a4\u30f3\u30bf<\/a>\u3068\u3001\u53d6\u5f97\u3059\u308b\u30d5\u30a3\u30fc\u30eb\u30c9\u540d\u3092\u6307\u5b9a\u3057\u305f <code>fields...<\/code> \u3068\u3057\u3066\u3059\u3079\u3066\u3092\u6e21\u3057\u3066\u3042\u3052\u53d6\u5f97\u3057\u307e\u3059\u3002All\u30e1\u30bd\u30c3\u30c9\u3067\u306f\u7b2c\u4e00\u5f15\u6570\u306b\u6e21\u3057\u305f <code>l<\/code> \u306b\u53d6\u5f97\u3057\u305f []Post \u304c\u4ee3\u5165\u3055\u308c\u307e\u3059\u3002<br \/>\n\u3044\u3064\u3082\u901a\u308a <code>err<\/code> \u304c <code>nil<\/code> \u3067\u3042\u308c\u3070 if \u306e\u4e2d\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<br \/>\n\u6307\u5b9a\u3057\u305f <code>fields<\/code> \u306e\u30b5\u30a4\u30ba\u304c0(\u672a\u6307\u5b9a)\u3067\u3042\u308c\u3070 <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/16\">range<\/a> \u3092\u4f7f\u3044 <code>l<\/code> \u306e\u4e2d\u8eab\u3092\u5168\u3066 <code>ml<\/code> \u3078 <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/15\">append<\/a> \u3057\u3066\u3044\u304d\u307e\u3059\u3002<br \/>\n\u6307\u5b9a\u3057\u305f <code>fields<\/code> \u304c\u5b58\u5728\u3057\u3066\u3044\u308c\u3070\u3053\u3061\u3089\u3082 <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/16\">range<\/a> \u3092\u4f7f\u3044 <code>l<\/code> \u306e\u4e2d\u8eab\u3092\u9806\u306b\u51e6\u7406\u3057\u307e\u3059\u3002<br \/>\n\u307e\u305a <code>m := make(map[string]interface{})<\/code> \u3067 <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/19\">map<\/a> \u3092 <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/22\">make<\/a>\u3067\u521d\u671f\u5316\u3057\u3001\u5909\u6570 <code>m<\/code> \u3078\u4ee3\u5165\u3057\u307e\u3059\u3002\u305d\u306e\u5f8c <a href=\"https:\/\/golang.org\/pkg\/reflect\/#ValueOf\">reflect.ValueOf<\/a> \u3067 reflect.Value \u578b\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\u3057 <code>val<\/code> \u306b\u4ee3\u5165\u3057\u307e\u3059\u3002<code>reflect.ValueOf<\/code> \u306b\u3064\u3044\u3066\u306f<a href=\"https:\/\/qiita.com\/atsaki\/items\/3554f5a0609c59a3e10d#reflect%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E5%9E%8B%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6%E3%81%AE%E6%83%85%E5%A0%B1%E3%82%92%E5%BE%97%E3%82%8B\">\u3053\u306e\u3078\u3093<\/a>\u304c\u53c2\u8003\u306b\u306a\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u7d9a\u3044\u3066\u5f15\u6570\u3067\u6307\u5b9a\u3055\u308c\u3066\u3044\u305f <code>fields<\/code> \u3092 <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/16\">range<\/a> \u3092\u4f7f\u3044\u3001\u3059\u3079\u3066\u306e\u6307\u5b9a\u3055\u308c\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u3092<a href=\"http:\/\/golang.jp\/pkg\/reflect#StructType.FieldByName\">val.FieldByName(fname)<\/a> \u3067\u53d6\u5f97\u3057\u3001<a href=\"https:\/\/www.soudegesu.com\/post\/go\/create-instance-with-reflection\/#reflect-interface-%E3%81%A7-interface-%E3%82%92%E8%BF%94%E5%8D%B4\">Interface()<\/a> \u3067<code>interface{}<\/code> \u3092\u53d6\u5f97\u3057\u3066 <code>m<\/code> \u306b\u4ee3\u5165\u3057\u3066\u3044\u304d\u307e\u3059\u3002\u3059\u3079\u3066\u4ee3\u5165\u304c\u7d42\u308f\u3063\u305f\u3068\u3053\u308d\u3067 <code>ml<\/code> \u3078\u3059\u3079\u3066 <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/15\">append<\/a> \u3057\u307e\u3059\u3002<br \/>\n\u3069\u3061\u3089\u304b\u306e\u5206\u5c90\u3067\u51e6\u7406\u304c\u5b8c\u4e86\u3057\u3001\u30a8\u30e9\u30fc\u304c\u7121\u3051\u308c\u3070 <code>return ml, nil<\/code> \u5024\u3092\u8fd4\u5374\u3057\u307e\u3059\u3002\u307e\u305f\u3053\u3053\u307e\u3067\u3067\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u5834\u5408\u306f <code>return nil, err<\/code> \u306b\u3066\u5024\u306f\u8fd4\u3055\u305a\u306b\u3001\u30a8\u30e9\u30fc\u306e\u307f\u3092\u8fd4\u5374\u3057\u307e\u3059\u3002<br \/>\n\u3068\u3066\u3082\u9577\u304b\u3063\u305f\u3067\u3059\u304c\u3001\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u306f\u4e0e\u3048\u3089\u308c\u305f\u5f15\u6570\u306e\u30b3\u30f3\u30c7\u30a3\u30b7\u30e7\u30f3\u306b\u3088\u3063\u3066 DB \u304b\u3089\u53d6\u5f97\u3057\u5f97\u3066\u304f\u308b\u5024\u3092\u5909\u3048\u3066\u3044\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<\/li>\n<li>\n<p>UpdatePostById<\/p>\n<pre><code><span class=\"hljs-comment\">\/\/ UpdatePost updates Post by Id and returns error if<\/span>\n<span class=\"hljs-comment\">\/\/ the record to be updated doesn't exist<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">UpdatePostById<\/span><span class=\"hljs-params\">(m *Post)<\/span> <span class=\"hljs-params\">(err error)<\/span><\/span> {\no := orm.NewOrm()\nv := Post{Id: m.Id}\n<span class=\"hljs-comment\">\/\/ ascertain id exists in the database<\/span>\n<span class=\"hljs-keyword\">if<\/span> err = o.Read(&v); err == <span class=\"hljs-literal\">nil<\/span> {\n    <span class=\"hljs-keyword\">var<\/span> num <span class=\"hljs-keyword\">int64<\/span>\n    <span class=\"hljs-keyword\">if<\/span> num, err = o.Update(m); err == <span class=\"hljs-literal\">nil<\/span> {\n        fmt.Println(<span class=\"hljs-string\">\"Number of records updated in database:\"<\/span>, num)\n    }\n}\n<span class=\"hljs-keyword\">return<\/span>\n}<\/code><\/pre>\n<p>\u6b21\u306b\u30ec\u30b3\u30fc\u30c9\u306e\u66f4\u65b0\u3067\u3059\u3002<br \/>\n\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u66f4\u65b0\u5f8c\u306e\u5024\u306e\u5165\u3063\u305f <code>Post<\/code> struct \u3092\u5f15\u6570\u3067\u6e21\u3057\u3066\u3042\u3052\u3001DB\u306e\u5024\u3092\u66f4\u65b0\u3059\u308b\u3082\u306e\u3067\u3059\u3002<br \/>\n\u6700\u521d\u306f\u3044\u3064\u3082\u901a\u308a <code>orm<\/code> \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057\u3066 <code>o<\/code> \u3068\u3044\u3046\u5909\u6570\u306b\u53d6\u308a\u3001\u5f15\u6570\u3067\u53d7\u3051\u53d6\u3063\u305f\u66f4\u65b0\u5bfe\u8c61\u3067\u3042\u308b <code>m.id<\/code> \u3067 <code>Post<\/code> \u306e\u69cb\u9020\u4f53\u3092\u521d\u671f\u5316\u3057\u3066 <code>v<\/code> \u306b\u4ee3\u5165\u3057\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/beego.me\/docs\/mvc\/model\/object.md#read\">o.Read()<\/a> \u306b\u5148\u307b\u3069\u521d\u671f\u5316\u3057\u305f <code>v<\/code> \u3092\u6e21\u3057\u3066\u3042\u3052\u3066\u5f15\u6570\u3067\u53d7\u3051\u53d6\u3063\u305f <code>id<\/code> \u3068\u4e00\u81f4\u3059\u308b Post \u304c DB \u5185\u306b\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<br \/>\n\u305d\u3057\u3066 <code>err<\/code> \u304c <code>nil<\/code> \u3067\u3042\u308c\u3070 if \u6587\u306e\u4e2d\u306b\u5165\u308a\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/beego.me\/docs\/mvc\/model\/object.md#update\">o.Update()<\/a>\u306b\u66f4\u65b0\u5bfe\u8c61\u306e ID \u3068\u3001\u66f4\u65b0\u3092\u3059\u308b\u5404\u7a2e\u5024\u3092\u6301\u3063\u305f Post \u3092\u6e21\u3057\u3066\u3042\u3052\u3001\u30a8\u30e9\u30fc\u7121\u304f\u51e6\u7406\u304c\u51fa\u6765\u305f\u3089 <code>fmt.Println<\/code> \u306b\u3066\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\u3057\u3001\u66f4\u65b0\u304c\u5b8c\u4e86\u3057\u307e\u3059\u3002<\/p>\n<\/li>\n<li>\n<p>DeletePost<\/p>\n<pre><code>&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ DeletePost deletes Post by Id and returns error if&lt;\/span&gt;\n&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ the record to be deleted doesn&#039;t exist&lt;\/span&gt;\n&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;func&lt;\/span&gt; &lt;span class=&quot;hljs-title&quot;&gt;DeletePost&lt;\/span&gt;&lt;span class=&quot;hljs-params&quot;&gt;(id &lt;span class=&quot;hljs-keyword&quot;&gt;int64&lt;\/span&gt;)&lt;\/span&gt; &lt;span class=&quot;hljs-params&quot;&gt;(err error)&lt;\/span&gt;&lt;\/span&gt; {\no := orm.NewOrm()\nv := Post{Id: id}\n&lt;span class=&quot;hljs-comment&quot;&gt;\/\/ ascertain id exists in the database&lt;\/span&gt;\n&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; err = o.Read(&amp;v); err == &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt; {\n    &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;\/span&gt; num &lt;span class=&quot;hljs-keyword&quot;&gt;int64&lt;\/span&gt;\n    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt; num, err = o.Delete(&amp;Post{Id: id}); err == &lt;span class=&quot;hljs-literal&quot;&gt;nil&lt;\/span&gt; {\n        fmt.Println(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Number of records deleted in database:&quot;&lt;\/span&gt;, num)\n    }\n}\n&lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;\/span&gt;\n}<\/code><\/pre>\n<p>\u6700\u5f8c\u306b\u30ec\u30b3\u30fc\u30c9\u306e\u524a\u9664\u3067\u3059\u3002 <code>id<\/code> \u3092\u53d7\u3051\u53d6\u3063\u3066\u305d\u306e id \u306e Post \u30ec\u30b3\u30fc\u30c9\u3092 DB \u304b\u3089\u524a\u9664\u3057\u307e\u3059\u3002<br \/>\n\u6bce\u5ea6\u304a\u306a\u3058\u307f <code>orm<\/code> \u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u751f\u6210\u3057\u3066 <code>o<\/code> \u3068\u3044\u3046\u5909\u6570\u306b\u53d6\u308a\u3001\u5f15\u6570\u3067\u53d7\u3051\u53d6\u3063\u305f <code>id<\/code> \u3067 <code>Post<\/code> \u306e\u69cb\u9020\u4f53\u3092\u521d\u671f\u5316\u3057\u3066 <code>v<\/code> \u306b\u4ee3\u5165\u3057\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/beego.me\/docs\/mvc\/model\/object.md#read\">o.Read()<\/a> \u306b\u5148\u307b\u3069\u521d\u671f\u5316\u3057\u305f <code>v<\/code> \u3092\u6e21\u3057\u3066\u3042\u3052\u3066\u5f15\u6570\u3067\u53d7\u3051\u53d6\u3063\u305f <code>id<\/code> \u3068\u4e00\u81f4\u3059\u308b Post \u304c DB \u5185\u306b\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<br \/>\n\u305d\u3057\u3066 <code>err<\/code> \u304c <code>nil<\/code> \u3067\u3042\u308c\u3070 if \u6587\u306e\u4e2d\u306b\u5165\u308a\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/beego.me\/docs\/mvc\/model\/object.md#delete\">o.Delete()<\/a>\u306b\u524a\u9664\u5bfe\u8c61\u306e ID \u3092\u6301\u3063\u305f Post \u3092\u6e21\u3057\u3066\u3042\u3052\u3001\u30a8\u30e9\u30fc\u7121\u304f\u51e6\u7406\u304c\u51fa\u6765\u305f\u3089 <code>fmt.Println<\/code> \u306b\u3066\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\u3057\u3001\u524a\u9664\u304c\u5b8c\u4e86\u3057\u307e\u3059\u3002<br \/>\n\u3061\u306a\u307f\u306b<a href=\"https:\/\/beego.me\/docs\/mvc\/model\/object.md#delete\">o.Delete()<\/a>\u306e\u5f15\u6570\u306b\u6e21\u3057\u3066\u308b <code>&amp;Post{Id: id}<\/code> \u3067\u3059\u304c\u3001\u540c\u3058\u3082\u306e\u3092\u5909\u6570\u306b\u53d6\u3063\u3066\u308b <code>&amp;v<\/code> \u3092\u6e21\u3057\u3066\u3082\u7d50\u679c\u306f\u5909\u308f\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u306a\u305c\u81ea\u52d5\u751f\u6210\u306e\u30b3\u30fc\u30c9\u304c <a href=\"https:\/\/go-tour-jp.appspot.com\/moretypes\/5\">struct<\/a> \u3092\u4f5c\u308a\u76f4\u3057\u3066\u3044\u308b\u306e\u304b\u306f\u3061\u3087\u3063\u3068\u3088\u304f\u308f\u304b\u308a\u307e\u305b\u3093\u3002(\u660e\u78ba\u306a\u7406\u7531\u304c\u3042\u308c\u3070\u305c\u3072\u8ab0\u304b\u6559\u3048\u3066\u304f\u3060\u3055\u3044\uff01)<\/p>\n<\/li>\n<\/ul>\n<h2>\u307e\u3068\u3081 <span class=\"revision-head-edit-button\"><\/span><\/h2>\n<p>\u3053\u3053\u307e\u3067\u3067\u524d\u56de\u3068\u5408\u308f\u305b\u3066 <code>\/models\/post.go<\/code> \u306e\u30b3\u30fc\u30c9\u3092\u3059\u3079\u3066\u8aad\u3093\u3067\u3044\u304d\u307e\u3057\u305f\u3002\u3060\u3044\u3076 go \u306e\u57fa\u672c\u7684\u306a\u6587\u6cd5\u3068 beego \u3067\u306e orm \u306e\u4f7f\u3044\u65b9\u306b\u306f\u6163\u308c\u3066\u304d\u305f\u3093\u3058\u3083\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002\u6587\u6cd5\u304c\u5206\u304b\u308c\u3070\u8907\u96d1\u306a\u51e6\u7406\u3082\u4e00\u3064\u4e00\u3064\u610f\u5473\u304c\u898b\u3048\u3066\u304d\u3066\u8d85\u9577\u304b\u3063\u305f GetAllPost \u30e1\u30bd\u30c3\u30c9\u3082\u7d50\u5c40\u306f\u30bd\u30fc\u30c8\u51e6\u7406\u3067\u914d\u5217\u4e8c\u3064\u3092\u8907\u96d1\u306b\u6761\u4ef6\u5206\u5c90\u3067\u51e6\u7406\u3057\u3066\u3044\u308b\u3060\u3051\u3067\u5927\u3057\u305f\u3053\u3068\u306f\u306a\u3044\u3068\u3044\u3046\u611f\u3058\u3067\u3057\u305f\u3002\u3061\u3087\u3063\u3068\u307e\u3060 <code>reflect<\/code> \u307f\u305f\u3044\u306a\u3061\u3087\u3063\u3068\u30c8\u30ea\u30c3\u30ad\u30fc\u306a\u578b\u304c\u7d61\u3093\u3060\u4f7f\u3044\u65b9\u307f\u305f\u3044\u306a\u306e\u306f\u307e\u3060\u307e\u3060\u52c9\u5f37\u304c\u5fc5\u8981\u3060\u3068\u601d\u3044\u307e\u3057\u305f\u304c\u3002<br \/>\n\u307f\u306a\u3055\u3093\u3082 <a href=\"https:\/\/go-tour-jp.appspot.com\/\">A Tour of Go<\/a> \u7247\u624b\u306b go \u306e\u30b3\u30fc\u30c9\u3092\u8aad\u307f\u305d\u3057\u3066\u66f8\u3044\u3066\u307f\u3066 web \u306e\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u30de\u30b9\u30bf\u30fc\u76ee\u6307\u3057\u3066\u307f\u3066\u306f\u3044\u304b\u304c\u3067\u3057\u3087\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b8c\u5168\u306bbeego\u3067\u5b9f\u88c5\u3057\u305f\u63b2\u793a\u677f\u30b5\u30fc\u30d3\u30b9\u306e Go\u8a00\u8a9e\u3092\u8aad\u3080|\u305d\u306e1\u306e\u7d9a\u304d\u3067\u3001\u5f15\u304d\u7d9a\u304dgo\u8a00\u8a9e+beego\u3067\u63b2\u793a\u677f\u3092\u4f5c\u308b \u3067\u4f5c\u6210\u3057\u305f \u63b2\u793a\u677f\u98a8\u30a2\u30d7\u30ea \u3067 beego \u306e scaffold \u6a5f\u80fd\u3092\u4f7f\u3063\u3066\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f go <\/p>\n","protected":false},"author":3,"featured_media":2180,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/568"}],"collection":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/comments?post=568"}],"version-history":[{"count":11,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/568\/revisions"}],"predecessor-version":[{"id":4092,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/568\/revisions\/4092"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media\/2180"}],"wp:attachment":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media?parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/categories?post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/tags?post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}